如何解决所有项目中./gradlew install无法运行的问题?
所有项目执行
./gradlew install均失败,创建新项目、升级Gradle无效 问题描述
所有项目中执行./gradlew install命令都报错,尝试创建新项目、升级Gradle版本均未解决问题。已将JAVA_HOME设置为JDK 17版本。
项目配置
- Project Gradle JDK版本: 17
- Gradle版本: 8.7
- Gradle Plugin版本: 8.5.2
模块build.gradle.kts内容
plugins { alias(libs.plugins.android.application) alias(libs.plugins.jetbrains.kotlin.android) alias(libs.plugins.compose.compiler) } android { namespace = "com.arzhangap.test" compileSdk = 34 defaultConfig { applicationId = "com.arzhangap.test" minSdk = 24 targetSdk = 34 versionCode = 1 versionName = "1.0" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { useSupportLibrary = true } } buildTypes { release { isMinifyEnabled = false proguardFiles( getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" ) } } compileOptions { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } kotlinOptions { jvmTarget = "17" } buildFeatures { compose = true } composeOptions { kotlinCompilerExtensionVersion = "1.5.1" } packaging { resources { excludes += "/META-INF/{AL2.0,LGPL2.1}" } } } dependencies { implementation(libs.androidx.core.ktx) implementation(libs.androidx.lifecycle.runtime.ktx) implementation(libs.androidx.activity.compose) implementation(platform(libs.androidx.compose.bom)) implementation(libs.androidx.ui) implementation(libs.androidx.ui.graphics) implementation(libs.androidx.ui.tooling.preview) implementation(libs.androidx.material3) testImplementation(libs.junit) androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.espresso.core) androidTestImplementation(platform(libs.androidx.compose.bom)) androidTestImplementation(libs.androidx.ui.test.junit4) debugImplementation(libs.androidx.ui.tooling) debugImplementation(libs.androidx.ui.test.manifest) }
执行./gradlew install后的错误日志
FAILURE: Build failed with an exception. * What went wrong: A problem occurred configuring root project 'test'. > Could not resolve all artifacts for configuration ':classpath'. > Could not resolve com.android.tools.build:gradle:8.5.2. Required by: project : > com.android.application:com.android.application.gradle.plugin:8.5.2 > No matching variant of com.android.tools.build:gradle:8.5.2 was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.7' but: - Variant 'apiElements' declares a library, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares a component for use during compile-time, compatible with Java 11 and the consumer needed a component for use during runtime, compatible with Java 8 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '8.7') - Variant 'javadocElements' declares a component for use during runtime, 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 elements (required them packaged as a jar) - Doesn't say anything about its target Java version (required compatibility with Java 8) - Doesn't say anything about org.gradle.plugin.api-version (required '8.7') - Variant 'runtimeElements' declares a library for use during runtime, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares a component, compatible with Java 11 and the consumer needed a component, compatible with Java 8 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '8.7') - Variant 'sourcesElements' declares a component for use during runtime, 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 elements (required them packaged as a jar) - Doesn't say anything about its target Java version (required compatibility with Java 8) - Doesn't say anything about org.gradle.plugin.api-version (required '8.7')
已尝试更换不同JDK版本,问题依旧存在。
解决方案
1. 修复Gradle与AGP版本兼容性
Gradle 8.7要求Android Gradle Plugin(AGP)版本至少为8.6.0,当前使用的AGP 8.5.2不兼容。两种解决方式:
- 升级AGP到8.6.0及以上版本(同步修改
libs.versions.toml中对应的AGP版本号) - 降级Gradle到8.0-8.6之间的版本(适配AGP 8.5.2)
2. 确保Gradle全局JDK配置正确
即使设置了JAVA_HOME,Gradle可能单独使用其他JDK:
- 检查项目根目录
gradle.properties,添加或修改:
替换为实际JDK 17的安装路径org.gradle.java.home=/path/to/your/jdk17 - 在Android Studio中打开
File > Project Structure > SDK Location,确认Gradle JDK选择的是JDK 17
3. 强制Gradle使用Java 17运行
在gradle.properties中添加:
org.gradle.jvmargs=-Djava.version=17
4. 清理缓存并重构建
执行以下命令清除Gradle缓存后重新尝试构建:
./gradlew clean build --refresh-dependencies
内容的提问来源于stack exchange,提问作者Ari Ap
相关产品推荐
相关产品推荐

