[英]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. 如果我从AndroidStudio运行Robolectric单元测试,那么一切都很好,并且所有测试都通过了。 If I try to run them from the command line, "./gradlew clean test" I get OutOfMemoryError errors.
如果尝试从命令行“ ./gradlew clean test”运行它们,则会收到OutOfMemoryError错误。
I've tried editing the gradle.properties file in the root directory of the project, but this seems to have no effect. 我已经尝试在项目的根目录中编辑gradle.properties文件,但这似乎没有效果。 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.
我什至将内存限制设置为8g,但仍然存在内存不足错误,因此我认为文件没有任何影响。
Project -> gradle.properties 项目-> 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.
更新到JDK 8后,我的内存不足错误消失了。
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. 修复此问题后,我面临着每个人都面临着从库运行Robolectric遇到的第1个问题:它找不到资源。 Many people suggest copying all the files over.
许多人建议将所有文件复制过来。 For me it was as simple as setting the src directory in gradle
对我来说,这就像在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'
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.