App Development made easy with Huawei Dynamic Ability
Android App Bundle (.aab) is a new publishing format introduced by Android. A few out of many benefits of using app bundle are dynamic ability, automatic multi-APK distribution, smaller APK size and dynamic feature modules
Overview
AppGallery uses your app bundle to generate and serve optimized APKs for each user’s device configuration, so they download only the code and resources they need to run your app. For example, a user should not get x86 libs if the device architecture is armeabi. Also, users should not get other resources like strings and drawables they are not using.
Introduction
Dynamic Ability modules allow you to separate certain features and resources from the base module of your app.
HUAWEI AppGallery provides a sub-app bundle that adapts only to the user’s device type, thereby reducing network data and device storage space, with which the same service functions can be provided.
Dynamic Ability initial app download is smaller for all users. Developers can customize how and when that feature is downloaded onto devices running Android 5.0 (API level 21) or higher. It gives freedom to integrate large 3rd party libraries (< 150MB) on demand.
Dynamic Ability with split APKs
Split APKs are very similar to regular APKs. They can include compiled DEX bytecode, resources, and an Android manifest. However, the Android platform is able to treat multiple installed split APKs as a single app. The benefit of split APKs is to break up a monolithic APK into smaller, discrete packages that are installed on a user’s device as required.
Base APK: This APK contains code and resources that all other split APKs can access and provides the basic functionality for your app.
Configuration APKs: Each of these APKs includes native libraries and resources for a specific screen density, CPU architecture, or language. That is, they are downloaded and installed along with the APK they provide code and resources for.
Dynamic Feature APKs: Each of these APKs contains code and resources for a feature of your app that is not required when your app is first installed. That is, using the Dynamic ability SDK, dynamic APKs may be installed on-demand after the base APK is installed on the device to provide additional functionality to the user.
Prerequisite
1. A computer with Android Studio installed and able to access the Internet
2. Huawei phone
3. Java JDK (1.8 or later)
4. Android API (level 21 or higher)
5. Android Studio (3.2 or later)
Integration process
1. Open the build.gradle file in the root directory of your Android Studio project.
2. Add the following lines in the build.gradle file in Base App. In the file, the Android closure contains the following configuration.
3. Add the following lines in the build.gradle file in Dynamic Feature Module. In the file, the dependencies closure contains the following configuration.
4. Set Application for Base App in the project, override the attachBaseContext() method, and add the SDK startup code.
5. Add the following configuration to the activity in the feature.
6. Initialise FeatureInstallManager to manage the entire loading process in a unified manner.
7. Create an instance of FeatureInstallRequest and specify loading information. In this request, you can specify one or more feature names.
8. Register a listener for the dynamic feature loading task to monitor the task status. The status may be successful or failed.
9. Register and deregister the listener.
10. Start Dynamic Feature Module. Then, you can start Dynamic Feature Module in Base App.
App Development
We need to add Dynamic Ability module in our project.
Create module name and package name.
Sync project and add the following plugin in module’s gradle file.
Let’s see module’s manifest.xml file:
<dist:module>: This new XML element defines attributes that determine how the module is packaged and distributed as APKs.
<dist:onDemand=”true|false”>: Specifies whether the module should be available as an on demand download.
<dist:title=”@string/feature_name”>: Specifies a user-facing title for the module.
<dist:fusing include=”true|false” />: Specifies whether to include the module in multi-APKs that target devices running Android 4.4 (API level 20) and lower.
Configure your app in your Android project, override the attachBaseContext() method in the project, and call FeatureCompat.install to initialize the Dynamic Ability SDK.
Integrated SDK based classes and callbacks to achieve Dynamic Ability feature in our activity-based class.
In the activity of a dynamic feature module, call FeatureCompat.install to initialize the Dynamic Ability SDK.
Result
Let us launch our application
Tips & Tricks
1. Dynamic Ability SDK supports only Android 5.0 or later. Therefore, user devices where the Android version is earlier than 5.0 do not support the Dynamic Ability service and can only install the full APK.
2. Total length of the package name, version number, feature name, and SO file name be less than or equal to 50 characters.
Conclusion
In this article, we have learned how to integrate Dynamic Ability in our application. It will reduce the apk size and provide on-demand installation.
With the help of Dynamic Ability, we are able to treat multiple installed split APKs as a single app.
Thanks for reading this article. Be sure to like and comments on this article if you found it helpful. It means a lot to me.
References