How to use Android App Bundle?
I think you have heard the news that now google is supporting the Android app bundle for the release on google play. So what is the app bundle? should you use it in your app? Let’s try to answer these questions in this blog post.
APK vs App Bundle
Until now we have used APK to distribute our apps. APK is just a package of all our classes, library and resources files. But there are some downside to using APK.
- APK bundles everything together so usually, the app with many features are huge. If your APK size is bigger, then it significantly reduces the number of downloads.
- Even though users don’t need all features at installation time, the APK ships everything together. For example, in an e-commerce app, we have a feature related to seller. It will take more storage on the user device and also confuse them.
To solve these problems, google introduced Android app bundle. It solves these problems by introducing dynamic feature delivery and modularising the app.
How does app bundle work?
Android app bundle is nothing but a set of multiple apk combined into one. When we create an android app bundle, under the hood android create multiple apk for the different module and serve them as needed by the user. Every app bundle always have one apk called base apk and it contains all the code that will be served to the user when downloaded for the first time.
We can conclude the following things from the image above.
- App bundle use .aab extension.
- It has one base module which contains all the necessary features that are shipped to the user.
- It can also have asset pack which is very useful for the big games which contain a lot of graphics and asset which might not be important when downloaded but can prove useful when user reach to some specific level.
- With a distinctive character, the user can download dynamic feature module on demand.
How to get started with android app bundle?
The simplest way to get started with android app bundle using the android studio is
- Open android studio
- Click build/build bundle
- Then use your normal keystore file to sign and create app bundle.
How to build a dynamic feature module?
To add a new module follow these steps
- Open your project in android studio
- SelectFile > New > New Modulefrom the menu bar.
- In theCreate New Moduledialog, selectDynamic Feature Moduleand clickNext.
- Now add all the necessary information related to the model like target version(should be same as a base), language and package name.
- Provide the title (you can change it later in string.xml file) and installation preference of the module. There are mainly 3 options.
- Include module at install time.
- Do not include module at install time (download it on demand only)
- Only include it at install time if the device has some specific feature for example if the device has AR CORE then only include this feature at install time otherwise not.
6. Check new module called “adminfeature” is created on the left navigation panel. If you open the manifest file of this module, you will notice the following things.
<manifest xmlns:android="<http://schemas.android.com/apk/res/android>" xmlns:dist="<http://schemas.android.com/apk/distribution>" package="com.example.adminfeature"> <dist:module dist:instant="false" dist:title="@string/title_adminfeature"> <dist:delivery> <dist:on-demand /> </dist:delivery> <dist:fusing dist:include="true" /> </dist:module></manifest>
Let’s break down all the things that this file is telling us.
- dist:module tag is used to represent the module.
- dist:instant=”false” It shows that our module is not an instant module (without installing it in the phone, a user can use instant module).
- <dist:on-demand /> This code shows that our module is on demand. Android will not install it on app-install time.
- <dist:fusing dist:include=”true” /> Fusing is just a way to tell android that it will include this module in the apk for pre lollipop device because pre lollipop device does not support dynamic delivery.
How to install dynamic feature at runtime?
For installing the dynamic feature, use play core library. It accesses the google play store.
- Include this dependency in your app-levelz Gradle file. Also, add google() in you project repositories.
// This dependency is downloaded from Google’s Maven repository.// So, make sure you also include that repository in your project's build.gradle file.implementation 'com.google.android.play:core:1.7.3'
- Now to install the module at any point in you app use the SplitInstallManager instance.
// Creates an instance of SplitInstallManager.SplitInstallManager splitInstallManager = SplitInstallManagerFactory.create(context);
- Create a request.
// Creates a request to install a module.SplitInstallRequest request = SplitInstallRequest .newBuilder() // You can download multiple on demand modules per // request by invoking the addModule method .addModule("pictureMessages") .addModule("promotionalFilters") .build();
- Submit a request and add success and failure listener.
splitInstallManager .startInstall(request) .addOnSuccessListener(sessionId -> { ... }) .addOnFailureListener(exception -> { ... });
- We can also download some that are not immediately required when app is in the background.
// You can specify more than one module at a time.splitInstallManager.deferredInstall(Arrays.asList("promotionalFilters"));
How to test android app bundle?
There are mainly two ways to test your android app bundle. You can either change your run configuration in an android studio and use that or you can upload it on google play console internal track and then test it.
Deploy using app bundles with Android Studio
- SelectRun > Edit Configurationsfrom the menu bar.
- Select a run/debug configuration from the left pane.
- Select Generaltab then SelectAPK from app bundleoption from dropdown menu next toDeploy.
- If your app includes Dynamic Feature modules, you can select which modules you want to deploy.
- Click Apply or OK.
Or you can upload your app bundle to internal track and your tester can go to the link which will contain bundle for testing and google play will serve the app bundle accordingly.
Example application
I have created this exampl
e application for the reference. It has two button
- Download app module. ( If you test it locally and do not include the module it will give error if you include, it will give success message.)
- Open app module – It will open activity from the app module if the module does not exist it will show you the message.
You can check out this application repository on Github.
Conclusion
In this blog post, we have learned about the android app bundle. We have learned how it works, how to build and install it in runtime, and also how we can test it. I hope you have found this article helpful if you have any query or suggestion for me,
let me know in the comment section below.
Thanks !! Have a nice day.