升级Gradle构建工具至8.1.4后gradlew clean任务失败求助
Gradle升级至8.1.4后Android库模块clean任务失败(libGdx项目)
问题描述
将android.tools.build:gradle从7.3.1升级到8.1.4后,名为android的Android库模块执行gradlew clean时持续失败。手动删除build目录后重试仍报错,但项目其他模块clean正常,google和huawei两个应用模块可正常运行。移除copyAndroidNatives任务后错误消失,但该任务是libGdx必需的,无法删除。
报错信息
> java.io.IOException: Unable to delete directory 'C:\my_project\android\build' Failed to delete some children. This might happen because a process has files open or has its working directory set in the target directory. - C:\my_project\android\build\tmp\.cache\expanded\expanded.lock - C:\my_project\android\build\tmp\.cache\expanded - C:\my_project\android\build\tmp\.cache - C:\my_project\android\build\tmp
项目结构
project(":core") { apply plugin: "java-library" dependencies { api "com.badlogicgames.gdx:gdx:$gdxVersion" api "com.badlogicgames.gdx:gdx-ai:$gdxAIVersion" } } project(":game") { apply plugin: "java-library" dependencies { api project(":core") } } project(":mobile") { apply plugin: "java-library" dependencies { api project(":game") } } project(":android") { apply plugin: "com.android.library" configurations { natives } dependencies { api project(":mobile") api "androidx.core:core:$androidxCoreVersion" api "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64" } } project(":google") { apply plugin: "com.android.application" dependencies { implementation project(":android") implementation "com.android.support:multidex:$multidexVersion" constraints { implementation('androidx.fragment:fragment') { version { strictly "$androidxCoreVersion" } } } } } project(":huawei") { apply plugin: "com.android.application" dependencies { implementation project(":android") implementation "com.android.support:multidex:$multidexVersion" } }
Android模块build.gradle内容
apply plugin: 'com.android.library' android { compileSdk 34 buildToolsVersion = '34.0.0' namespace "my.package.name.android" defaultConfig { minSdkVersion 19 targetSdkVersion 34 } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } task copyAndroidNatives() { file("libs/armeabi/").mkdirs(); file("libs/armeabi-v7a/").mkdirs(); file("libs/x86/").mkdirs(); file("libs/arm64-v8a/").mkdirs() file("libs/x86_64/").mkdirs() configurations.natives.files.each { jar -> def outputDir = null if (jar.name.endsWith("natives-armeabi-v7a.jar")) outputDir = file("libs/armeabi-v7a") if (jar.name.endsWith("natives-x86.jar")) outputDir = file("libs/x86") if (jar.name.endsWith("natives-arm64-v8a.jar")) outputDir = file("libs/arm64-v8a") if (jar.name.endsWith("natives-x86_64.jar")) outputDir = file("libs/x86_64") if (outputDir != null) { copy { from zipTree(jar) into outputDir include "*.so" } } } } tasks.whenTaskAdded { packageTask -> if (packageTask.name.contains("package")) { packageTask.dependsOn 'copyAndroidNatives' } }
解决方案
1. 修改copyAndroidNatives任务执行时机
原任务在配置阶段就会读取configurations.natives.files,导致Gradle提前解压依赖到build目录,clean时文件被锁定。将任务改为仅在执行阶段处理依赖:
task copyAndroidNatives(type: Copy) { // 声明输出目录,让Gradle自动管理缓存 def libsDir = file("libs") outputs.dir libsDir doFirst { // 仅在执行阶段创建目录、读取依赖 ["armeabi-v7a", "x86", "arm64-v8a", "x86_64"].each { arch -> file("$libsDir/$arch").mkdirs() } configurations.natives.files.each { jar -> def arch = jar.name.split("-").find { it.matches("armeabi-v7a|x86|arm64-v8a|x86_64") } if (arch) { from zipTree(jar) { include "*.so" } into "$libsDir/$arch" } } } }
2. 调整任务依赖逻辑
替换原whenTaskAdded代码,精准绑定到Android库的打包任务,避免无差别触发:
// 绑定到库打包任务 tasks.named("packageLibrary") { dependsOn copyAndroidNatives } // 兼容debug/release变体的打包任务 tasks.withType(com.android.build.gradle.tasks.PackageLibrary) { dependsOn copyAndroidNatives }
3. 给clean任务添加强制清理逻辑
确保clean时释放锁定文件:
clean.doFirst { // 强制删除锁定文件 def lockFile = file("build/tmp/.cache/expanded/expanded.lock") if (lockFile.exists()) { lockFile.delete() } }
4. 升级libGdx版本
检查libGdx版本是否适配Gradle 8.x,新版本的libGdx模板已优化native复制逻辑,从根源避免资源占用问题。
内容的提问来源于stack exchange,提问作者Bero
相关产品推荐
相关产品推荐

