ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Dynamic Delivery with Jetpack Compose

--

Dynamic Delivery is one of my favorite tools in Android Development. With this serving model, it is possible to provide optimized APKs for your users creating smaller apps and also provide downloadable features even after your application is installed.

Before we get started, previously I wrote an article about how to create your own Dynamic Feature Module (DFM), with step-by-step explanation and code samples. Please, take a look on this article if you are not familiar with this concept.

If you want more information about Dynamic Delivery, please access the official documentation.

Quick intro

One of the best ways to handle navigation in Compose is using Jetpack Compose Navigationlibrary. It provides a nice way to handle backstack, pass arguments, testing and else. One example of code implementation a simple navigation can be seen below:

However, when porting my application, I missed one feature in the library that impacted my development: the support for Dynamic Feature Modules. As a quick recap, the dependency between :app modules and DFM modules is inverted, as is represented below:

Dependency flow between the app and dynamic feature module

Once the :app modules does not know of the existence of the Dynamic Feature Module, the NavGraph can not add the composable node in their declaration. Also the navigation library does not allow us to add mode nodes in other parts of the code.

As usual, I asked on Kotlinlang Slack for help regarding this issue. Ian Lake explained that Jetpack Navigation Compose does not have support for DFMs yet, but they are investigating for adding it in the future.

The proposed solution

After thinking about it for a while, I developed a simple solution to overcome this issue using an additional Activity and deep links. First of all, a new Activity is created inside the Dynamic Feature Module to compose the screen:

The new DynamicActivity is registered in the module’s AndroidManifest.xml adding a deep link to open it. In our example, when the deep link app://com.example.dynamic is called, it will open our Activity.

In order to test if the deep link is working, run the following adb command:

adb shell am start -W -a android.intent.action.VIEW -d “app://com.example.dynamic”

Now, in our navigation graph we create a new action to be called when the user interacts with some Composable, which will trigger the deep link when the module is installed. In order to make the code smaller, I’m using a simple assist class I created to install DFMs using Kotlin DSL. This class provides confirmation and progress dialogs for a better user experience.

In the code above, the “dynamic_feature” module is verified and when the feature is ready (meaning that it finishes the installation or it is already installed), it will simply start an Activity via deep link.

And that’s it! Now we can keep using the power of Dynamic Delivery in our Compose apps until the official support is released.

Dynamic Feature from Alkaa in action

Final thoughts

Although this approach is not the best one, specially because we are not following the recommendation to have a single Activity, it works until we have an official support. If you already have an application using DFMs, it is better to use a workaround for a period of time instead of disabling the feature for your final users.

If you want to see more real scenario code, this is the pull request for porting my Dynamic Feature Module to work with Jetpack Navigation Compose.

For following updates and showing Google your interest in an official support, please star this Issue Tracker.

Thanks a lot for reading my article and a special thanks for Ian Lake for being so helpful for our community. ❤️

Sign up to discover human stories that deepen your understanding of the world.

--

--

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Igor Escodro

Passionate Android developer | Google Developer Expert for Android

Responses (2)

Write a response