Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can’t I use val inside Plugins {}?

val kotlinVersion = "1.3.72"
plugins {
    // Error: 'val kotlinVersion: String' can't be called in this context by implicit receiver. Use the explicit one if necessary
    kotlin("jvm").version(kotlinVersion)]
}

I use Kotlin's standard library as a dependency too and I only want to have to specify the version in one place but when I try something like I did above in my build.gradle.kts I get that error you see in the comment. How do I resolve this?

like image 614
SmushyTaco Avatar asked Jul 02 '26 18:07

SmushyTaco


1 Answers

See Constrained Syntax in Gradle documentation:

The plugins {} block does not support arbitrary code. It is constrained, in order to be idempotent (produce the same result every time) and side effect free (safe for Gradle to execute at any time)...

and links to Plugin Version Management to work around it, in your case

pluginManagement {
  plugins {
    id("kotlin").version(kotlinVersion)
  }
}

should work I believe.

Or according to

Interpolated strings are permitted for PluginDependencySpec.version(String), however replacement values must be sourced from Gradle properties.

It seems if you declare kotlinVersion in gradle.properties it can be used as

version("${kotlinVersion}")

in plugins {}.

like image 97
Alexey Romanov Avatar answered Jul 05 '26 08:07

Alexey Romanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!