Search code examples
gradlegradle-kotlin-dsl

Migrate nested extra properties from .gradle to .gradle.kts (DSL)


Creating example:

ext {
    versions = [
            kotlin     : "1.3.72",

            application: [
                    versionName     : getVersionName(),
                    versionCode     : getVersionCode(),
                    imageVersionCode: getImageVersionCode(),
            ],
            android    : [
                    compileSdk: 29,
                    minSdk    : 21,
                    targetSdk : 29,
                    java      : JavaVersion.VERSION_1_8,
            ],
    ]
}

Using example

android {
    compileSdkVersion versions.android.compileSdk
    defaultConfig {
        minSdkVersion versions.android.minSdk
        targetSdkVersion versions.android.targetSdk
        ...
    }
    ...
}

I found examples without nesting for .kts:

val springVersion by extra("3.1.0.RELEASE")
val emailNotification by extra { "[email protected]" }

But I can't find any documentation to write this code on Kotlin DSL.


Solution

  • here

    val versions by extra( mapOf(
        "kotlin" to "1.3.72",
        "application" to mapOf(
            "versionName" to project.version,
            "versionCode" to project.version,
            "imageVersionCode" to project.version
        ),
        "android" to mapOf(
            "compileSdk" to 29,
            "minSdk" to 21,
            "targetSdk" to 29,
            "java" to JavaVersion.VERSION_1_8
        )
    ))