Android Studio升级Gradle至8.2.1失败,求解决方案
Gradle升级至8.2.1/8.3的解决方案
问题描述
我尝试将Gradle升级到最新版本,Android Studio提示可升级至8.2.1,但Gradle官网显示8.3才是最新版。同步时Maven仓库找不到8.2.1,手动下载并在gradle-wrapper.properties配置本地路径也无效,报错如下:
- 配置根项目'StarterApp'时出错
无法解析':classpath'的所有文件。
找不到com.android.tools.build:gradle:8.2.1。
已在以下位置搜索:
- https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/8.2.1/gradle-8.2.1.pom
查看Google仓库后发现未提供对应版本,求升级到8.2.1甚至8.3的有效方案。
现有配置文件
build.gradle(Project)
buildscript { ext.kotlin_version = '1.8.20' repositories { google() } dependencies { classpath 'com.android.tools.build:gradle:8.2.1' } } allprojects { repositories { mavenLocal() maven { url "https://jitpack.io" } } } buildscript { dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } }
build.gradle(Module)
plugins { id 'com.android.application' } android { namespace 'com.example.starter' compileSdk 34 defaultConfig { applicationId "com.example.starter" minSdk 24 targetSdk 34 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { debug { debuggable true } 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 } } tasks.withType(JavaCompile).configureEach { options.compilerArgs += '-Xlint:deprecation' } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation 'androidx.exifinterface:exifinterface:1.3.6' // check for the latest version online implementation 'androidx.activity:activity:1.7.2' implementation 'androidx.activity:activity-ktx:1.7.2' implementation 'androidx.fragment:fragment:1.6.1' implementation 'com.google.android.gms:play-services-maps:18.1.0' implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.9.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.2' implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2' implementation 'androidx.navigation:navigation-fragment:2.7.2' implementation 'androidx.navigation:navigation-ui:2.7.2' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' }
gradle.properties
# Project-wide Gradle settings. # IDE (e.g. Android Studio) users: # Gradle settings configured through the IDE *will override* # any settings specified in this file. # For more details on how to configure your build environment visit # http://www.gradle.org/docs/current/userguide/build_environment.html # Specifies the JVM arguments used for the daemon process. # The setting is particularly useful for tweaking memory settings. org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true # AndroidX package structure to make it clearer which packages are bundled with the # Android operating system, and which are packaged with your app's APK # https://developer.android.com/topic/libraries/support-library/androidx-rn android.useAndroidX=true # Enables namespacing of each library's R class so that its R class includes only the # resources declared in the library itself and none from the library's dependencies, # thereby reducing the size of the R class for that library android.nonTransitiveRClass=true
gradle-wrapper.properties
#Tue Aug 08 17:09:22 PDT 2023 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionUrl=file:///C:/Gradle/gradle-8.2.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists
解决方案
核心问题澄清
你混淆了Gradle发行版和**Android Gradle插件(AGP)**的版本:
gradle-wrapper.properties中配置的是Gradle发行版版本(如8.2.1、8.3)build.gradle里的com.android.tools.build:gradle:8.2.1是AGP版本,两者版本号需严格匹配,不能随意混用。
升级步骤(可选AGP 8.2.1或8.3)
1. 修正Project级build.gradle配置
合并重复的buildscript块,添加必要仓库,同时匹配AGP与Kotlin版本:
buildscript { ext { kotlin_version = '1.9.20' // AGP 8.2+需搭配Kotlin 1.9.0及以上 agp_version = '8.2.1' // 可替换为'8.3'升级到最新AGP } repositories { google() mavenCentral() // AGP部分依赖仅在此仓库分发 } dependencies { classpath "com.android.tools.build:gradle:$agp_version" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { google() mavenCentral() mavenLocal() maven { url "https://jitpack.io" } } }
2. 匹配Gradle发行版版本
AGP与Gradle版本对应关系:
- AGP 8.2.1 → Gradle 8.2
- AGP 8.3 → Gradle 8.4
修改gradle-wrapper.properties的distributionUrl:
# 对应AGP 8.2.1 distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip # 对应AGP 8.3 # distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
若已本地下载,保留file:///C:/Gradle/gradle-8.2-bin.zip即可,需确保路径和压缩包完整。
3. 修正Module级build.gradle
改用规范的插件DSL格式,添加Kotlin插件及兼容配置:
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' // 引入Kotlin插件 } android { namespace 'com.example.starter' compileSdk 34 defaultConfig { applicationId "com.example.starter" minSdk 24 targetSdk 34 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { debug { debuggable true } release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_17 // AGP 8.0+推荐使用Java 17,兼容Java 8也可 targetCompatibility JavaVersion.VERSION_17 } kotlinOptions { jvmTarget = '17' // 与Java版本保持一致 } buildFeatures { viewBinding true } } tasks.withType(JavaCompile).configureEach { options.compilerArgs += '-Xlint:deprecation' } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation 'androidx.exifinterface:exifinterface:1.3.6' implementation 'androidx.activity:activity:1.7.2' implementation 'androidx.activity:activity-ktx:1.7.2' implementation 'androidx.fragment:fragment:1.6.1' implementation 'com.google.android.gms:play-services-maps:18.1.0' implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.9.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.2' implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2' implementation 'androidx.navigation:navigation-fragment:2.7.2' implementation 'androidx.navigation:navigation-ui:2.7.2' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' }
4. 同步与清理
- 点击Android Studio右上角「Sync Project with Gradle Files」
- 执行
File → Invalidate Caches... → Invalidate and Restart - 若仍有问题,删除项目根目录下的
.gradle和build文件夹后重新同步
内容的提问来源于stack exchange,提问作者Sean Zlatnik
相关产品推荐
相关产品推荐

