简体   繁体   English

从gradle运行Robolectric测试会给出OutOfMemoryError。 来自AndroidStudio的罚款

[英]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.

相关问题 从IntelliJ运行Robolectric测试会导致内存不足 - Running a Robolectric test from IntelliJ causes OutOfMemory Robolectric-Gradle插件运行单个测试 - Robolectric-Gradle plugin running single test 使用Robolectric和AppCompat运行Gradle测试会导致NullPointerException - Running Gradle test with Robolectric and AppCompat results in NullPointerException 来自gradle(AndroidStudio)的奇怪消息。 - strange message from gradle (AndroidStudio). 带有robolectric的Android Gradle测试 - Android gradle test with robolectric Robolectric 4.0.2测试未在带有NoSuchMethodError的gradle 3.2.1中运行:setUseLegacyResources(Z)V - Robolectric 4.0.2 test are not running in gradle 3.2.1 with NoSuchMethodError: setUseLegacyResources(Z)V 从Github导入项目到androidstudio(Gradle) - Import project from Github to androidstudio (Gradle) 使用Robolectric和Gradle测试资源文件夹 - Test resources folder with Robolectric and Gradle 无法从Robolectric 2单元测试中创建ActionBarActivity - Cannot create ActionBarActivity from Robolectric 2 unit test 运行单元测试时将 Android Gradle 插件从 3.5.3 更新到 3.6.1 后出现 OutOfMemoryError - Got OutOfMemoryError after update Android Gradle Plugin to 3.6.1 from 3.5.3 when run unit test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM