Reader small image

You're reading from  Android Studio 4.1 Development Essentials – Java Edition

Product typeBook
Published inMay 2021
PublisherPackt
ISBN-139781801815161
Edition1st Edition
Right arrow
Author (1)
Neil Smyth
Neil Smyth
author image
Neil Smyth

Neil Smyth has over 25 years of experience in the IT industry, including roles in software development and enterprise-level UNIX and Linux system administration. In addition to a bachelor’s degree in information technology, he also holds A+, Security+, Network+, Project+, and Microsoft Certified Professional certifications and is a CIW Database Design Specialist. Neil is the co-founder and CEO of Payload Media, Inc. (a technical content publishing company), and the author of the Essentials range of programming and system administration books.
Read more about Neil Smyth

Right arrow

85. An Overview of Android Dynamic Feature Modules

As outlined in the preceding chapter, the introduction of app bundles and dynamic delivery considerably reduced the size of the application package files that need to be downloaded when a user installs an app on an Android device. Although initially intended as a way to automatically generate separate package files for each possible device configuration, another key advantage of dynamic delivery is the ability to split an app into multiple dynamic feature modules that can be installed on-demand.

In this chapter, we will begin to explore the basic concepts of dynamic feature modules in preparation for working through a detailed practical example in the next chapter.

85.1 An Overview of Dynamic Feature Modules

The primary goals of dynamic delivery are to reduce the amount of time and bandwidth it takes to install an app from the app store, while also ensuring that only the minimum storage space is occupied by the app once it is installed.

Dynamic feature modules (also referred to as on-demand modules) allow the different features that comprise an Android app to be packaged into separate modules that are only downloaded and installed onto the device when they are required by the user. An app might, for example, include news and discussion features. In this scenario the app might only install the news feature by default and separate the discussion feature into a dynamic feature module. When a user attempts to access the discussion feature, the app will download the feature module from the Google Play store and launch it. If the user never accesses the feature, the module will never be installed, thereby ensuring that only the absolute minimum...

85.2 Dynamic Feature Module Architecture

From the outset Android was designed with modularity in mind, particularly in terms of the concepts of Intents and Activities. Dynamic features bring this philosophy to a logical conclusion by allowing an app to install only what the user needs, when the user needs it. Given the flexibility and power of this capability, the implementation of dynamic feature modules is relatively simple.

In basic terms, dynamic feature modules are built using split APK files which allow multiple APK files to be brought together to form a single app.

As we learned in “Creating, Testing and Uploading an Android App Bundle”, dynamic delivery and app bundles work by generating a custom APK file that contains only the bytecode and resources necessary to run the app on a specific device configuration. In this case, the app is still installed via a single APK file, albeit one customized for the user’s device.

In contrast, dynamic feature...

85.3 Creating a Dynamic Feature Module

A dynamic feature can be added to a project either by adding an entirely new module, or by migrating an existing module. To add a new module, simply select the Android Studio File -> New -> New Module... menu option. From the resulting dialog, select either the Dynamic Feature Module or Instant Dynamic Feature Module template, depending on the type of dynamic feature you are creating:

Figure 85-2

With the appropriate dynamic feature option selected, click on the Next button and, in the configuration screen, name the module and change the minimum API setting so that it matches the API level chosen for the base module (the project will fail to build if these versions do not match).

Click the Next button once more to configure the On-Demand options:

Figure 85-3

When the Fusing option is enabled, the module will be downloaded with the base APK on devices running older versions of Android (Android 4.4 API 20 or older) that...

85.4 Converting an Existing Module for Dynamic Delivery

If an app contains an existing feature that is a good candidate for dynamic delivery it can be converted to a dynamic feature with a few basic steps. Consider, for example, the following project structure:

Figure 85-5

In this project, the app module will serve as the base module while the secondfeature module is an ideal candidate for conversion to a dynamic feature module.

To convert an existing module within your app to a dynamic feature module, begin by editing the module level build.gradle file (Gradle Scripts -> build.gradle (Module: secondfeature) in the above example) and modifying it to use the com.android.dynamic-feature instead of the com.android.application plugin, and changing the dependencies so that the module only depends on the base (app) module. For example:

apply plugin: 'com.android.dynamic-feature'

.

.

dependencies {

    implementation fileTree(dir...

85.5 Working with Dynamic Feature Modules

Once an app project has one or more dynamic feature modules added, code will need to be written to install and manage those modules. This will involve performing tasks such as checking whether a module is already installed, installing the module, tracking installation progress and deleting an installed module that is no longer required. All of these tasks are performed using the API provided by the Play Core Library and most of these API calls involve the use of a SplitInstallManager instance which can be created as follows:

private SplitInstallManager manager;

manager = SplitInstallManagerFactory.create(this);

Usually, the first step before allowing the user to launch a dynamic feature is to check that the corresponding module has already been installed. The following code, for example, obtains a list of installed modules and checks if a specific module is installed:

if (manager.getInstalledModules().contains("my_dynamic_feature...

85.6 Handling Large Dynamic Feature Modules

The Android dynamic delivery system will not permit the download of a dynamic feature module greater than 10MB in size without first requesting permission from the user. When a request is made to download a large feature module, the listener will be called and passed a REQUIRES_USER_CONFIRMATION status. It is then the responsibility of the app to display the confirmation dialog and, optionally, implement an onActivityResult() handler method to identify whether the user approved or declined the download.

private static final int REQUEST_CODE = 101;

.

.

SplitInstallStateUpdatedListener listener =

                        new SplitInstallStateUpdatedListener() {

    @Override

    public void onStateUpdate(SplitInstallSessionState state) {

 

  ...

85.7 Summary

Dynamic feature modules allow an Android app to be divided into separate features which are downloaded on-demand when the feature is needed by the user. Dynamic feature modules can also be configured to be instant features. Instant features can be run without installing the app on the device and can be accessed by users either via a Try Now button on the app store page, or via a web URL.

Dynamic feature implementation involves splitting an app into a base module and one or more dynamic feature modules. New dynamic feature modules can be created within Android Studio, and existing modules converted to dynamic feature modules by making a few project configuration changes.

Once dynamic feature modules have been added to an app, the download and management of those modules is handled by the app via the SplitInstall classes of the Play Core Library.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android Studio 4.1 Development Essentials – Java Edition
Published in: May 2021Publisher: PacktISBN-13: 9781801815161
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Author (1)

author image
Neil Smyth

Neil Smyth has over 25 years of experience in the IT industry, including roles in software development and enterprise-level UNIX and Linux system administration. In addition to a bachelor’s degree in information technology, he also holds A+, Security+, Network+, Project+, and Microsoft Certified Professional certifications and is a CIW Database Design Specialist. Neil is the co-founder and CEO of Payload Media, Inc. (a technical content publishing company), and the author of the Essentials range of programming and system administration books.
Read more about Neil Smyth