Understanding Android Dynamic Features, new META
In this modern days when everything can be solved with an app, the world have another problem that is APK size.
APK size getting bigger and bigger over time and those APK size also take part on user decision whether they want to download the app or not.
Have you ever face a problem where your app size getting bigger and in the same time you have some functionality in the app that gonna be used in rare cases? If no, let me give you some example.
You create a marketplace product and that product handle both seller and buyer functionality in the same app. We can clearly see that the app contain 2 different functionalities that not necessary needed for every user. If you are a buyer, then obviously you don’t need “seller functionality” because buyer will not sell anything. When the developer publish that app as a single module, as a result the app gonna have big apk size.
That problem can be solved with Dynamic Feature. But, what is that?
What is Dynamic Feature?
Dynamic Feature is a new feature in Android that allows the developer to publish their app with just specific module, and when the app needs another module, then the app itself will download the other module. It means you as a developer have power to publish only “the core” of your app.
Let’s say you have an App with some additional feature. With Dynamic Feature, you able to publish only your main-app to production and when the user need additional feature, then the app will download that additional feature automatically. Magic? No. It’s science…
With Dynamic Feature approach, you will make the apk size smaller because user will only get the main-app (base apk) and user don’t need to download unused code/feature to their device.
Creating Dynamic Feature
Requirement:
- Android Studio 4.+ , you can get it here
- Nothing, let’s start
Objective:
We create a project that have dynamic feature. That project will have 2 main pages and 3 additional pages that act as onboarding page (this is an example of onboarding page). My idea is to create a Pokedex (Pokemon Guide) application with onboarding page on that project
Hands-on:
Dynamic feature is the same like regular app modules. That module have their own class, assets, resources.
1. Create an empty project
2. Create a new module. File
> New
> New Module
. Chose Dynamic Feature Module
then click Next
3. Let’s name that module onboarding
then click next
4. Configure download options.
- Choose
Do not include module at install-time
because we want this module to be installed only when the user need. - Enable
Fusing
. Dynamic feature is not supported on pre-lollipop devices. In result, “Onboarding” module that act as Dynamic Features will be installed with the base APK.
5. Then click Finish
. After that we can see that we have 2 different modules. app
and onboarding
5. Then click Finish
. After that we can see that we have 2 different modules. app
and onboarding
6. After new module have been created, we must configure relation between base module and dynamic feature module (onboarding module)
- Open build.gradle in app module, and you should add onboarding module as a dynamic feature inside android tag. Usually Android Studio already add this manually, but you still need to double-check.
- Open build.gradle in onboarding module, and you should implement app module to that new module.
7. In this project, i will use Navigation Architecture Component. I have create two pages in base-module and one page in feature-module called OnboardingFragment. I put all of my fragment to one navigation graph file.
8. In our project, we will show onboarding page when the user open a certain page. So we can download the additional module after the page have shown up. To download feature-module, we use DynamicInstallMonitor()
and that object will download the page that we need.
As you can see on line 7 above, we want to navigate to onboardingFragment. If onboardingFragment haven’t get downloaded in the device, then DynamicInstallMonitor
will install it for us.
How do we know if our program failed, success, or still loading additional feature? We can observe DynamicInstallMonitor!
And that’s all. When we open our PokemonDetailFragment
, the user will be navigated to onboarding page first to get introduced on the things in Pokemon Detail page.
Deployment and Testing:
In order to use Dynamic Feature, you must use AAB insted of APK. Why? Because bundle able to break down your code for optimization purpose, so that it have capabilities to send the user only necessary resources (it include sending additional feature only when user need it). For example, if the user device have small resolution, then the device will only get small resolution image resource.
We should upload our AAB to playstore in-order to feel the dynamic feature. Because if we install our app directly from PC to Android Device, then Android Studio will install every modules within one installation.
But if you don’t want to upload your AAB to playstore or you don’t have any playstore account, you can see in my project below, there is a script to install your app from PC to Android Device by splitting the modules. As result, you will install only the base-apk then it will download feature-module when needed (just the same as uploading a to playstore). So you can test your dynamic features locally.
gradlew installApkSplitsForTestDebug