Configure at-install delivery

Dynamic feature modules allow you to separate certain features and resourcesfrom the base module of your app and include them in your app bundle. ThroughDynamic Delivery, you can then customize deliveryoptions to control whenand how devices running Android 5.0 (API level 21) or higher download your app'sfeatures.

Keep in mind, this type of modularization requires more effort and possiblyrefactoring your app’s existing code, so consider carefully which of yourapp’s features would benefit the most from being available to users on demand.

If you want to gradually modularize app features over time, withoutchanging the behavior of your app or customizing advanced delivery options, youcan do so by creating dynamic feature modules that are configured for at-installdelivery. That is, you can modularize a feature as a dynamic feature, but notenable advanced options so the feature is available when a user installs yourapp.

Additionally, it might be useful to create dynamic features that you configurefor at-install delivery because you have the option to later uninstall thatfeature if it's no longer required. For example, to reduce the installed size ofyour app, you can modularize content that's required for training or onboarding,and thenuninstall the dynamic feature moduleafter the user is set up to use your app.

This section describes how to create a dynamic feature module for at-installdelivery. Before you begin, make sure you'reusing Android Studio 3.3 or higher and Android Gradle Plugin 3.3.0or higher.

Configure a new module for at-install delivery

The easiest way to create a new dynamic feature module is by usingAndroid Studio 3.3 or higher.Because dynamic feature modules have aninherent dependency on the base app module, you can add them only to existingapp projects.

To add a dynamic feature module to your app project using Android Studio,proceed as follows:

  • If you haven’t already done so, open your app project in the IDE.
  • Select File > New > New Module from the menu bar.
  • In the Create New Module dialog, selectDynamic Feature Module and click Next.
  • In the Configure your new module section, complete thefollowing:
    • Select the Base application module for your app project fromthe dropdown menu.
    • Specify a Module name. The IDE uses this name to identify themodule as a Gradle subproject in yourGradle settings file. When youbuild your app bundle, Gradle uses the last element of the subprojectname to inject the <manifest split> attribute in thedynamic feature module’s manifest.
    • Specify the module’s package name. By default, Android Studiosuggests a package name that combines the root package name of thebase module and the module name you specified in the previous step.
    • Select the Minimum API level you want the module to support.This value should match that of the base module.
  • Click Next.
  • In the Configure On-Demand Options section, uncheck the box next toEnable on-demand. If you want to learn how to create a dynamic featurethat you can download after app install, readconfigure on-demand delivery.
  • Click Finish.
    After Android Studio finishes creating your module, inspect its contentsyourself from the Project pane (select View > Tool Windows > Projectfrom the menu bar). The default code, resources, and organization should besimilar to those of the standard app module.

Dynamic feature module build configuration

When you create a new dynamic feature module using Android Studio, the IDEapplies the following Gradle plugin to the module’s build.gradle file.

  1. // The following applies the dynamic-feature plugin to your dynamic feature module.
  2. // The plugin includes the Gradle tasks and properties required to configure and build
  3. // an app bundle that includes your dynamic feature module.
  4. apply plugin: 'com.android.dynamic-feature'

Many of the properties available tothe standard application pluginare also available to your dynamic feature module. The following sectionsdescribe the properties you should and shouldn’t include in your dynamicfeature module’s build configuration.

What not to include in the dynamic feature module build configuration

Because each dynamic feature module depends on the base module, it alsoinherits certain configurations. So, you should omit the following in thedynamic feature module’s build.gradle file:

  • Signing configurations: App bundles are signed using signingconfigurations that you specify in the base module.
  • The minifyEnabled property: You canenable code shrinkingfor your entire app project from only the base module’s buildconfiguration. So, you should omit this property from dynamicfeature modules. You can, however,specify additional ProGuard rulesfor each dynamic feature module.
  • versionCode and versionName: When building your app bundle,Gradle uses app version information that the base module provides.You should omit these properties from your dynamic module’sbuild.gradle file.

Establish a relationship to the base module

When Android Studio creates your dynamic feature module, it makes it visibleto the base module by adding the android.dynamicFeatures property to thebase module’s build.gradle file, as shown below:

  1. // In the base module’s build.gradle file.
  2. android {
  3. ...
  4. // Specifies dynamic feature modules that have a dependency on
  5. // this base module.
  6. dynamicFeatures = [":dynamic_feature", ":dynamic_feature2"]
  7. }

Additionally, Android Studio includes the base module as a dependency of thedynamic feature module, as shown below:

  1. // In the dynamic feature module’s build.gradle file:
  2. ...
  3. dependencies {
  4. ...
  5. // Declares a dependency on the base module, ':app'.
  6. implementation project(':app')
  7. }

Specify additional ProGuard rules

Although only the base module’s build configuration may enable code shrinkingfor your app project, you can provide custom ProGuard rules with each dynamicfeature module using theproguardFilesproperty, as shown below.

  1. android.buildTypes {
  2. release {
  3. // You must use the following property to specify additional ProGuard
  4. // rules for dynamic feature modules.
  5. proguardFiles 'proguard-rules-dynamic-features.pro'
  6. }
  7. }

Note that these ProGuard rules are merged with those from other modules(including the base module) at build time. So, while each dynamic featuremodule may specify a new set of rules, those rules apply to all modules in theapp project.

Deploy your app

While you're developing your app with support for Dynamic Delivery, you candeploy your app to a connected device like you normally would by selectingRun > Run from the menu bar (or by clicking RunConfigure at-install delivery - 图1 inthe toolbar).

If your app project includes one or more dynamic feature modules, you canchoose which dynamic features to include when deploying your app by modifyingyour existing run/debug configuration asfollows:

  • Select Run > Edit Configurations from the menu bar.
  • From the left panel of the Run/Debug Configurations dialog, selectyour desired Android App configuration.
  • Under Dynamic features to deploy in the General tab, check thebox next to each dynamic feature module you want to include whendeploying your app.
  • Click OK.
    By default, Android Studio doesn't deploy your app using app bundles to deployyour app. Instead, the IDEbuilds and installs APKs to your device that are optimized for deployment speed,rather than APK size. To configure Android Studio to instead build and deployAPKs and instant experiences from an app bundle, modify your run/debugconfiguration.


上一页


下一页
Configure on demand delivery