从gradle运行Robolectric测试会产生OutOfMemoryError。来自AndroidStudio

时间:2016-02-26 18:25:30

标签: android gradle robolectric

如果我从AndroidStudio运行我的Robolectric单元测试,一切都很好,我的所有测试都通过了。如果我尝试从命令行运行它们,“./ gradlew clean test”我得到OutOfMemoryError错误。

我已经尝试在项目的根目录中编辑gradle.properties文件,但这似乎没有任何效果。我甚至将内存限制设置为8g并且仍然存在内存错误,因此我认为文件没有任何影响。

项目 - > gradle.properties

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# https://stackoverflow.com/questions/17012619/android-studio-gradle-issue-outofmemoryerror-permgen-space
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

栈跟踪

objc[18543]: Class JavaLaunchHelper is implemented in both     /Library/Java/JavaVirtualMachines/jdk1.7.0_72.jdk/Contents/Home/bin/java and /Library/Java/JavaVirtualMachines/jdk1.7.0_72.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.

com.foo.android.ui.MapControllerFragmentTest > testSomething FAILED
java.lang.NoClassDefFoundError at MapControllerFragmentTest.java:92
    Caused by: java.lang.ClassNotFoundException at MapControllerFragmentTest.java:92

com.foo.android.ui.MapFragmentTest > testGoogleMapListenersRemovedOnDestroy FAILED
java.lang.OutOfMemoryError

com.foo.android.ui.MapFragmentTest > testLifecyclesPassedToMapView FAILED
java.lang.NoClassDefFoundError at MapFragmentTest.java:37

Unexpected exception thrown.
java.lang.OutOfMemoryError: PermGen space
at sun.misc.Unsafe.defineClass(Native Method)
at sun.reflect.ClassDefiner.defineClass(ClassDefiner.java:63)
at sun.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:399)
at sun.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:396)
at java.security.AccessController.doPrivileged(Native Method)
at sun.reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:395)
at sun.reflect.MethodAccessorGenerator.generateSerializationConstructor(MethodAccessorGenerator.java:113)
at sun.reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java:331)
at java.io.ObjectStreamClass.getSerializableConstructor(ObjectStreamClass.java:1376)

我试过answers from other threads但没有成功。当涉及Activity或Fragment时,它似乎只会抛出错误。

1 个答案:

答案 0 :(得分:0)

欧根是对的。一旦我更新到JDK 8,我的内存不足错误就消失了。

有了这个问题,我留下了一个问题,每个人都面临着从库中运行Robolectric:它没有找到资源。很多人建议复制所有文件。对我来说,就像在gradle中设置src目录一样简单

android {
....
    sourceSets {
    // a work around for robolectric
    // https://github.com/robolectric/robolectric/issues/1796
    test.java.srcDirs += '../App/build/generated/source/r/debug'
}