Android App Development

Using native library inside dynamic feature module: Part 2

Tejas Mane
4 min readJun 22, 2021

--

In part 1, we created a native C++ project in Android Studio and converted the app module to a library module. In this part, we will learn about creating an on-demand dynamic feature module. I have explained what is dynamic feature delivery in part 1. If you haven’t gone through it, check it out here.

Creating a Dynamic Feature Module

1. Create a new Android Studio Project by selecting the Empty Activity option. I am naming it ‘Dynamic Feature App’. You can use any other name you like xD.

2. Now we will create a dynamic feature module inside this project. Select File->Project Structure->Modules. Click on the + button.

Project Structure dialog

3. Select the Dynamic Feature option from templates. Name your module accordingly. The name will be used inside the code to refer to this module. Click Next.

4. Give a title to the module. This title is visible to users. Select the delivery type. We will be going on with on-demand delivery. Learn more about it here. Click Finish. Close the Project Structure dialog.

5. Sync project with Gradle files. The dynamic feature module will be displayed in android view groups.

Android view groups for Dynamic Feature Module

6. Create new Activity inside the dynamic feature module. Name it accordingly. Add TextView to layout files and modify it so that we can distinguish between the app module and dynamic feature module. Like, I have added a TextView in the dynamic feature module with the text “Hello World from dynamic feature module!”.

7. Inside the MainActivity of the app module, we will simulate the installation of a dynamic feature module on a button click.

8. Add two buttons in activity_main.xml. One for installing the dynamic module and the other for opening an activity in the dynamic module. Similarly, add a button in activity_dynamic_feature.xml for opening activity from the native library.

activity_main.xml and activity_dynamic_feature.xml

Installing the Dynamic Feature

1. We need to add play core dependency to our project. Add below dependency to app/build.gradle in dependencies block. Replace version.playcore with recent version code. You should get the most current version of the Play Core library from here. Sync the project.

implementation "com.google.android.play:core:${versions.playcore}"

2. Create a function installModule() inside MainActivity.java and add the below code to it.

3. To open the activity from the dynamic feature module, we need to use .setClassName() method of the Intent class.

Note: If BuildConfig is not identified, build the project once and it will get resolved.

4. Run the app. Install the dynamic module and open the Dynamic Feature Activity. For local testing purposes, you can use bundletool.

You can also refer to this codelab for a detailed walk-through about adding a dynamic feature. In the next part, we will add our native library module inside this dynamic feature module. Complete source code for this project is available on GitHub.

Happy coding!

--

--