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.

  1. 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.
  2. 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.

https://developer.android.com/images/app-bundle/aab_format-2x.png?dcb_=0.4574495683276687

We can conclude the following things from the image above.

  1. App bundle use .aab extension.
  2. It has one base module which contains all the necessary features that are shipped to the user.
  3. 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.
  4. 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

  1. Open android studio
  2. Click build/build bundle
  3. 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

  1. Open your project in android studio

  2. Select File > New > New Module from the menu bar.

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

  4. Now add all the necessary information related to the model like target version(should be same as a base), language and package name.

  1. Provide the title (you can change it later in string.xml file) and installation preference of the module. There are mainly 3 options.

    1. Include module at install time.

    2. Do not include module at install time (download it on demand only)

    3. 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.

  1. dist:module tag is used to represent the module.
  2. 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).
  3. <dist:on-demand /> This code shows that our module is on demand. Android will not install it on app-install time.
  4. <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.

  1. 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'
  1. 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);
  1. 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();
  1. Submit a request and add success and failure listener.
splitInstallManager
    .startInstall(request)
    .addOnSuccessListener(sessionId -> { ... })
    .addOnFailureListener(exception -> { ... });
  1. 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

  1. Select Run > Edit Configurations from the menu bar.
  2. Select a run/debug configuration from the left pane.
  3. Select General tab then Select APK from app bundle option from dropdown menu next to Deploy.
  4. If your app includes Dynamic Feature modules, you can select which modules you want to deploy.
  5. 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 example application for the reference. It has two button

  1. 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.)

  2. 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.