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

Posted ianduin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 
 
 

以上是关于android gradle打包常见问题及解决方案的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio 打包及引用 aar

Android Studio下项目构建的Gradle配置及打包应用变体

利用jenkins+gradle完成Android项目打包

Android Studio 打包 jar 及 aar 包

Android Studio 打包 jar 及 aar 包

Android Studio 打包 jar 及 aar 包