如何修复Android Studio无法加载BuildCancellationToken类错误
修复Android Studio Hedgehog中“Unable to load class 'org.gradle.initialization.BuildCancellationToken'”错误
1. 修复gradle.properties语法错误
你的gradle.properties文件开头多了{、结尾多了},这会直接导致Gradle解析配置失败。替换为以下正确内容:
# 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 # Kotlin code style for this project: "official" or "obsolete": kotlin.code.style=official # 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
2. 确保AGP与Gradle版本兼容
你当前使用Gradle 8.2,对应的Android Gradle Plugin(AGP)版本必须为8.2.x,版本不匹配是类加载错误的核心原因:
- 打开
app/build.gradle.kts(或app/build.gradle) - 修改plugins块中的AGP版本至匹配值,同时确保Kotlin版本与AGP兼容:
// Kotlin DSL示例 plugins { id("com.android.application") version "8.2.0" id("org.jetbrains.kotlin.android") version "1.9.0" }
// Groovy DSL示例 plugins { id 'com.android.application' version '8.2.0' id 'org.jetbrains.kotlin.android' version '1.9.0' }
3. 彻底清理Gradle缓存与守护进程
- 完全关闭Android Studio
- 删除Gradle缓存目录:Windows路径为
C:\Users\你的用户名\.gradle\caches,直接删除整个caches文件夹 - 杀掉相关进程:
- Windows:打开任务管理器,结束所有
java.exe、javaw.exe、gradle.exe进程 - Mac/Linux:终端执行
killall java
- Windows:打开任务管理器,结束所有
- 重新打开Android Studio,点击工具栏的「Sync Project with Gradle Files」按钮
4. 重新下载Gradle分发包
如果Gradle包损坏,手动删除已下载的分发文件:
- 找到Gradle wrapper存储目录:Windows为
C:\Users\你的用户名\.gradle\wrapper\dists\gradle-8.2-bin - 删除该目录下的所有文件
- 回到Android Studio重新触发同步,Gradle会自动重新下载完整的8.2版本包
5. 验证SDK与Build Tools配置
- 打开SDK Manager,确认已安装Android SDK Build Tools 34.0.0(AGP 8.2.0对应版本)
- 检查
local.properties中的sdk.dir路径是否正确,确保无拼写错误
内容的提问来源于stack exchange,提问作者Dewmini Chamodya
相关产品推荐
相关产品推荐

