Chapters

Hide chapters

Real-World Android by Tutorials

Second Edition · Android 12 · Kotlin 1.6+ · Android Studio Chipmunk

Section I: Developing Real World Apps

Section 1: 7 chapters
Show chapters Hide chapters

9. Dynamic Features Theory
Written by Ricardo Costeira

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

In real life, you’ll probably have many more modules in your app than PetSave has at this point. You might also have some kind of analytics logic in the app, which gives you insight into how users interact with it.

Suppose you have a share animal feature module. This feature allows the user to share an animal’s information on their social networks. Through analytics, you know that this feature is seldom used. However, even users that don’t want to use the feature still have to install it, taking up precious disk space.

Android app size has a direct relationship to the number of app installs and user retention, with larger apps tending to have fewer installs and lower user retention.

Say that the share animal feature module takes up half the space of the whole app. Wouldn’t it be frustrating to see your app being frequently uninstalled due to a large feature module that almost no one uses?

The Android team is aware of this, so they came up with some mechanisms to mitigate the problem. One of these mechanisms is the app bundle publishing format. Using this publishing format can already help you reduce your app size.

For the most part, however, this chapter focuses on another mechanism: Play Feature Delivery. This mechanism takes advantage of advanced app bundle features to allow you to develop dynamic features.

This chapter is optional if you already know what app bundles and dynamic features are. On the other hand, if you don’t understand those concepts thoroughly yet, what you read here will help.

The chapter focuses on the theoretical side of dynamic features. You’ll learn about:

  • The app bundle publishing format.
  • What dynamic features are.
  • The delivery options for dynamic features.
  • Two of the most common challenges with dynamic features: dependency injection and navigation.

Android App Bundle

Before diving into dynamic features, you need to know about app bundles. An app bundle is Google’s clever app delivery format, which splits the APK into different pieces. It then delivers only the pieces the user’s device requires.

When you upload an APK to Google Play, all users receive that universal APK when they download your app. When you upload an app bundle, however, Google Play uses it to create a few different APKs, called split APKs. These split APKs are available from Android API 21 onwards. There are three different types:

  • Base APK: Google Play generates this APK from the app module of the app. It contains everything you need to configure and launch the app as well as shared code, in most cases. It’s the first APK that the user downloads and installs.

  • Configuration APKs: APKs related to different screen densities, languages, CPU architectures or native libraries. When the user downloads the app, Google Play installs only the configuration APKs related to the user’s device.

  • Dynamic feature APKs: APKs with code and resources for each dynamic feature.

Even if you don’t care about dynamic features, it’s a good idea to use app bundles. If your app is properly modularized, it’ll reduce the final app size. Besides, since August 2021, Google requires that new apps submitted to Google Play use app bundles. Moreover, apps larger than 150 MB will have to use either Play Feature Delivery or Play Asset Delivery.

Play Feature Delivery delivers dynamic features to the user via app bundle features and APIs. You’ll learn more about these later. As for Play Asset Delivery, the logic is the same as Play Feature Delivery, but it applies to game assets.

Dynamic Delivery

Google Play installs the split APKs on the user’s device and makes them appear as a single app. This is called an optimized APK. This optimized APK is built through a process called dynamic delivery. This optimizes the APK because dynamic delivery generates it using only the components that matter for the user’s specific device.

What Are Dynamic Features?

When you have a multi-module app, each feature usually has its own module. If you do it right, each feature module will — for the most part — be independent. You still need an application module to use the feature and it might depend on a few core modules, but everything that defines the feature will be in its module. This module isolation is essential to creating dynamic features.

Play Core

The first main difference is that you can specify how and when the user can access a dynamic feature. You can even define when the feature is installed, or uninstalled. This behavior is possible due to the Play Core Library.

Module Dependencies

The second main difference between regular feature modules and dynamic feature modules is the way the dependencies between base and feature modules work. In a regular multi-module app, the app module depends on feature modules:

Figure 9.1 — Typical dependency graph between base module and regular feature (library) modules.
Hewevu 6.2 — Lrqiluw jojezjathd mzudm vesvoey yagi cibino eqh doduzel veevubu (nemsolc) nufuler.

Figure 9.2 — Dependency graph between base module and dynamic feature modules.
Lekine 0.7 — Boponsoyyz fqody koghaid pewe zutina azx ntcakag voazaza nijezoz.

Injecting Dynamic Dependencies

PetSave uses Hilt for dependency injection. Hilt requires the entire dependency graph to be built at compile time. Hilt builds the dependency graph starting at the Application annotated with @HiltAndroidApp. This class is in the base module, which works out of the box for a monolithic app. For a multi-module app, it’ll work as long as the base module is aware of all the dependencies, like you’ve seen in Chapter 8, “Multi-Module Apps”.

Navigation With Dynamic Features

You’ve already learned that navigating between features in multi-module apps is a challenge. In the previous chapter, you solved the problem using the Navigation component with deep links.

Key Points

  • App bundle is a publishing format that optimizes and tailors APKs for users’ devices.
  • Play Feature Delivery uses advanced app bundle features that allow you to optimize app installations to the next level.
  • The Play Core Library provides the mechanisms for you to decide how and when you want to deliver dynamic features.
  • Navigation and dependency injection become more challenging with dynamic features.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.

Unlock now