Gradle Kotlin DSL: Define Android build tools version in unique place

Viewed 1074

I am using Android Studio 4.0 and I added buildSrc module in a multi-module project to manage dependency versions. Inside buildSrc i have Dependencies object and Version object to use kotlin-dsl in the build.gradle.kts, The problem it's I can't access to Constants object from gradle.kts and I have 2 versions of com.android.tools.build:gradle:4.0.0 and I need this dependency for my custom plugins, How I can have only one version of the buildtools?

//Dependencies.kt
object PluginDependencies {
 const val buildTools = "com.android.tools.build:gradle:${Versions.buildTools}"
}

//Versions.kt
object Versions {
const val buildTools = "4.0.0" 
}

I have this in my build.gradle.kts

plugins {
    `kotlin-dsl`
}
gradlePlugin {
    plugins {
        register("CustomPlugin") {
            id = "customPlugin"
            implementationClass = "CustomPlugin"
        }
    }
}
repositories {
    mavenCentral()
    google()
    jcenter()
}

dependencies {
    // this cuase a compile error -> implementation("com.android.tools.build:gradle:${constants.Versions.buildTools}")
    implementation("com.android.tools.build:gradle:4.0.0")
    implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72")

    /* Depend on the default Gradle API's since we want to build a custom plugin */
    implementation(gradleApi())
    implementation(localGroovy())
}
1 Answers

You can not use the constant PluginDependencies in your buildSrc/build.gradle.kts file because at this stage the build script isn't aware of your Code which relies in buildSrc/src/main/kotlin/PluginDependencies.kt. The buildSrc/build.gradle.kts file is the first that gets build and after that the rest.

This is still true but what you can do is creating a buildSrc within a buildSrc. I wasn't aware of it but currently found this sample:

https://github.com/badoo/Reaktive/

Related