android gradle打包常见问题及解决方案

冲天香阵透长安,满城尽带黄金甲。这篇文章主要讲述android gradle打包常见问题及解决方案相关的知识,希望能为你提供帮助。
背景:
【android gradle打包常见问题及解决方案】 
问题:

Q1: UNEXPECTED TOP-LEVEL ERROR: java.lang.OutOfMemoryError: Java heap space at com.android.dx.cf.direct.MethodListParser.set(MethodListParser.java:80) at com.android.dx.cf.direct.MemberListParser.parse(MemberListParser.java:217) at com.android.dx.cf.direct.MemberListParser.parseIfNecessary(MemberListParser.java:108) at com.android.dx.cf.direct.MethodListParser.getList(MethodListParser.java:54) at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:542) at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406) at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388) at com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251) at com.android.dx.command.dexer.Main.parseClass(Main.java:764) at com.android.dx.command.dexer.Main.access$1500(Main.java:85) at com.android.dx.command.dexer.Main$ClassParserTask.call(Main.java:1684) at com.android.dx.command.dexer.Main.processClass(Main.java:749) at com.android.dx.command.dexer.Main.processFileBytes(Main.java:718) at com.android.dx.command.dexer.Main.access$1200(Main.java:85) at com.android.dx.command.dexer.Main$FileBytesConsumer.processFileBytes(Main.java:1645) at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284) at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166) at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144) at com.android.dx.command.dexer.Main.processOne(Main.java:672) at com.android.dx.command.dexer.Main.processAllFiles(Main.java:569) at com.android.dx.command.dexer.Main.runMultiDex(Main.java:366) at com.android.dx.command.dexer.Main.run(Main.java:275) at com.android.dx.command.dexer.Main.main(Main.java:245) at com.android.dx.command.Main.main(Main.java:106):transformClassesWithDexForDebug FAILED :transformClassesWithDexForDebug (Thread[main,5,main]) completed. Took 5 mins 2.571 secs.FAILURE: Build failed with an exception.* What went wrong: Execution failed for task ‘:transformClassesWithDexForDebug‘. > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process ‘command ‘/g3an/ci/jdk1.7.0_79/bin/java‘‘ finished with non-zero exit value 3* Try: Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output.


原因:gradle在编译过程中栈内存溢出

解决方案:在build.gradle配置文件中增加

dexOptions { javaMaxHeapSize "2g" }

//2g表示内存大小,32位的jdk最大好像不能超过3g,超过最大允许内存大小时会报下面这个错误
Successfully started process ‘command ‘/g3an/ci/jdk1.7.0_79/bin/java‘‘ Invalid maximum heap size: -Xmx4g The specified size exceeds the maximum representable size. Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit.

  Q2:
UNEXPECTED TOP-LEVEL ERROR: java.lang.OutOfMemoryError: GC overhead limit exceeded:transformClassesWithDexForDebug FAILED :transformClassesWithDexForDebug (Thread[main,5,main]) completed. Took 1 mins 58.823 secs.FAILURE: Build failed with an exception.* What went wrong: Execution failed for task ‘:transformClassesWithDexForDebug‘. > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process ‘command ‘/g3an/ci/jdk1.7.0_79/bin/java‘‘ finished with non-zero exit value 3* Try: Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output.

原因:JVM虚拟机的内存空间太小

解决方案:在用户目录下的gradle.properties文件中指定gradle编译时使用的jvm空间大小

文件路径:/home/[user]/.gradle/wrapper/gradle.properties
在文件中增加:org.gradle.jvmargs=-Xmx2048m
   


    推荐阅读