升级Android Studio Electric Eel后Gradle项目同步失败求助
Android Studio Electric Eel升级后Gradle同步失败问题
问题描述
将Android Studio升级至Electric Eel | 2022.1.1版本后,创建新项目时触发Gradle project sync failed错误。已尝试Invalidating Cache、通过Project Structure修改Gradle版本、禁用Flutter插件,均未解决问题。
错误信息
A problem occurred configuring root project 'BIAssignment'. > Could not resolve all files for configuration ':classpath'. > Could not resolve com.android.tools.build:gradle:7.4.0. Required by: project : > com.android.application:com.android.application.gradle.plugin:7.4.0 project : > com.android.library:com.android.library.gradle.plugin:7.4.0 > No matching variant of com.android.tools.build:gradle:7.4.0 was found. The consumer was configured to find a runtime of a library compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.5' but: - Variant 'apiElements' capability com.android.tools.build:gradle:7.4.0 declares a library, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares an API of a component compatible with Java 11 and the consumer needed a runtime of a component compatible with Java 8 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '7.5') - Variant 'javadocElements' capability com.android.tools.build:gradle:7.4.0 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 version (required compatibility with Java 8) - Doesn't say anything about its elements (required them packaged as a jar) - Doesn't say anything about org.gradle.plugin.api-version (required '7.5') - Variant 'runtimeElements' capability com.android.tools.build:gradle:7.4.0 declares a runtime of a library, 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 '7.5') - Variant 'sourcesElements' capability com.android.tools.build:gradle:7.4.0 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 version (required compatibility with Java 8) - Doesn't say anything about its elements (required them packaged as a jar) - Doesn't say anything about org.gradle.plugin.api-version (required '7.5')
当前项目配置
项目结构
- 截图1:标准Android项目结构,包含app模块、gradle文件夹等核心目录
- 截图2:Gradle相关配置界面展示
根目录build.gradle
plugins { id 'com.android.application' version '7.4.0' apply false id 'com.android.library' version '7.4.0' apply false }
模块build.gradle
plugins { id 'com.android.application' } android { namespace 'com.biassignment' compileSdk 33 defaultConfig { applicationId "com.biassignment" minSdk 24 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } buildFeatures { viewBinding true } } dependencies { implementation 'androidx.appcompat:appcompat:1.6.0' implementation 'com.google.android.material:material:1.7.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.navigation:navigation-fragment:2.5.3' implementation 'androidx.navigation:navigation-ui:2.5.3' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' }
解决方案
核心原因
错误日志明确指向两个问题:
com.android.tools.build:gradle:7.4.0要求运行环境为Java 11,但当前项目配置的是Java 8- Gradle插件7.4.0与Gradle版本7.5存在属性不匹配问题
修复步骤
切换项目JDK版本为Java 11
- 打开Android Studio,依次进入
File > Project Structure > SDK Location - 在
JDK location选择Android Studio自带的Java 11(路径通常为Android Studio安装目录/jbr),或手动指定本地已安装的Java 11路径 - 点击
OK保存配置
- 打开Android Studio,依次进入
调整Gradle插件版本(可选)
若切换Java 11后仍有同步问题,可升级Gradle插件至与Electric Eel兼容的稳定版,修改根目录build.gradle:plugins { id 'com.android.application' version '7.4.2' apply false id 'com.android.library' version '7.4.2' apply false }同步项目时同意下载对应Gradle版本即可
保留模块编译配置
模块中的compileOptions可继续使用Java 8,Android Gradle插件支持用Java 8编写业务代码,同时用Java 11运行Gradle重新同步项目
点击右上角Sync Project with Gradle Files按钮,等待同步完成
内容的提问来源于stack exchange,提问作者Sidharth Mudgil
相关产品推荐
相关产品推荐

