How to fix ''Gradle Build Failed'' on Unity?

问题描述 投票:0回答:5

我正在尝试构建我的 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**
c# unity3d gradle build build.gradle
5个回答
9
投票

这是因为

android.enableR8
已被弃用。要从您的
gradle.properties
文件中删除它,您必须转到Project Settings>Player>Android(选项卡)>Other Settings>并检查Custom Gradle Properties Template

之后,一个

gradleTemplate.properties
文件生成在:

Assets/Plugins/Android/gradleTemplate.properties

在此文件中,删除行

android.enableR8
,或将其注释掉,现在进行新的构建。

这个技巧应该有用。

或者,导出为 Android 项目,然后在 Android Studio 中更改它。在 Android Studio 中,您可以更好地控制这些事情。


0
投票

安装新版Unity,就可以了

可以用Unity 2020.3.33f1版本,我用的就是这个,很好用

记得设置Preferences:Unity默认JDK、Gradle、NDK


0
投票

我在很多地方都看到了,这个问题真的很痛苦。就我而言,我的问题是我的 csproj 中没有我的旧 CSharp 界面不正确,所以我在 Visual Studio 中看不到。

由于Unity不关心csproj,它在编译时使用它。我刚刚注意到,因为控制台窗口中显示了一个不存在的 java 问题(由于 Android,CSharp 被转换为 Java,因此 Graddle 引发了这个问题)。

尝试在你的 CSharp 程序中找到一些没有出现在 Visual Studio 的解决方案资源管理器中的类,并删除或修复它。

另外,我遵循了 Jaimin 上面指出的修复方法。它对我不起作用,但我就这样离开了。它更有意义。


0
投票

试试这个 对于 Windows 前往地点

“C:\Program Files\Unity\Hub\Editor�1.1.24f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK uild-tools .0.0”

找到一个名为 d8.bat 的文件。这是一个 Windows 批处理文件。

将 d8.bat 重命名为 dx.bat.

在文件夹 lib 中(“C:\ProgramFiles\Unity\Hub\Editor�1.1.24f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK uild-tools .0.0\lib”)

将 d8.jar 重命名为 dx.jar 请记住 AppData 是一个隐藏文件夹。打开隐藏项以查看 AppData 文件夹。

现在运行你的项目...

来自ELNexs https://forum.unity.com/threads/gradle-error-problem-with-gradle-build.538320/


0
投票

我通过迁移到 Unity 2022.2 简单地解决了这个问题

© www.soinside.com 2019 - 2024. All rights reserved.