Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parametrize Kotlin version in the plugins block of a build.gradle.kts script?

In the script below:

val kotlinVersion by extra ("1.3.61")

println("Version "+kotlinVersion)

plugins {
    kotlin("jvm") version kotlinVersion
}

The variable kotlinVersion is correctly printed. Nevertheless it is not recognized in the plugins block and the following error is risen:

e: /home/achadde/sources/kotlin/minichain/build.gradle.kts:6:27: Unresolved reference: kotlinVersion

How can I fix this?

like image 331
Emile Achadde Avatar asked Feb 11 '20 09:02

Emile Achadde


People also ask

How do I set the gradle plugin version?

You can specify the Gradle version in either the File > Project Structure > Project menu in Android Studio, or update your Gradle version using the command line. The preferred way is to use the Gradle Wrapper command line tool, which updates the gradlew scripts. The following example sets the Gradle version to 7.5.

How do I find my Kotlin version?

You can check the Kotlin version in Tools | Kotlin | Configure Kotlin Plugin Updates. If you have projects created with earlier Kotlin versions, change the Kotlin version in your projects and update kotlinx libraries if necessary – check the recommended versions.

How do I change my Kotlin Android version?

In your Android studio, Go to Tools -> Kotlin -> Configure Kotlin Updates.

How do I convert build gradle to kts?

Because you can combine Groovy and KTS build files in a project, a simple way to start converting your project to KTS is to select a simple build file, like settings. gradle , rename it to settings. gradle. kts , and convert its contents to KTS.


2 Answers

Short answer:

There is currenlty no way of accessing anything of the outer scope from inside the lambda passed to plugins.

Long answer:

In case you use IntelliJ it will show you a bit more information:

'val kotlinVersion: String' can't be called in this context by implicit receiver. Use the explicit one if necessary.

The outer scope (this@Build_gradle) where you define kotlinVersion is not avaiable in the this@plugins scope so you have to define kotlinVersion inside the plugins lambda.

Since the extra delegate isn't available there either you can't use it:

plugins {
    val kotlinVersion = "1.3.61"
    // ...
}

Unfortunately using a label does not work:

val kotlinVersion by extra ("1.3.61")

plugins {
    // ... Unresolved reference: kotlinVersion 
    kotlin("jvm") version this@Build_gradle.kotlinVersion
}
like image 158
Willi Mentzel Avatar answered Oct 12 '22 15:10

Willi Mentzel


For whatever reason, the plugins block can't read the outer scope, so what I did was added the following entry to my settings.gradle.kts instead:

pluginManagement.resolutionStrategy.eachPlugin {
    if (requested.id.id.startsWith("org.jetbrains.kotlin.")) {
        useVersion("1.4.10")
    }
}

With my build.gradle.kts now as follows:

plugins {
    kotlin("jvm")
    kotlin("kapt")
}

println(kotlin.coreLibrariesVersion)

Note how the version can now be omitted. The following print statement will print out the version we specified in our settings.gradle.kts, if we ever want to access our Kotlin version further down in our build script.

like image 29
Andrey Kaipov Avatar answered Oct 12 '22 15:10

Andrey Kaipov