The best book to teach you to build professional, secure Android apps for the real world using the most important architectures and libraries. You’ll gain all the foundations of Android development you need to make the best decisions in your own codebase, while addressing critical problems like finding the right architecture, making the UI responsive and appealing, and implementing efficient animations.
Before You Begin
This section tells you a few things you need to know before you get started, such as what hardware and software you’ll need, where to find the project files for this book and more.
In this section, you’ll learn how to design and implement PetSave as an example of a professional, real world app. You’ll learn how to choose the right architectural pattern and how to structure the code to make the app testable and maintainable.
To make your app easier to change, it’s a good practice to define different layers with specific responsibilities. In particular, you’ll learn how to design and implement the domain layer for the PerSave app. You’ll also see how to access the network efficiently, by implementing the repository pattern in the data layer.
At the end of this section, you’ll have a clear idea about how to structure the code of your app.
The Android platform is more than 10 years old and the number of frameworks, libraries and tools you can use to develop your app are countless. This book covers the technologies and practices you need to follow in the real world if you want to create a successful Android app.
Building Android apps isn't just a matter of writing code. You also need to use the best architecture and structure your files in a way that makes it easy to apply changes. Keeping high cohesion and low coupling is just one of the main principles you'll learn in this chapter, where you'll start working on the PetSave app.
The domain layer is a collection of entity objects and related business logic, designed to represent the business model of any app. Here, you'll learn what a domain layer is and how to create a good one. You'll see the differences between entity and value objects along with the advantages of using the repository abstraction. Finally, you'll test the domain logic of your app.
Every useful app allows users to access information that's either stored locally or fetched from the network. This is what a data layer helps you do. In this chapter, you'll learn what the data layer is and what choices you have when you need one. You'll be introduced to the repository pattern and learn how to implement it from the design to the testing phase.
Most apps access data from the network, which introduces different kinds of problems, like latency and connectivity costs. To improve performance and reduce costs, it's a good practice to store data locally. Therefore, you need to implement the repository pattern as a cache or simple local store. In this chapter, you'll learn how to implement and test the repository pattern in the PetSave app using Room and Hilt.
PetSave allows users to find animals close to them based on certain criteria. There are several architectural design patterns you can use to create this feature. Here, you'll use ModelViewViewModel (MVVM) to implement the "Animals Near You" feature. You'll also have the opportunity to learn how to use fundamental libraries like LiveData, ViewModel and ViewBinding.
PetSave's main feature is to let you search for pets based on certain criteria. In this chapter, you'll use a top-down approach to implement the search, from defining the UI to fetching the data.
Section II: Modularizing Your App
As you add new features to your app, the code becomes more complex and difficult to maintain. For this reason, it’s important to separate your app into different libraries to make your code both maintainable and reusable. This also improves the app’s build time, which is a very important metric for your CI.
In this section, you’ll learn how to split your app into different modules. In particular, you’ll learn how to use the dynamic feature option, which optimizes the size of the code your users have to download to use your app.
Modularization is one of the most challenging problems you face with big apps. Splitting the app into different modules lets you reduce build time while creating code you can reuse in other apps. In this chapter, you'll learn how to create a feature module for PetSave, focusing on the navigation.
Mobile apps, especially games and apps with lots of graphic assets, often have large APKs. Google's dynamic feature option lets you keep your app sleek by splitting it into parts that users can load if and when they actually need them. In this chapter, you'll learn how to use dynamic features to install PetSave more efficiently.
Section III: Enhancing Your UI
The user interface (UI) is one of the most important aspects of any real world app. How your user interacts with the features of your app is what decides if the app is successful or not.
When you build your app, you have three options for creating your UI. Most of the time you’ll use what the Android SDK provides. In other cases, you customize the existing components. Your third option is to create your own custom components.
In this section, you’ll learn how to enhance the user interface of your app. You’ll create and customize animation using the new Animation Editor, which comes with recent versions of Android Studio. You’ll also learn how to master themes and styles and to create a custom view.
After reading this section, you’ll have a more appealing app and happier users.
Animations are vital to making your app both easy and pleasant to use. The Android platform provides several APIs to implement different types of animations. In this chapter, you'll learn how to use both physics-based and Lottie animation.
Implementing animation in the Android platform can be difficult and time-consuming. It's very important to use the right tool. In recent releases of Android Studio, the Motion Editor lets you implement different types of animations in a simple and declarative way. In this chapter, you'll learn how to use Motion Editor and how to integrate MotionLayout into your app.
The Android platform provides different standard components you can use to create the UI for your app. Most of the time, they're enough. Sometimes, as in the PetSave app, you need something custom. To create custom UI elements, you use the custom view, which you'll learn all about in this chapter.
Android provides styles and themes to customize the look and feel of the UI components of any app. They allow you to completely change any aspect of the UI of your app in a declarative way by providing a simple XML file. In this chapter, you'll see how to customize a UI that uses standard components and how to use styles and themes with a custom view.
Section IV: Securing Your App
Making your app more secure is an aspect of development that’s often ignored, but, at the same time, is absolutely vital. Imagine what would happen if somebody would hack your code, stealing important data or even the usernames and passwords of your users. That would be a disaster.
In this chapter, you’ll learn how to handle security from different points of view. You’ll learn how to protect user data and how to securely connect to a server. Finally, you’ll learn how to make hackers’ lives more difficult, by using different methods to protect your code and your data.
Accessing data from local storage has security implications, so Android provides different APIs and tools to avoid unwanted access to your data. Here, you'll learn how to make your app more secure by encrypting and decrypting data and by using biometrics to implement the user login.
In this chapter, you’ll learn to secure the network connections of your app by using HTTPS for network calls, trusting a connection with certificate pinning and verifying the integrity of transmitted data.
Any mobile app can be vulnerable to different types of attacks from hackers. It's common to read about hackers breaking apps and stealing important information. Because of this, avoiding code vulnerability and validating the input from the user are very important topics. In this chapter, you'll learn how to use some of the most important techniques to make your app difficult to hack.
Section V: Maintaining Your App
When you’re developing your real world app, you might think that publishing is your end goal. But really, your work isn’t over just because you’ve released your app. You still need to understand if your app is working properly and what problems your users are experiencing.
In this section, you’ll learn everything you need to know about maintaining and controlling your app after it’s published and available to users. In particular, you’ll learn how to use Firebase for logging crashes, how to enable or disable certain features and how to use A/B tests to understand what solution is the best for your users.
Finally, you’ll see how to optimize your app’s size and how to use Android Studio as a profiling tool.
After reading this section, you’ll be ready to use all the available tools for improving your app’s quality.
Firebase is a powerful tool to manage important aspects of your app. Here, you'll see how to use Crashlytics to monitor crashes and errors. Then, you'll see how to use Remote Config to control your app remotely. Finally, you'll learn how to use Test Lab to test your app on a wide range of devices.
When you build an app, it isn't enough to implement its features and run tests. You also need to spend time improving your app's quality through optimization. For example, reducing the APK's size is important to support old devices and users with slow internet connections. In this chapter, you'll learn how to optimize your app before release.
When you implement an app or try to fix a bug, you need some tools to check what's happening. For example, you experience a crash and don't know why, or you receive invalid information from the network and need to check the data. This chapter contains everything you need to know about debugging, from memory to network, from battery usage to the definition of the UI and much more.
Sometimes, your app has problems and you don't know why. To solve the problem, you need to do some deep investigation, not just into your code but also into code you imported from third-party libraries. In other cases, the problem isn't the code, but a corrupted file or database. In this chapter, you'll learn how to examine the bytecode of your app and the files it uses to find bugs and security problems.
I've been working with Java since 1995 when I a co-founded the first Italian magazine about this technology...
Who is this book for
This book is for intermediate Android developers who already know the basics of the Android platform and the Kotlin language, and who are looking to build modern and professional apps using the most important libraries. If you want to create a reactive and good-looking UI and are determined not to ignore important aspects like security, this book will help.
Concepts covered in this book
Choosing the Right Architecture
Second Edition · Android 12, Kotlin 1.6+, Android Studio Chipmunk
v2.0 · Aug 30 2022 · Ricardo Costeira, Antonio Roa-Valverde & Subhrajyoti Sen