Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use TOML vesion file in gradle

I try use version from libs.versions.toml to set composeOptions like this:

android {
 ...
    composeOptions {
        kotlinCompilerExtensionVersion = libs.versions.kotlinCompilerExtensionVersion.get()
    }
}

But I get error with .get() function :

Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: public inline operator fun <K, V> Map<out TypeVariable(K), TypeVariable(V)>.get(key: TypeVariable(K)): TypeVariable(V)? defined in kotlin.collections public operator fun MatchGroupCollection.get(name: String): MatchGroup? defined in kotlin.text

TOML versioning working well when a use this to apply plugins and implement libraries but not working when I want to get a version with .get()

Gradle version 7.5.1

like image 407
M.Devan Avatar asked Oct 30 '25 05:10

M.Devan


2 Answers

toml file MyProject/gradle/libs.versions.toml:

[versions]
minSdk = "24"
kotlinVersion = "1.8.21"#https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-gradle-plugin
[libraries]
kotlinStdlibJdk8 = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8", version.ref = "kotlinVersion" }

In gradle use: libs.versions.kotlinVersion.get(), for example:

minSdk libs.versions.minSdk.get().toInteger()
...
dependencies {
    
    implementation "org.jetbrains.kotlin:kotlin-reflect:${libs.versions.kotlinVersion.get()}"
    implementation libs.kotlinStdlibJdk8

}

See documentation

like image 185
NickUnuchek Avatar answered Oct 31 '25 19:10

NickUnuchek


If you get Unresolved reference. None of the following candidates is applicable receiver type mismatch... when you are trying to get a version of library xxx from your Gradle Version Catalogs via .get() method, in 99% of cases it means that you have library xxx-yyy in your [versions] block, e.g.

[versions]

kotlin = "1.7.20"
kotlin-coroutines = "1.6.4"
kotlin-serialization = "1.4.1"

In that case Gradle won't generate regular Provider for the field libs.versions.kotlin (in the example), it generates more complex object, because it has to generate libs.versions.kotlin.coroutines, libs.versions.kotlin.serialization fields.

That's why if you want to get library version you have to explicitly ask for provider using .asProvider() before getter .get():

libs.versions.kotlin.asProvider().get()

and for library in question it will be:

libs.versions.kotlinCompilerExtensionVersion.asProvider().get()
like image 24
Evgeny K Avatar answered Oct 31 '25 20:10

Evgeny K



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!