简体   繁体   中英

Running Robolectric test from gradle gives OutOfMemoryError. Fine from AndroidStudio

If I run my Robolectric unit tests from AndroidStudio everything is just fine and all my tests pass. If I try to run them from the command line, "./gradlew clean test" I get OutOfMemoryError errors.

I've tried editing the gradle.properties file in the root directory of the project, but this seems to have no effect. I even set the memory limit to 8g and still had out of memory errors so I don't think file is have any effects.

Project -> 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

stacktrace

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)

I have tried the answers from other threads with no success. It only seems to throw errors when an Activity or Fragment is involved.

Eugen was correct. Once I updated to JDK 8 my out of memory errors went away.

With that fixed, I was left with the 1 issue that everyone faces with running Robolectric from a library: it doesn't find the resources. Many people suggest copying all the files over. For me it was as simple as setting the src directory in gradle

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM