无法解析com.android.tools.build:gradle:7.4.2,Gradle插件开发报错求助
问题描述
在Android项目中开发Gradle插件,项目包含build-logic模块及其中的convention子模块。编写了JvmLibraryPlugin并在convention模块的build.gradle.kts中注册,添加com.android.tools.build:gradle依赖时同步失败,报错如下:
Execution failed for task ':build-logic:convention:compileKotlin'. > Error while evaluating property 'filteredArgumentsMap' of task ':build-logic:convention:compileKotlin' > Could not resolve all files for configuration ':build-logic:convention:compileClasspath'. > Could not resolve com.android.tools.build:gradle:7.4.2. Required by: project :build-logic:convention > No matching variant of com.android.tools.build:gradle:7.4.2 was found. The consumer was configured to find an API of a library compatible with Java 8, preferably in the form of class files, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' but: - Variant 'apiElements' capability com.android.tools.build:gradle:7.4.2 declares an API of a library, packaged as a jar, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm': - Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8 - Variant 'javadocElements' capability com.android.tools.build:gradle:7.4.2 declares a runtime of a component, and its dependencies declared externally: - Incompatible because this component declares documentation and the consumer needed a library - Other compatible attributes: - Doesn't say anything about its target Java environment (preferred optimized for standard JVMs) - Doesn't say anything about its target Java version (required compatibility with Java 8) - Doesn't say anything about its elements (required them preferably in the form of class files) - Doesn't say anything about org.jetbrains.kotlin.platform.type (required 'jvm') - Variant 'runtimeElements' capability com.android.tools.build:gradle:7.4.2 declares a runtime of a library, packaged as a jar, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm': - Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8 - Variant 'sourcesElements' capability com.android.tools.build:gradle:7.4.2 declares a runtime of a component, and its dependencies declared externally: - Incompatible because this component declares documentation and the consumer needed a library - Other compatible attributes: - Doesn't say anything about its target Java environment (preferred optimized for standard JVMs) - Doesn't say anything about its target Java version (required compatibility with Java 8) - Doesn't say anything about its elements (required them preferably in the form of class files) - Doesn't say anything about org.jetbrains.kotlin.platform.type (required 'jvm')
尝试过将AGP版本改为8.x.x,修改Gradle JDK为11、15、17、19,问题依旧。
项目相关配置
build-logic:convention模块的build.gradle.kts
plugins { `kotlin-dsl` } group = "***.android.scandroid_2.buildlogic" java { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } tasks.withType<KotlinCompile>().configureEach { kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8.toString() } } dependencies { compileOnly(libs.kotlin.gradle.plugin) compileOnly(libs.android.gradlePlugin) } gradlePlugin { plugins { register("jvmLibraryPlugin") { id = "JvmLibraryPlugin" implementationClass = "JvmLibraryPlugin" } } }
build-logic模块的settings.gradle.kts
pluginManagement { repositories { google() mavenCentral() } versionCatalogs { create("libs") { from(files("../gradle/libs.versions.toml")) } } } rootProject.name = "build-logic" include(":convention")
JvmLibraryPlugin代码
class JvmLibraryPlugin : Plugin<Project> { override fun apply(target: Project) = with(target) { applyPlugins() } private fun Project.applyPlugins() { with(pluginManager) { apply("org.jetbrains.kotlin.jvm") } } }
项目根目录build.gradle.kts
plugins { alias(libs.plugins.ksp) apply false alias(libs.plugins.android.application) apply false alias(libs.plugins.android.library) apply false alias(libs.plugins.kotlin.jvm) apply false }
项目根目录settings.gradle
pluginManagement { includeBuild("build-logic") repositories { google() mavenCentral() gradlePluginPortal() } } dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } } rootProject.name = "Scandroid_2" include(":app") include(":core-db-impl") include(":core-domain") include(":feature:settings-impl") include(":feature:scanner-api") include(":feature:scanner-impl") include(":feature:code-list-api") include(":feature:code-list-impl") include(":feature:code-details-api") include(":feature:code-details-impl") include(":core-ui") include(":core-resources") include(":feature:settings-api") include(":core-utils") include(":core-executor") include(":core-mvi")
gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists
解决方案
问题核心是**build-logic:convention模块强制使用Java 8编译,但AGP 7.4.2及以上版本要求运行环境为Java 11+**,即使修改了IDE的Gradle JDK,模块自身的Java版本配置仍在限制依赖解析。
步骤1:更新build-logic:convention的Java版本配置
修改build-logic/convention/build.gradle.kts中的Java配置,将版本从Java 8升级到Java 11:
java { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 } tasks.withType<KotlinCompile>().configureEach { kotlinOptions { jvmTarget = JavaVersion.VERSION_11.toString() } }
步骤2:确保Gradle版本与AGP版本匹配
- AGP 7.4.2要求Gradle版本至少7.5(当前Gradle版本7.5符合要求)
- 如果使用AGP 8.x.x,需要对应Gradle 8.0+,同时Java版本需升级到17,此时需同步修改上述Java配置为
VERSION_17,并更新gradle-wrapper.properties中的distributionUrl为Gradle 8.x版本(比如gradle-8.0-bin.zip)
步骤3:验证IDE的Gradle JDK配置
确保IDE中设置的Gradle JDK为Java 11(或对应AGP版本要求的Java版本):
- Android Studio:
File > Settings > Build, Execution, Deployment > Build Tools > Gradle,设置Gradle JDK为Java 11+
额外说明
kotlin-dsl插件本身对Gradle和Java版本有要求,升级Java版本后需确保整体版本链一致- 若业务模块仍需兼容Java 8,只需在业务模块中单独配置
sourceCompatibility和targetCompatibility为Java 8即可,build-logic模块作为插件开发环境,需遵循AGP的版本要求
内容的提问来源于stack exchange,提问作者TOvchinnikova
相关产品推荐
相关产品推荐

