Gradle has a special module called buildSrc where you can put Kotlin code and make it directly available to all your Gradle build files.
This is better than the traditional solution of putting your global variables in a Gradle ext
property or in a Groovy HashMap
,
because in that case, the IDE support is exactly what you would expect when you work with an HashMap
.
Now, what is better than writing manually those files Versions.kt
and Libs.kt
?
Having a Gradle plugin write them for you maybe?
-
Kotlin, JVM and Android projects.
-
Gradle 4.3+ and Gradle 5.0+
-
The IDE integration works in IntelliJ >= 2008.03+ and Android Studio 3.2+.
-
The IDE integration works for both
build.gradle
(Groovy) andbuild.gradle.kts
(Kotlin) files.
Edit your root build.gradle(.kts)
file
buildscript {
//...
}
plugins {
id("de.fayard.buildSrcVersions") version "0.3.2"
}
// Don't put any code before the buildscripts {} and plugins {} block
The plugin adds a task to your build, also called :buildSrcVersions
. Run it like this:
$ ./gradlew buildSrcVersions
> Task :dependencyUpdates
> Task :buildSrcVersions
new file: buildSrc/build.gradle.kts
new file: buildSrc/.gitignore
new file: buildSrc/settings.gradle.kts
new file: buildSrc/src/main/kotlin/Libs.kt
new file: buildSrc/src/main/kotlin/Versions.kt
Synchronize your Gradle build and you can start to replace your magic strings:
dependencies {
implementation(Libs.okio)
implementation(Libs.com_squareup_moshi_moshi)
}
Upgrade dependencies tend to be tedious but it is critically important:
-
Few projects have versioned documentation:
master/README.md
is their documentation. This means you can waste time trying out snippets that do not work in the version you are using. -
When you open an issue, you will often be asked: Can you reproduce this problem in the latest version?
This plugin inherits from Ben Manes’s Versions plugin the feature to automatically determine which dependencies have updates.
When you decide you want to update some dependencies, run again the plugin:
$ ./gradlew buildSrcVersions
> Task :dependencyUpdates
> Task :buildSrcVersions
modified: buildSrc/src/main/kotlin/Libs.kt
modified: buildSrc/src/main/kotlin/Versions.kt
The file Versions.kt
is regenerated with a comment indicating which new version is available.
object Versions {
val moshi = "1.8.0"
val com_squareup_okhttp3 = "3.11.0" // available: "3.12.0"
}
As shown in this screencast:
-
To update, just delete the part
"3.11.0" // available
. -
Remove the comment if you don’t want to update.
Questions? Look at the existing issues, then ask your own.
See CHANGELOG.md
The project was called gradle-kotlin-dsl-libs
before, which was a bad name for reasons explained in #26
-
This project is licensed under the MIT License. See LICENSE.txt
-
Explain your use case and start the discussion before your submit a pull-request
-
Please read CONTRIBUTING.md the process for submitting pull requests to us.
Gradle and JetBrains have made this plugin possible
by working on improving the IDE support for Kotlin code from the buildSrc
module.
This plugin embraces and extends Ben Manes’s wonderful "Versions" plugin.
The Kotlin code generation is powered by Square’s Kotlinpoet