Unity 5.6.x Gradle build APK报错解决方法
发表于2018-07-10
问题描述
公司有一个海外项目需要接入 Admob 并聚合多家广告联盟,同时需要接入 Facekbook 相关服务。由于 Google Play Service 的基础库比较多,加入其它广告SDK后,遇到了 64K问题,安卓代码中方法数量超过了 65536 个,导致 Unity 默认的打包方式 Internal 无法打包成功。
为了解决 64K问题,必须改用 Gradle 方式打包。经过测试,选择 Export Project 导出 Gradle Project 并用 Android Studio 打开,是可以成功编译出 APK 的,但是在 Unity 中直接 Build, 就会报出如下错误:
CommandInvokationFailure: Gradle build failed. D:/SDK/Java/jdk1.8.0_171\bin\java.exe -classpath "D:\Program Files\Unity565f1\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\lib\gradle-launcher-4.0.1.jar" org.gradle.launcher.GradleMain "assembleRelease" stderr[ FAILURE: Build failed with an exception. * What went wrong: A problem occurred configuring root project 'gradleOut'. > Could not resolve all files for configuration ':classpath'. > Could not resolve com.android.tools.build:gradle:2.1.0. Required by: project : > Could not resolve com.android.tools.build:gradle:2.1.0. > Could not get resource 'https://jcenter.bintray.com/com/android/tools/build/gradle/2.1.0/gradle-2.1.0.pom'. > Could not HEAD 'https://jcenter.bintray.com/com/android/tools/build/gradle/2.1.0/gradle-2.1.0.pom'. > Connect to 127.0.0.1:80 [/127.0.0.1] failed: Connection refused: connect * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. BUILD FAILED in 8s ] stdout[ Starting a Gradle Daemon (subsequent builds will be faster) ] exit code: 1 UnityEditor.Android.Command.Run (System.Diagnostics.ProcessStartInfo psi, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) UnityEditor.Android.GradleWrapper.RunJava (System.String args, System.String workingdir, UnityEditor.Android.Progress progress) Rethrow as GradleInvokationException: Gradle build failed UnityEditor.Android.GradleWrapper.RunJava (System.String args, System.String workingdir, UnityEditor.Android.Progress progress) UnityEditor.Android.GradleWrapper.Run (System.String workingdir, System.String task, UnityEditor.Android.Progress progress) UnityEditor.Android.PostProcessor.Tasks.BuildGradleProject.Execute (UnityEditor.Android.PostProcessor.PostProcessorContext context) UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context) UnityEditor.HostView:OnGUI()
解决方法尝试
1.修改 gradle.properties
观察报错内容,其中有 > Connect to 127.0.0.1:80 [/127.0.0.1] failed: Connection refused: connect,第一反映是网络代理问题,于是找到 系统盘符:\Users\管理员用户名\.gradle 文件夹中的 gradle.properties 文件,将其中的代理相关设置,用 # 注释掉。
# systemProp.http.proxyHost=127.0.0.1 # systemProp.http.proxyPort=80 # systemProp.https.proxyHost=127.0.0.1 # systemProp.https.proxyPort=80
2.替换 gralde\lib
经过以上修改,在一般环境下,就已经可以成功打包了,但是也遇到了任然无法打包的情况,报错如下:
CommandInvokationFailure: Gradle build failed. D:/SDK/Java/jdk1.8.0_171\bin\java.exe -classpath "D:\Program Files\Unity565f1\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\lib\gradle-launcher-4.0.1.jar" org.gradle.launcher.GradleMain "assembleRelease" stderr[ FAILURE: Build failed with an exception. * Where: Build file 'E:\Unity3D\TTAiLaoyu\Temp\gradleOut\build.gradle' line: 20 * What went wrong: A problem occurred evaluating root project 'gradleOut'. > Failed to apply plugin [id 'com.android.application'] > Gradle version 2.10 is required. Current version is 4.0.1. If using the gradle wrapper, try editing the distributionUrl in E:\Unity3D\TTAiLaoyu\Temp\gradleOut\gradle\wrapper\gradle-wrapper.properties to gradle-2.10-all.zip * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. BUILD FAILED in 16s ] stdout[ ] exit code: 1 UnityEditor.Android.Command.Run (System.Diagnostics.ProcessStartInfo psi, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) UnityEditor.Android.GradleWrapper.RunJava (System.String args, System.String workingdir, UnityEditor.Android.Progress progress) Rethrow as GradleInvokationException: Gradle build failed UnityEditor.Android.GradleWrapper.RunJava (System.String args, System.String workingdir, UnityEditor.Android.Progress progress) UnityEditor.Android.GradleWrapper.Run (System.String workingdir, System.String task, UnityEditor.Android.Progress progress) UnityEditor.Android.PostProcessor.Tasks.BuildGradleProject.Execute (UnityEditor.Android.PostProcessor.PostProcessorContext context) UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context) UnityEditor.HostView:OnGUI()
经过测试,类似结构的项目,在 Unity 2018.1.2f1 中可以打包成功,因此怀疑是 Unity 集成的 Gradle 版本与项目中某些设置不匹配导致的。
Unity 2018.1.2f1 中集成 Gralde 版本为 4.0.1。
Unity 5.6.51 中集成 Gralde 版本为 2.1.4。
尝试做如下修改:
1.分别找到 Unity 5.6.x 和 Unity 2018.x 的安装目录中
Unity安装目录\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\ 中的 lib 文件夹,
删掉 Unity 5.6.x 的 lib,并将 Unity 2018.x 的 lib 复制过来。
2.找到 Unity 5.6.x 的
Unity安装目录\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\GradleTemplates 文件夹,
将其中的 mainTemplate.gradle 和 libTemplate.gradle 两个文件中的 Gralde 版本号改为 2.3.0。这两个文件,分别是 Gradle 项目的 项目级 build.gradle 模板和 模块级 build.gradle 模板。
3.重新打开 Unity 5.6.x 测试打包,成功。
来自:https://blog.csdn.net/ls9512/article/details/80690910