最近升级了unity,直接跨版本更新到了2018,工程随之遇到了问题,StreamAssets文件超标的问题,遍寻各大国内外帖子博客也没有找到解决方法。好一阵折腾,特此记录分享下。先上错误码:
CommandInvokationFailure: Gradle build failed.
C:/Program Files/Java/jdk1.8.0_171\bin\java.exe -classpath "D:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\lib\gradle-launcher-4.0.1.jar" org.gradle.launcher.GradleMain "-Dorg.gradle.jvmargs=-Xmx2048m" "assembleRelease"
stderr[
FAILURE: Build failed with an exception.
* Where:
Build file 'F:\project\IlRuntime\ILRuntimeU3D-master\ILRuntimeDemo\Temp\gradleOut\build.gradle'
* What went wrong:
Could not compile build file 'F:\project\IlRuntime\ILRuntimeU3D-master\ILRuntimeDemo\Temp\gradleOut\build.gradle'.
> startup failed:
General error during class generation: 1866
java.lang.ArrayIndexOutOfBoundsException: 1866
at org.codehaus.groovy.classgen.asm.CallSiteWriter.getCreateArraySignature(CallSiteWriter.java:58)
at org.codehaus.groovy.classgen.asm.CallSiteWriter.makeCallSite(CallSiteWriter.java:317)
at org.codehaus.groovy.classgen.asm.InvocationWriter.makeCachedCall(InvocationWriter.java:307)
at org.codehaus.groovy.classgen.asm.InvocationWriter.makeCall(InvocationWriter.java:397)
at org.codehaus.groovy.classgen.asm.InvocationWriter.makeCall(InvocationWriter.java:104)
at org.codehaus.groovy.classgen.asm.InvocationWriter.makeInvokeMethodCall(InvocationWriter.java:88)
at org.codehaus.groovy.classgen.asm.InvocationWriter.writeInvokeMethod(InvocationWriter.java:464)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitMethodCallExpression(AsmClassGenerator.java:771)
at org.codehaus.groovy.ast.expr.MethodCallExpression.visit(MethodCallExpression.java:66)
at org.codehaus.groovy.classgen.asm.StatementWriter.writeReturn(StatementWriter.java:590)
at org.codehaus.groovy.classgen.asm.OptimizingStatementWriter.writeReturn(OptimizingStatementWriter.java:324)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitReturnStatement(AsmClassGenerator.java:620)
at org.codehaus.groovy.ast.stmt.ReturnStatement.visit(ReturnStatement.java:49)
at org.codehaus.groovy.classgen.asm.StatementWriter.writeBlockStatement(StatementWriter.java:85)
at org.codehaus.groovy.classgen.asm.OptimizingStatementWriter.writeBlockStatement(OptimizingStatementWriter.java:159)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitBlockStatement(AsmClassGenerator.java:570)
at org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:71)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClassCodeContainer(ClassCodeVisitorSupport.java:104)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructorOrMethod(ClassCodeVisitorSupport.java:115)
很明显,问题就是越界了,标记了 java.lang.ArrayIndexOutOfBoundsException: 1866,这正是StreamAssets下资源数量。原因不明,找到了org.codehaus.groovy.classgen.asm.CallSiteWriter.getCreateArraySignature 直接翻看源码如下:
private static String [] sig = new String [255]; | |
private static String getCreateArraySignature(int numberOfArguments) { | |
if (sig[numberOfArguments] == null) { | |
StringBuilder sb = new StringBuilder("("); | |
for (int i = 0; i != numberOfArguments; ++i) { | |
sb.append("Ljava/lang/Object;"); | |
} | |
sb.append(")[Ljava/lang/Object;"); | |
sig[numberOfArguments] = sb.toString(); | |
} | |
return sig[numberOfArguments]; | |
} |
原来groovy已经把数组定义成了最大255。问题就是这样,修改groovy类库是不可能的了,那就是个深坑,还是不要动的好。那如何解决这个问题?
传入getCreateArraySignature 函数的参数其实是build.gradle的属性【aapOptions】的数量,这里记录的值就是1866个啊,太多了,
我不需要把每个文件都记录一遍,我只需按后缀分类就可以了。
修改步骤如下:
1.使用自定义gradle template
在unity工程里依次: Build Setting-->Publishing Setting-->Build --Custom Gradle Template勾选上,
这时候会自动生成Plugins/Android下mainTemplate,打开这个文件,清爽了许多,少了一大串的文件名。
2.修改mainTemplate
aaptOptions {
noCompress '.unity3d', '.ress', '.resource', '.obb'**STREAMING_ASSETS**
}
添加我们自己的类型后缀,比如我的资源后缀名是 '.bundle'.
aaptOptions {
noCompress '.unity3d', '.ress', '.resource', '.obb','.bundle'
}
ok,再次打包,提示打包成功。
好了,下班了,就写到这里,有说的不对的地方还请指正,敬谢。