preface

Starting in August 2021, Google Play will begin requiring new apps to be distributed using the Android App Bundle. The format will replace APK as the standard distribution format. Although this policy does not affect domestic applications at present, as Android developers, we should have a certain understanding of the new trend. This article is not an introduction to The Android App Bundle, because it is not new and there are many articles about it. This article focuses on the practical application of the “dynamic functional modules” in the Android App Bundle.

Android App Bundle

Android App Bundle is a distribution technology based on Google Play that can effectively reduce the size of APK.

The Android App Bundle can automatically package resources for different resolutions, languages, and platforms (CPU), so that only the resources corresponding to the current device can be installed during installation, which greatly saves memory space.

In addition to these resources, the Android App Bundle also supports Dynamic Feature modules, which are not installed if the user does not need them, thus optimizing download and installation even more.

Create dynamic function modules

Here’s how to create dynamic functional modules:

1. From the menu bar, choose File > New > New Module. This step is the same as creating a normal module.

2. In the Create New Module dialog box, select Dynamic Feature Module and click Next.

3. In the Configure Your New Module section, do the following:

  • Select the Base Application Module for your application project from the drop-down menu. (Usually the main module)

  • Specify Module name. The IDE uses this name to identify the module as a Gradle subproject in the Gradle Settings file. When you build your App bundle, Gradle uses the last element of the subproject name to inject properties into the list of feature modules.

  • Specify the package name for the module. By default, Android Studio suggests a package name that is a combination of the base module’s root directory package name and the module name you specified in the previous step.

  • Select the Minimum API level that you want the module to support. This value should be consistent with that of the base module.

  • Click Next.

4. In the Module Download Options section, complete the following operations:

1) Specify a Module title with a maximum of 50 characters. (This will be shown to the user)

2) From the drop-down menu under install-time inclusion, select distribution timing as follows:

  • Include Module at install-time Distribution (similar to regular modules)

  • Do not include module at install-time

  • Only include Module at app install for devices with Specified features distributed conditionally

    Conditional distribution also requires configuring conditions, as shown in the following figure

Click “+ device-feature” to add a condition (you can add multiple conditions). Next to each device-feature, select one of the following options from the drop-down menu and then specify a value for it:

  • Name: Specifies the hardware or software functions required by the device to download modules during application installation. Conditional distribution supports the same functionality that PackageManager lists as FEATURE_* constants. If you choose this option, enter any part of the constant value for the corresponding function (for example, “Bluetooth”) in the field next to the drop-down menu, and then select one of the suggestions displayed.

  • OpenGL ES Version: Specifies the OpenGL ES Version required for downloading modules during application installation. If you select this option, enter the corresponding version (for example, “0x00030001”) in the field next to the drop-down menu, and then select one of the suggestions displayed.

3) If you want this module to be available on devices running Android 4.4 (API level 20) and lower and included in multiple APK, check the box next to Fusing. This means that you can omit the split APK from devices that do not support downloading and installing it.

(This part of the configuration will eventually be reflected in the Manifest under the module, as described below)

5. Click Finish to create the module

Dynamic functional module

Create the following module directory:

You can see that it is the same as a normal module, but the Manifest contents are different as follows:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:dist="http://schemas.android.com/apk/distribution"
    package="com.huichongzi.dynamicfeature">

    <dist:module
        dist:instant="false"
        dist:title="@string/title_dynamicfeature">
        <dist:delivery>
            <dist:on-demand />
        </dist:delivery>
        <dist:fusing dist:include="true" />
    </dist:module>
</manifest>
Copy the code

You can see that there is a DIST TAB, under which is the configuration of the dynamic function module. Dist: Instant =”false”, for example, does not support free installation (more on free installation later), and title is the Module title set in step 4 above. while

<dist:delivery>
    <dist:on-demand />
</dist:delivery>
Copy the code

On-demand Do not include module at install-time Install-time Include module at install-time Include module at install-time

 <dist:delivery>
      <dist:install-time>
          <dist:conditions>
              <dist:device-feature dist:name="android.hardware.camera.ar"/>.</dist:conditions>
      </dist:install-time>
  </dist:delivery>
Copy the code

This is where you can add new distribution conditions after the module is created.

Conditions can also be specified by country/region

<dist:conditions>
   <! -- Set to "true" to specify countries to exclude from downloading this module at app install-time. By default, modules are available for download to all user countries. -->
  <dist:user-countries dist:exclude="true">
    <! -- Specifies the two-letter CLDR country code for regions that should not download the module at app install-time. -->
    <dist:country dist:code="CN"/>
    <dist:country dist:code="HK"/>
  </dist:user-countries>
</dist:conditions>
Copy the code

Specify conditions based on the API level

<dist:conditions>
    <! -- Specifies the minimum API level that the device must satisfy in order to download your module at app install-time. The API level you specify must be greater or equal to the module's own minSdkVersion. -->
   <dist:min-sdk dist:value="21"/>
    <! -- Specifies the maximum API level that the device cannot exceed in order to download your module at app install-time. The API level you specify must be less than or equal to the module's own maxSdkVersion. -->
   <dist:max-sdk dist:value="24"/>
</dist:conditions>
Copy the code

Establish associations with the main module

When Android Studio creates a feature module, it automatically adds the Android.dynamicFeatures property to the build.gradle file of the base module to make the feature module visible to the base module, as shown below:

// In the base module’s build.gradle file.android { defaultConfig { ... }...// Specifies feature modules that have a dependency on
    // this base module.
    dynamicFeatures = [":dynamic_feature".":dynamic_feature2"]}Copy the code

In addition, Android Studio adds base modules as feature module dependencies, as shown below:

// In the feature module’s build.gradle file:.dependencies{...// Declares a dependency on the base module, ':app'.
    implementation project(':app')}Copy the code

The deployment of application

When developing applications that support functional modules, you can deploy the application to connected devices by selecting Run > Run from the menu bar (or clicking the Run icon in the toolbar) as usual.

If your application project contains one or more functional modules, you can modify the existing run/debug configuration to select the functionality you want to include when deploying the application, as follows:

  • Choose Run > Edit Configurations from the menu bar in turn.

  • In the left pane of the Run/Debug Configurations dialog box, select the Desired Android App configuration.

  • Under Dynamic Features to Deploy on the General TAB, select the check box next to each function module that you want to include when deploying the application.

  • Click OK.

By default, Android Studio does not deploy your application using app Bundles. Instead, the IDE builds an APK optimized for deployment speed (not APK size) and installs it on the device. To configure Android Studio to build and deploy APK via app Bundle and install free experience, modify the Run/debug configuration.

Distribute on-demand

There are three classifications mentioned above: install-time distribution, on-demand distribution, and conditional distribution.

Distribution at install is easier to understand, i.e. the module will be downloaded when the app is downloaded and installed on Google Play; Conditional distribution is also easier to understand. When downloading and installing an application, the module will be downloaded only if the current device meets the conditions. Distribution only on demand is not easy to understand.

On-demand distribution is similar to the plug-in of many domestic applications, that is, the part of the code and resources will be downloaded only when the function is used. Here’s the official description:

If only 20% of people using a shopping app post an item for sale, a good strategy to reduce the initial download size for most users would be to configure the ability to take a picture, enter a description, and list an item as an on-demand download. That is, you can configure feature modules for the app’s sales function so that it is downloaded only when the user wants to sell on the shopping platform. In addition, if the user stops selling items after a period of time, the application can reduce its install size by requesting uninstallation of this feature.

Developer. The android. Google. Cn/guide/app – b…

But by creating the process of deploying dynamic modules above, we found that there is no logic for downloading on demand. This is because if you want to use dynamic modules, you need to work with Google’s Play Core Library:

implementation "com.google.android.play:core:${versions.playcore}"
Copy the code

The SplitInstallManager class in the library is used to determine whether the module is installed, and if not, to download and install the logic.

This part of the content are many, is limited by article is not opened, the official has a very detailed tutorial: developer.android.com/codelabs/on… Interested students can read on their own (ladder required). I will publish a Separate Chinese tutorial when I have time in the future, please pay attention to my official account update.

Installation-free distribution

There is also a dynamic functional module, which is distributed without installation. The Google Play Install Free experience lets users interact with your app without having to install APK on their device. Users can experience your app through the “Play Now” button in the Google Play Store or through the url you create. This form of content distribution makes it easier for you to increase user engagement with your application.

You can enable installation-free functionality only if the application’s base modules also support installation-free experience. This is because if a user wants to experience a feature module in your application that supports an install-free experience, their device must also download the application’s base module for common code and resources. Please note that in order to support the Google Play install free experience, the download of basic modules and features must meet several conditions:

Maximum size: The total size of basic modules and functional modules that support installation-free experience cannot exceed 10 MB. For more details, read support for install-free experiences by reducing the size of your app or game.

Background Activities: Modules that support installation-free experience cannot use background services. In addition, such modules cannot send notifications when running in the background.

If you use Android Studio 3.5 or later to create feature modules that support the installation-free experience (as described in this section), the IDE automatically enables the installation-free experience for both base and feature modules by adding the following to the list of each module:

<manifest xmlns:dist="http://schemas.android.com/apk/distribution". > <dist:module dist:instant="true" />
    ...
Copy the code

In addition, when downloading and installing your application, the system automatically downloads and installs the functional modules that support the installation-free experience along with the application’s basic APK. Therefore, the IDE also adds the following to the functional modules that support the installation-free experience.

<dist:module ... > <dist:delivery> <dist:install-time /> </dist:delivery> </dist:module>Copy the code

This behavior means that when you set dist:instant=”true”, you cannot also include
. However, you can use the Play Core library to request modules that support the installation-free experience in the Installation-free experience as needed.

Create installation-free modules

This is the same process as creating a dynamic module, with a few differences:

1. Select Instant Dynamic Feature Module when creating module type

Configure your New Module. Configure your New Module. Configure your New Module. Configure your New Module and finish.

The deployment of application

Deployment is the same as other dynamic functional modules.

conclusion

Android App Bundle can effectively reduce the size of the installation package, and the Dynamic Feature Module can greatly reduce the size of the installation package, so that users can greatly reduce the waiting time for download and installation, and users can experience the application faster. Improve the user experience. However, dynamic functional modules (especially on-demand distribution) must be well designed, otherwise users will have to download modules frequently during the process of use, which will degrade the user experience.

Pay attention to the public number: BennuCTech, get more dry goods!