Build types and flavours in multi module android application

1.2k Views Asked by At

I have a multi module android application. I have core modules (network, data, database etc), data modules and feature modules. How should I handle the build types in the different build.gradle.kts files? If i don't copy paste buildTypes to all module's gradle file the app won't build. How can I inherit or something? What is the best practice to it? Thank you!

1

There are 1 best solutions below

2
Nojipiz On

In Kotlin DSL isn't as simplest as Gradle, because all it's typed so that "magic trick" with another Gradle script doesn't work here.

The solution is create a Gradle plugin and apply it to all of your modules. First create a buildSrc folder in the root of your project, then in the root of buildSrc create build.gradle.kt with this content:

plugins {
    `kotlin-dsl`
}

repositories {
    google()
    mavenCentral()
}

dependencies {
    implementation("com.android.tools.build:gradle-api:<your-gradle-version>")
}

gradlePlugin {
    plugins {
        register("base-configuration-plugin") { // Your custom plugin name
            id = "base-configuration-plugin" // Your custom plugin name
            implementationClass = "BaseConfigurationPlugin" // The class where your plugin is located.
        }
    }
}

After that, you will need to create your custom plugin implementation, in buildSrc/main/java/BaseConfigurationPlugin.kt

class BaseConfigurationPlugin : Plugin<Project> {
    override fun apply(target: Project) {
        val extension = target.extensions.getByName("android")
        if (extension is ApplicationExtension) {
            extension.setBuildFlavors()
        }
        if(extension is LibraryExtension){
            extension.setBuildFlavors()
        }
    }
}

Of course, "setBuildFlavors" doesn't exist, is just an extension method created to make it look more "kotlin style" here is the method.

private fun ApplicationExtension.setBuildFlavors() {
    flavorDimensions("version", "other_dimension")
    productFlavors {
        create("dev") {
            dimension = "version"
            applicationIdSuffix = ""
            versionNameSuffix = ""
        }
        create("production") {
            dimension = "version"
            applicationIdSuffix = ""
            versionNameSuffix = ""
        }
        create("pro") {
            dimension = "other_dimension"
            applicationIdSuffix = ""
            versionNameSuffix = ""
        }
        create("noob") {
            dimension = "other_dimension"
            applicationIdSuffix = ""
            versionNameSuffix = ""
        }
    }
}

// Same but for libraries
private fun LibraryExtension.setBuildFlavors(){
    flavorDimensions("version", "other_dimension")
    productFlavors {
        create("dev") { dimension = "version" }
        create("production") { dimension = "version" }
        create("pro") { dimension = "other_dimension" }
        create("noob") { dimension = "other_dimension" }
    }
}

And you are ready to apply the plugin!! Just put in all of your module's build.gradle.kts and it should works like a charm!

plugins {
    `base-configuration-plugin`
}

Here is an article where you can get more info. :)