Android App Development

Using native library inside dynamic feature module: Part 1

App size significantly affects the app install rate. Larger apps tend to have lower install rates. Developers often include features that are not going to be used by the majority of users. This leads to larger app sizes which could have been reduced. Dynamic features play a major role here. Play Feature Delivery uses advanced capabilities of app bundles, allowing certain features of your app to be delivered conditionally or downloaded on demand.

Now we understood how important is dynamic features for your app. Can we use native code inside this dynamic feature? Sometimes, only Java or Kotlin code is not enough. We have to include some native C++ code as well for desired functionality. While it won’t benefit most apps, it could prove useful for CPU-intensive applications such as game engines. C++ software is easily reusable and coding in C++ is faster and efficient compared to Java. In this series, we are going to learn about adding these C++ functionalities in a dynamic feature using Play Feature Delivery.

To get a clear idea of what we are going to do, have a look at the below image. We will create a new project which contains an app module and add a dynamic feature module. Both of these modules contain activities and their respective layout files. Then we are going to use a native-built library inside the dynamic feature module.

Creating a Native C++ project

1. Open Android Studio and create a new Native C++ project and click Next

2. Add project name and other necessary information. Click Next.

3. If you want to select another standard you may do so. I am going to use default settings here. Selecting Toolchain Default uses the default CMake setting. Click Finish.

4. After Android Studio finishes creating your new project, open the Project pane from the left side of the IDE and select the Android view. As shown in figure 2, Android Studio adds the cpp group. Make sure you have these files in your project structure.

Setting up Android NDK

5. To build and debug the native code, we need Android NDK installed in the system. Follow the below steps to set up the NDK:

i. Select File->Settings->Appearance & Behavior->System Settings->Android SDK.

ii. Select SDK Tools tab and check NDK (Side by side) and CMake options.

iii. Click Apply and select OK in the Confirm Change dialog box. Wait until the installation is completed.

iv. Configure NDK for the project. Select File->Project Structure->Modules. Select the NDK version from the dropdown in the Properties tab.Click Apply and OK.

v. NDK is installed successfully.

6. Now Run the app. Android studio will launch the app displaying “Hello world from C++” on your emulator or device.

Follow this link to know more about Adding native code to android.

Converting app module to a library module

In order to use this project in other projects, we have to convert it to a library module. Before converting it to a library, we will rename some files to avoid future conflicts.

1. Rename MainActivity.java to some unique name like NativeMainActivity.java.

2. Rename activity_main.xml to activity_main_native.xml

3. Open AndroidManifest.xml and remove android:icon, android:roundIcon and android:theme tags from application tag. Modified manifest file will contain the following code:

4. Now, we will remove unnecessary resources to avoid future conflicts. Delete all file res->mipmap folders. Then delete files in res->drawable.

5. Now, we can convert our app to a library module. Modify the build.gradle (app level) file, as below.

6. Remove applicationId, versionCode, versionName entries from build.gradle (app level).

7. (Optional but recommended) Disable viewBinding or dataBinding feature if enabled by default.

Modified build.gradle (app level) will contain the following code:

Know more about Converting an app module to a library module here.

6. We will need .aar file to include this library module in other projects. Generate the .aar file by selecting Build->Make module ‘NativeC++App.app’. .aar file will be generated in RootFolder->app->build->outputs->aar with name app-debug.aar.

In the following parts, we will create an on-demand dynamic feature module and use this library inside the dynamic feature module. Complete source code for this project is available on GitHub.

Happy coding!

Android developer