1

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.

4
  • Any time you mention an exception is being throw, post the stack trace. Feb 26, 2016 at 20:24
  • Do you have jdk7? I think AS has JDK8 incorporated. Can you upgrade your JDK? Mar 1, 2016 at 7:19
  • Thanks. That actually fixes the memory errors. I'm just left with the java.lang.ClassNotFoundException. 1 error is better than 2
    – whizzle
    Mar 1, 2016 at 18:19
  • p.s. which is just the standard problem everyone has when they have a library (not an application) which uses robolectric. I should be able to get it from here.
    – whizzle
    Mar 1, 2016 at 18:46

1 Answer 1

0

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'
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.