How to declare dependencies in one place when using Gradle Kotlin DSL?

Finally figure out how to do this.

I create example project to demonstrate this approach.

Basically what you need to do is precompile all your build extensions into jar and add this jar to classpath in settings.gradle[.kts] like so:

buildscript {
    repositories {
        flatDir {
            dirs("${rootProject.projectDir}/libs/")
        }
    }
    dependencies {
        classpath(group = "ru.bejibx.android.gradle", name = "build-extensions", version = "1.0")
    }
}

After this you can reference almost all of your extension code in build scripts.

Two problems with this approach that I found:

  • pluginManagement block in settings.gradle[.kts] should be first block so you can’t update classpath before it and use you code here. The solution is to not use this block and move plugins configuration logic to your own gradle plugin and just apply this plugin inside settings.gradle[.kts].

  • For some strange reason plugins block only allow you to use code from default java package. See this stackoverflow question for more info. To fix this I just keep my extension methods for `` in default java package.