Creating a Dynamic Delivery Module in Android with Conditional Inclusion at Install Time

54 views Asked by At

I am working on an Android project where I need to create a dynamic delivery module. However, during the module creation, I am presented with the following options:

  • Do not include module at install-time (on-demand only)

  • Include module at install time

  • Only include module at install-time for devices with specified features

My requirement is as follows: If a certain condition is true during the creation of a release build (AAB or APK), I want the module (let's call it Module A) to be present in the base APK, i.e., include the module at install time. If the condition is false, Module A should be a dynamic module, i.e., do not include the module at install time (on-demand only).

Is there any way to achieve this conditional inclusion of a module at install time based on a certain condition during the build process?

My condition is if we are building an apk (then include all module A which is dynamic module) else if we are building aab file then don't do anything since the module A is already dynamic

1

There are 1 answers

0
Tehleel Mir On

so I finally did it in the following way

Changes in the dynamic module gradle file

plugins {
    if (true) { // we are building aab
        id("com.android.dynamic-feature")
    } else { // we are building apk
        id("com.android.library")
    }
    id("org.jetbrains.kotlin.android")
}

dependencies {
    if (true) { // we are building aab
        implementation(project(":app"))
    }

    implementation("androidx.appcompat:appcompat:1.6.1")
    implementation("com.google.android.material:material:1.9.0")
    implementation("androidx.constraintlayout:constraintlayout:2.1.4")
}

changes in app gradle file

inside android tag

if (true) { //we are building aab
    dynamicFeatures = [':dynamicModule']
}

inside dependency tag

if (true) { //we are building apk
        implementation project(':dynamicModule')
    }

changes where we are using module in the main code

binding?.textView?.setOnClickListener {
            if (true) { // we had build aab
                checkForModule()
            } else { // we had build apk
                accessTheContentsOfModule()
            }
        }

condition can be anything, you want, a property or a Fastlane or as i said anything you want