提问人:muetzenflo 提问时间:10/9/2023 最后编辑:muetzenflo 更新时间:10/20/2023 访问量:139
Gradle 无法解析复合构建 + 版本目录中的“java”
Gradle cannot resolve `java` in composite build + version catalog
问:
我正在创建一个 gradle 版本目录以及复合构建中定义的构建逻辑的最小示例。到目前为止,基本设置正在工作。但是由于某种原因,我无法分级识别我的第一个构建逻辑插件中的表达式。java
my.android-commons.gradle.kts
我不明白为什么,因为java插件已经声明了,应该足以让gradle找到,对吧?java
这是我当前的文件树:
/root
- /app
- /core
- /feature
- /build-logic
--- /android-commons
---- /src/main/kotlin
----- my.android-commons.gradle.kts (5)
--- build.gradle.kts (4)
- settings.gradle.kts (3)
- /gradle
-- libs.versions.toml
- build.gradle.kts (2)
- settings.gradle.kts (1)
以下是重要文件的内容:
(1) /root/settings.gradle.kts
// == Define locations for build logic ==
pluginManagement {
includeBuild("build-logic")
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}
// == Define locations for components ==
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
include("core")
include("feature")
// == Define the inner structure of this component ==
rootProject.name = "Session Timer"
include("app")
(2) /root/build.gradle.kts
plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.android.library) apply false
}
(3) ../build-logic/settings.gradle.kts
dependencyResolutionManagement {
repositories {
gradlePluginPortal()
google()
}
versionCatalogs {
create("libs") {
from(files("../gradle/libs.versions.toml"))
}
}
}
rootProject.name = "build-logic"
include("android-commons")
(4) ../build-logic/android-commons/build.gradle.kts
plugins {
`kotlin-dsl`
}
(5) ../build-logic/android-commons/src/../my.android-commons.gradle.kts
plugins {
id("java")
}
java { // not found by gradle
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
答:
0赞
ΓDΛ
10/20/2023
#1
我审查了您的项目并提供了如下解决方案。
off.android-commons.gradle.kts
fun Project.configureJava() {
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}
}
fun Project.java(action: JavaPluginExtension.() -> Unit) = extensions.configure<JavaPluginExtension>(action)
fun Project.configureKotlin() {
// Configure Java to use our chosen language level. Kotlin will automatically pick this up
configureJava()
}
class KotlinAndroidConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
with(pluginManager) {
apply("org.jetbrains.kotlin.android")
}
configureKotlin()
}
}
}
build.gradle.kts
android-commons 关卡
plugins {
`kotlin-dsl`
}
repositories {
mavenCentral()
}
gradlePlugin{
plugins {
register("kotlinAndroid") {
id = "app.sessiontimer.kotlin.android"
implementationClass = "Off_android_commons_gradle.KotlinAndroidConventionPlugin"
}
}
}
评论
0赞
muetzenflo
10/23/2023
在我更新 kotlin 编译器版本后,构建突然变绿了。所以我没有尝试你的解决方案。但既然你付出了努力:谢谢你的时间,享受赏金:)
0赞
ΓDΛ
10/23/2023
别客气。不要忘记分享您的解决方案。
评论