https://kotlinlang.org logo
Title
s

Stephan Schroeder

04/29/2019, 11:16 AM
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
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

yousefa2

04/29/2019, 11:25 AM
Try using
project.extensions
. Creating the version in the extensions and getting them in the
dependencies
block.
s

Stephan Schroeder

04/29/2019, 11:53 AM
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

gildor

04/29/2019, 1:16 PM
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

Stephan Schroeder

04/29/2019, 3:52 PM
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.
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>)
(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

gildor

04/29/2019, 5:12 PM
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
val jacksonVersion by project
Or val jacksonVersion = properties ["jacksonVersion"]
e

efemoney

05/01/2019, 4:04 AM
OR you could place the buildSrc property you’ve defined in the package
org.gradle.kotlin.dsl
😉
g

gildor

05/02/2019, 1:34 AM
Why put properties to this package? It looks unnecessary, you can just use default package to have auto import in build.gradle files