欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

unity 编译游戏包时,报错:gradle build failed class too large

程序员文章站 2022-06-02 20:13:07
...

1. 报了什么错


我们在使用 Gradle 方式出包时,经常会出现如下的错误,导致总是无法正确地出包

DisplayProgressbar: Building Gradle project
CommandInvokationFailure: Gradle build failed. 
/Applications/Unity/PlaybackEngines/AndroidPlayer/Tools/OpenJDK/MacOS/bin/java -classpath "/Applications/Unity/PlaybackEngines/AndroidPlayer/Tools/gradle/lib/gradle-launcher-5.1.1.jar" org.gradle.launcher.GradleMain "-Dorg.gradle.jvmargs=-Xmx4096m" "assembleDebug"

stderr[

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/zhoutaosheng/Documents/DistributeProject/Game/Temp/gradleOut/build.gradle'

* What went wrong:
Could not compile build file '/Users/zhoutaosheng/Documents/DistributeProject/Game/Temp/gradleOut/build.gradle'.
> startup failed:
  General error during class generation: Class too large: _BuildScript_$_run_closure3$_closure9
  
  groovyjarjarasm.asm.ClassTooLargeException: Class too large: _BuildScript_$_run_closure3$_closure9
  	at groovyjarjarasm.asm.ClassWriter.toByteArray(ClassWriter.java:549)
  	at org.codehaus.groovy.control.CompilationUnit$18.call(CompilationUnit.java:860)
  	at org.codehaus.groovy.control.CompilationUnit$18.call(CompilationUnit.java:875)
  	at org.codehaus.groovy.control.CompilationUnit$18.call(CompilationUnit.java:875)
  	at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1095)
  	at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:649)
  	at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:627)
  	at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:604)
  	...

2. 为什么会出这样的错


出现这样的错,其原因是:groovy 函数会使用 /${UNITY_DIR}/AndroidPlayer/Tools/GrandleTemplates/mainTemplate.gradleaaptOptions.noCompress 来填充数组时,要求数组长度小于 255。对于 STREAMING_ASSETS,它会将 Streaming Assets 下的文件一个一个地添加到数组中,如此数组很容易就超过了。原始的 mainTemplate.gradle 如下所示:

aaptOptions {
        noCompress '.unity3d', '.ress', '.resource', '.obb'**STREAMING_ASSETS**
    }

3. 怎么解决?


解决这个问题的办法很简单,把 Streaming Assets 下的文件类型抄到这个数组,而不是一个个加进去。比如,Streaming Assets 下有 .awb,.bundle 类型的数据,将 mainTemplate.gradle 改成如下就能解决问题:

aaptOptions {
        noCompress '.unity3d', '.ress', '.resource', '.obb', ,'awb', '.bundle'
    }


不想改 unity 的设置,只针对当前工程修改,可以在 Android 的 PlayerSettings,使用自定义的 Gradle Template

unity 编译游戏包时,报错:gradle build failed class too large

相关标签: 爬坑日记