英文:
Use TOML vesion file in gradle
问题
我尝试使用来自libs.versions.toml
的版本设置composeOptions
,如下所示:
android {
// ...
composeOptions {
kotlinCompilerExtensionVersion = libs.versions.kotlinCompilerExtensionVersion.get()
}
}
但是我在使用.get()
函数时遇到了错误:
> 无法解析的引用。以下候选项都不适用
> 由于接收者类型不匹配: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版本工作正常,当我使用它来应用插件和实现库时,但在使用.get()
获取版本时不起作用。
Gradle版本为7.5.1。
英文:
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
答案1
得分: 3
toml文件 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" }
在Gradle中使用: libs.versions.kotlinVersion.get()
,例如:
minSdk libs.versions.minSdk.get().toInteger()
...
dependencies {
implementation "org.jetbrains.kotlin:kotlin-reflect:${libs.versions.kotlinVersion.get()}"
implementation libs.kotlinStdlibJdk8
}
英文:
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
}
答案2
得分: 1
如果在尝试通过.get()
方法从您的Gradle版本目录获取库xxx
的版本时出现Unresolved reference. None of the following candidates is applicable receiver type mismatch...
的错误,99%的情况下意味着您在您的[versions]
块中有一个名为xxx-yyy
的库,例如:
[versions]
kotlin = "1.7.20"
kotlin-coroutines = "1.6.4"
kotlin-serialization = "1.4.1"
在这种情况下,Gradle不会为字段libs.versions.kotlin
(示例中)生成常规的Provider
,而是生成更复杂的对象,因为它必须生成libs.versions.kotlin.coroutines
,libs.versions.kotlin.serialization
字段。
这就是为什么如果您想获取库版本,您必须在使用.get()
之前明确要求提供者,使用.asProvider()
:
libs.versions.kotlin.asProvider().get()
对于相关库,如下所示:
libs.versions.kotlinCompilerExtensionVersion.asProvider().get()
英文:
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()
答案3
得分: 0
以下是翻译好的部分:
简短答案:
如果你的 libs.versions.toml
文件如下所示:
[versions]
compose = "1.4.3"
compose-compiler = "1.4.7"
那么你应该使用以下代码:
composeOptions {
kotlinCompilerExtensionVersion libs.versions.compose.compiler.get()
}
长篇答案:
Gradle 将根据 libs.versions.toml
文件生成一个名为 LibrariesForLibs
的类,你可以在项目根路径下的 .gradle/7.5/dependencies-accessors/sources
文件夹中找到这个类。
例如,上面提到的 toml 文件将生成如下代码:
@NonNullApi
public class LibrariesForLibs extends AbstractExternalDependencyFactory {
@Inject
public LibrariesForLibs(DefaultVersionCatalog config, ProviderFactory providers) {
super(config, providers);
}
private final VersionAccessors vaccForVersionAccessors = new VersionAccessors(providers, config);
/**
* 返回在 versions 下的版本组
*/
public VersionAccessors getVersions() { return vaccForVersionAccessors; }
public static class VersionAccessors extends VersionFactory {
private final ComposeVersionAccessors vaccForComposeVersionAccessors = new ComposeVersionAccessors(providers, config);
public VersionAccessors(ProviderFactory providers, DefaultVersionCatalog config) { super(providers, config); }
/**
* 返回在 versions.compose 下的版本组
*/
public ComposeVersionAccessors getCompose() { return vaccForComposeVersionAccessors; }
}
public static class ComposeVersionAccessors extends VersionFactory implements VersionNotationSupplier {
public ComposeVersionAccessors(ProviderFactory providers, DefaultVersionCatalog config) { super(providers, config); }
/**
* 返回与此别名相关联的版本:compose (1.4.3)
* 如果版本是丰富的版本且不能表示为单个版本字符串,则返回空字符串。
* 此版本在 libs.versions.toml 目录中声明。
*/
public Provider<String> asProvider() {return getVersion("compose");}
/**
* 返回与此别名相关联的版本:compose.compiler (1.4.7)
* 如果版本是丰富的版本且不能表示为单个版本字符串,则返回空字符串。
* 此版本在 libs.versions.toml 目录中声明。
*/
public Provider<String> getCompiler() {return getVersion("compose.compiler");}
}
}
当你在 gradle.build 文件中使用 libs
变量时,实际上是在访问 LibrariesForLibs
的一个实例对象。因此,如果你想获取 compose-compiler
的版本,你需要调用 libs.getVersions().getCompose().getCompiler().get()
,而 groovy
具有内置的 getter
语法糖,因此我们可以编写更优雅的代码 libs.versions.compose.compiler.get()
英文:
Short answer:
If your libs.versions.toml is like below
[versions]
compose = "1.4.3"
compose-compiler = "1.4.7"
then you should use
composeOptions {
kotlinCompilerExtensionVersion libs.versions.compose.compiler.get()
}
Long answer:
Gradle will generate a LibrariesForLibs
class based on the libs.versions.toml
file, which you can find in the .gradle/7.5/dependencies-accessors/sources
folder in the project root path.
For example, the toml mentioned above would generate code as follows:
@NonNullApi
public class LibrariesForLibs extends AbstractExternalDependencyFactory {
@Inject
public LibrariesForLibs(DefaultVersionCatalog config, ProviderFactory providers) {
super(config, providers);
}
private final VersionAccessors vaccForVersionAccessors = new VersionAccessors(providers, config);
/**
* Returns the group of versions at versions
*/
public VersionAccessors getVersions() { return vaccForVersionAccessors; }
public static class VersionAccessors extends VersionFactory {
private final ComposeVersionAccessors vaccForComposeVersionAccessors = new ComposeVersionAccessors(providers, config);
public VersionAccessors(ProviderFactory providers, DefaultVersionCatalog config) { super(providers, config); }
/**
* Returns the group of versions at versions.compose
*/
public ComposeVersionAccessors getCompose() { return vaccForComposeVersionAccessors; }
}
public static class ComposeVersionAccessors extends VersionFactory implements VersionNotationSupplier {
public ComposeVersionAccessors(ProviderFactory providers, DefaultVersionCatalog config) { super(providers, config); }
/**
* Returns the version associated to this alias: compose (1.4.3)
* If the version is a rich version and that its not expressible as a
* single version string, then an empty string is returned.
* This version was declared in catalog libs.versions.toml
*/
public Provider<String> asProvider() {return getVersion("compose");}
/**
* Returns the version associated to this alias: compose.compiler (1.4.7)
* If the version is a rich version and that its not expressible as a
* single version string, then an empty string is returned.
* This version was declared in catalog libs.versions.toml
*/
public Provider<String> getCompiler() {return getVersion("compose.compiler");}
}
}
When you use the libs
variable in gradle.build, you are actually accessing an instance object of LibrariesForLibs
. So if you want to get the version of compose-compiler
, you need to call libs.getVersions().getCompose().getCompiler().get()
, and groovy
has built-in getter
syntactic sugar, so we can write more elegant code libs.versions.compose.compiler.get()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论