5

I'm trying to adopt the Kotlin Gradle DSL. I have a gradle.properties with, among other things:

slf4jVersion=2.0.0-alpha1

I declare the dependency in build.gradle.kts with:

implementation( name = "slf4j-api",
  group = "org.slf4j", version = project.properties["slf4jVersion"].toString())

Yes, it works, but it also smells. The issue is the ugliness of project.properties["slf4jVersion"].toString()

Shouldn't I be able to just write slf4jVersion? It makes standard configuration look like a custom hack. What is the canonical and succinct way to keep version numbers together in a separate file with the Kotlin Gradle DSL?

1 Answer 1

2

You can access project properties via delegated properties.

In your build.gradle.kts:

val slf4jVersion: String by project
implementation(name = "slf4j-api", group = "org.slf4j", version = slf4jVersion)

Official sample: https://github.com/gradle/kotlin-dsl-samples/blob/3c977388f78bdcff1f7ed466e8d27feb5bf32275/samples/project-properties/build.gradle.kts#L14

Sign up to request clarification or add additional context in comments.

3 Comments

So every entry in gradle.properties needs a matching val delegation? This looks nicer but is also more fragile. I do appreciate the option, though. Maybe there could be a better way to declare the version numbers than in gradle.properties.
@TravisWell same question... have you found a solution?
@Arst no. now I just put versions in the gradle file instead of using a properties file. with a naming convention, it's easy to grep versions from the build.gradle.kts files, and a one-line bash script uses sed -i to update versions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.