In my last post, I discussed the basic understanding of Android on-demand feature module delivery and its project setup. Now, in this post, we will implement the core functionalities of the dynamic module.
3 parts of this blog series
- Part 1: Understanding the problem, solution approach & project setup
- Part 2: Implementation of Dynamic Feature Module (you are here)
- Part 3: Testing & troubleshooting of Dynamic Feature Module
Create dynamic module Activity
My dynamic module is named ‘customer_support’ module. However, it’s not my objective to develop a real customer support chat or audio/video call feature in this sample project. For the sake of simplicity, I have included six large images (~18 MB) in CustomerSupportActivity.kt. Using these large images, we will clearly understand the effect of the dynamic module.

MainActivity.kt (app module)
In my app module, I have only one activity — MainActivity.kt. I simply added a banner image of the eCommerce app and then included a Customer Support button. When a user taps on the Customer Support button for the first time, they will see an alert dialog to confirm downloading the dynamic module. Upon clicking the Download button, MainActivity.kt will start downloading the customer_support module from the Google Play Store. After downloading and installing, MainActivity.ktwill receive a few callbacks. Based on those callbacks, the activity will redirect the user to CustomerSupportActivity.kt.
Add a simple button in MainActivity to open the dynamic module Activity. After clicking the button, we will check if the dynamic module is already downloaded. If yes, then open the dynamic module Activity. Otherwise, we will request to download the dynamic module using our utility class.
When our utility class tries to download and completes the process, successfully installing the new module, our app module’s MainActivity will be notified by a callback.
class MainActivity : ComponentActivity(), DynamicModuleListener {
private val CUSTOMER_SUPPORT_DYNAMIC_MODULE = "customer_support"
private lateinit var dynamicModuleDownloadUtil: DynamicModuleDownloadUtil
private var logState = mutableStateOf("Activity Log:\n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
dynamicModuleDownloadUtil = DynamicModuleDownloadUtil(baseContext, this)
// ui codes
}
override fun onDownloading() {
logState.value += "${getCurrentTimestamp()}: Downloading...\n"
}
override fun onDownloadCompleted() {
logState.value += "${getCurrentTimestamp()}: Module download completed.\n"
}
override fun onInstallSuccess() {
logState.value += "${getCurrentTimestamp()}: Module install Success!\n"
startCustomerSupportActivity()
}
override fun onFailed() {
logState.value += "${getCurrentTimestamp()}: Module download or installation failed.\n"
}
private fun openCustomerSupportFeature() {
if (dynamicModuleDownloadUtil.isModuleDownloaded(CUSTOMER_SUPPORT_DYNAMIC_MODULE)) {
logState.value += "${getCurrentTimestamp()}: Module is already downloaded.\n"
startCustomerSupportActivity()
} else {
dialogState.value = true
}
}
private fun startCustomerSupportActivity() {
val intent = Intent()
intent.setClassName(
"com.hellohasan.hasanerrafkhata",
"com.hellohasan.customer_support.CustomerSupportActivity"
)
startActivity(intent)
}
}
DynamicModuleDownloadUtil.kt
You can simply copy and paste this Util class in your project.


2 thoughts on “Mastering Android Dynamic Feature Delivery (2/3)”