如何在 Unity 上修复“Gradle Build Failed”?

Posted

技术标签:

【中文标题】如何在 Unity 上修复“Gradle Build Failed”?【英文标题】:How to fix ''Gradle Build Failed'' on Unity? 【发布时间】:2021-12-14 22:57:51 【问题描述】:

我正在尝试构建我的 android 应用程序一段时间,但似乎找不到任何解决此问题的方法: Image

Unity 版本:Unity 2020.3.21f1(64 位) 使用的JDK、SDK、NDK、Gradle都是Unity推荐的(其他已经试过了)。

控制台详情在这里:

Configure project :launcher
WARNING: The option setting 'android.enableR8=false' is deprecated.
It will be removed in version 5.0 of the Android Gradle plugin.
You will no longer be able to disable R8

UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)`

    FAILURE: Build failed with an exception.

    * What went wrong:
    com/sun/istack/FinalArrayList
    
    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

    CommandInvokationFailure: Gradle build failed. 
    C:\Program Files\Unity\Hub\Editor\2020.3.21f1\Editor\Data\PlaybackEngines\AndroidPlayer\OpenJDK\bin\java.exe -classpath "C:\Program Files\Unity\Hub\Editor\2020.3.21f1\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\lib\gradle-launcher-6.1.1.jar" org.gradle.launcher.GradleMain "-Dorg.gradle.jvmargs=-Xmx4096m" "assembleRelease"
    
    stderr[
    
    FAILURE: Build failed with an exception.

    Build completed with a result of 'Failed' in 60 seconds (59527 ms)
    UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

UnityEditor.BuildPlayerWindow+BuildMethodException: 4 errors
  at UnityEditor.BuildPlayerWindow+DefaultBuildMethods.BuildPlayer (UnityEditor.BuildPlayerOptions options) [0x002ca] in <208f92fc783546f9abbe643bfc23920c>:0 
  at UnityEditor.BuildPlayerWindow.CallBuildMethods (System.Boolean askForBuildLocation, UnityEditor.BuildOptions defaultBuildOptions) [0x00080] in <208f92fc783546f9abbe643bfc23920c>:0 
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

我已经尝试过 youtube 和其他论坛上的所有教程,但欢迎大家提供帮助,谢谢!

编辑 1:

在尝试了 Jaimin 的解决方案后,现在正在发生这种情况:

Assets/Plugins/Android\gradleTemplate.properties file is using a deprecated way of controlling which tool should be used to do the minification. To fix this, remove "useProguard" entries yourself. If not fixed, your build can fail.
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)


 File C:\Users\Windows\.android\repositories.cfg could not be loaded.
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

Starting a Gradle Daemon, 1 incompatible Daemon could not be reused, use --status for details

UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

FAILURE: Build failed with an exception.

* What went wrong:
com/sun/istack/FinalArrayList
> com.sun.istack.FinalArrayList

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 5s
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8

UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

CommandInvokationFailure: Gradle build failed. 
C:\Program Files\Unity\Hub\Editor\2020.3.21f1\Editor\Data\PlaybackEngines\AndroidPlayer\OpenJDK\bin\java.exe -classpath "C:\Program Files\Unity\Hub\Editor\2020.3.21f1\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\lib\gradle-launcher-6.1.1.jar" org.gradle.launcher.GradleMain "-Dorg.gradle.jvmargs=-Xmx4096m" "assembleRelease"

stderr[

FAILURE: Build failed with an exception.

* What went wrong:
com/sun/istack/FinalArrayList
> com.sun.istack.FinalArrayList

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 5s
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
]
stdout[
Starting a Gradle Daemon, 1 incompatible Daemon could not be reused, use --status for details
]
exit code: 1
UnityEditor.Android.Command.WaitForProgramToRun (UnityEditor.Utils.Program p, UnityEditor.Android.Command+WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) (at <f94168442e634613b6a5965ed17c38f4>:0)
UnityEditor.Android.Command.Run (System.Diagnostics.ProcessStartInfo psi, UnityEditor.Android.Command+WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) (at <f94168442e634613b6a5965ed17c38f4>:0)
UnityEditor.Android.Command.Run (System.String command, System.String args, System.String workingdir, UnityEditor.Android.Command+WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) (at <f94168442e634613b6a5965ed17c38f4>:0)
UnityEditor.Android.AndroidJavaTools.RunJava (System.String args, System.String workingdir, System.Action`1[T] progress, System.String error) (at <f94168442e634613b6a5965ed17c38f4>:0)
UnityEditor.Android.GradleWrapper.Run (UnityEditor.Android.AndroidJavaTools javaTools, System.String workingdir, System.String task, System.Action`1[T] progress) (at <f94168442e634613b6a5965ed17c38f4>:0)
Rethrow as GradleInvokationException: Gradle build failed
UnityEditor.Android.GradleWrapper.Run (UnityEditor.Android.AndroidJavaTools javaTools, System.String workingdir, System.String task, System.Action`1[T] progress) (at <f94168442e634613b6a5965ed17c38f4>:0)
UnityEditor.Android.PostProcessor.Tasks.BuildGradleProject.Execute (UnityEditor.Android.PostProcessor.PostProcessorContext context) (at <f94168442e634613b6a5965ed17c38f4>:0)
UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context) (at <f94168442e634613b6a5965ed17c38f4>:0)
Rethrow as BuildFailedException: Exception of type 'UnityEditor.Build.BuildFailedException' was thrown.
UnityEditor.Android.PostProcessor.CancelPostProcess.AbortBuild (System.String title, System.String message, System.Exception ex) (at <f94168442e634613b6a5965ed17c38f4>:0)
UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context) (at <f94168442e634613b6a5965ed17c38f4>:0)
UnityEditor.Android.PostProcessAndroidPlayer.PostProcess (UnityEditor.BuildTarget target, System.String stagingAreaData, System.String stagingArea, System.String playerPackage, System.String installPath, System.String companyName, System.String productName, UnityEditor.BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry, UnityEditor.Build.Reporting.BuildReport report) (at <f94168442e634613b6a5965ed17c38f4>:0)
UnityEditor.Android.AndroidBuildPostprocessor.PostProcess (UnityEditor.Modules.BuildPostProcessArgs args, UnityEditor.BuildProperties& outProperties) (at <f94168442e634613b6a5965ed17c38f4>:0)
UnityEditor.PostprocessBuildPlayer.Postprocess (UnityEditor.BuildTargetGroup targetGroup, UnityEditor.BuildTarget target, System.String installPath, System.String companyName, System.String productName, System.Int32 width, System.Int32 height, UnityEditor.BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry, UnityEditor.Build.Reporting.BuildReport report) (at <208f92fc783546f9abbe643bfc23920c>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)

Build completed with a result of 'Failed' in 127 seconds (127448 ms)
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

UnityEditor.BuildPlayerWindow+BuildMethodException: 5 errors
  at UnityEditor.BuildPlayerWindow+DefaultBuildMethods.BuildPlayer (UnityEditor.BuildPlayerOptions options) [0x002ca] in <208f92fc783546f9abbe643bfc23920c>:0 
  at UnityEditor.BuildPlayerWindow.CallBuildMethods (System.Boolean askForBuildLocation, UnityEditor.BuildOptions defaultBuildOptions) [0x00080] in <208f92fc783546f9abbe643bfc23920c>:0 
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

自定义 Gradle 属性模板文件的编码方式如下:

org.gradle.jvmargs=-Xmx**JVM_HEAP_SIZE**M
org.gradle.parallel=true
unityStreamingAssets=.unity3d**STREAMING_ASSETS**
**ADDITIONAL_PROPERTIES**

【问题讨论】:

【参考方案1】:

这是因为 'android.enableR8' 已被弃用。要从 gradle.properties 文件中删除它,您必须转到 Project Settings > Player > (Android Tab) > Other Settings > 并标记“Custom Gradle Properties Template”。

之后在“Assets/Plugins/Android/gradleTemplate.properties”处生成一个 gradleTemplate.properties 文件。

在此文件中删除“android.enableR8”行或将其注释掉,然后重新构建。

这个技巧应该有效。

导出为 android 项目,然后在 android studio 上进行更改。在 android studio 中,您可以更好地控制这些事情。

【讨论】:

您好,感谢您的回复。我试过了,现在有一些新的错误,我将它们添加到上面的问题文本中。 尝试导出android项目,然后在android studio中编辑

以上是关于如何在 Unity 上修复“Gradle Build Failed”?的主要内容,如果未能解决你的问题,请参考以下文章

如何更正我的旋转平台脚本并修复 Unity 中的错误纹理?

Unity 2019 - 如何修复 TextMeshPro 2.0 错误 cs0433

如何修复 3d 模型中的“裂缝”?

如何修复 Unity 中的“无法从复合材料中读取 Vector2 类型的值”错误?

如何在我的Mac OS的Unity Shader Graph 2018.2中修复我的问题

Unity3D游戏xlua轻量级热修复框架