This is probably a standard newbie question (but t...
# gradle
s
This is probably a standard newbie question (but the only solution i found so far used [buildSrc](

https://www.youtube.com/watch?v=mAtrEPeAJSc&t=1710

) which I couldn’t make to work0, so how can I externalise the version information using KotlinDSL so I don’t have to retype the same String? The naive approach is along the lines of
Copy code
val kotlinVersion = "1.3.21"

plugins {
    kotlin("jvm") version kotlinVersion
    ...
}

dependencies {
    ...
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion")
}
But this fails (“String can’t be called from this context by implicit receiver”) for the usage in plugins.
y
Try using
project.extensions
. Creating the version in the extensions and getting them in the
dependencies
block.
s
Copy code
extensions.apply {
    add(String.javaClass, "kotlinVersion", "1.3.21")
}

plugins {
    kotlin("jvm") version project.extensions["kotlinVersion"] as String
    ...
}

dependencies {
    ...
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit:${extensions["kotlinVersion"]}")
}
works for the dependency- but not for the plugins-block. It’s the same problem as before (“can’t be called in this context by implicit receiver”) this time it’s the project variable that is flagged in this way.
g
Version in plugins block is enough, just remove version from dependency, Kotlin plugin will set it implicitly equal to plugin veraion
Also, if you still need it, I would add version to gradle.propertiea and read it from
properties
object instead using this ugly extensions block
s
ok, so we don’t have duplicated version information between the plugins- and dependencies-blocks (because I can drop the version information from the dependencies-block if that’s the case), So if i only want to introduce version-variables for the dependencies i can do so simply in the dependency block. e.g.
Copy code
dependencies {
    val jacksonVersion = "2.9.8"
    implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:$jacksonVersion")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion")
}
or I put it into the properties (which i have to awkwardly cast because Kotlin interpretes MutableMap<String, ?> as MutableMap<String, Nothing>)
Copy code
(properties as MutableMap<String, Any>).apply {
    put("jacksonVersion", "2.9.8")
}

dependencies {    implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:${properties[jacksonVersion]}")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin:${properties[jacksonVersion]}")
}
?!
g
Recommended way is to use buildSrc and just put versions there, those kotlin properties/o ject will be available for plugins block and for other modules, but not typed properties also work
No need to cast properties, just use delegates or get syntax And main thing, no need to put properties from code, create gradle.properties in root of your project and define properties there
Copy code
val jacksonVersion by project
Or val jacksonVersion = properties ["jacksonVersion"]
e
OR you could place the buildSrc property you’ve defined in the package
org.gradle.kotlin.dsl
😉
g
Why put properties to this package? It looks unnecessary, you can just use default package to have auto import in build.gradle files