从Gradle运行Robolectric测试会出现内存不足错误,在AndroidStudio中运行正常

t40tm48m  于 2023-02-23  发布在  Android
关注(0)|答案(2)|浏览(160)

如果我从AndroidStudio运行我的Robolectric单元测试,一切都很好,我的所有测试都通过了。如果我尝试从命令行运行它们,“./gradlew clean test”,我会得到OutOfMemoryError错误。
我试过编辑gradle.properties项目根目录下的www.example.com文件,但似乎没有效果。我甚至将内存限制设置为8 g,但仍然出现内存不足错误,所以我认为文件没有任何效果。
项目-〉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,但没有成功。它似乎只有在涉及活动或片段时才会抛出错误。

nwnhqdif

nwnhqdif1#

Eugen是正确的。一旦我更新到JDK 8,我的内存不足错误就消失了。
修复后,我只剩下1个问题,每个人都面临着从图书馆运行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'
}
quhf5bfb

quhf5bfb2#

答案应该是(至少对我来说):
在build.gradle(项目:xxx)中,添加以下内容:

subprojects {
  tasks.withType(Test) {
    maxParallelForks = 2
    forkEvery = 80
    maxHeapSize = "2048m"
    minHeapSize = "1024m"
  }
}

参考来源:https://github.com/robolectric/robolectric/issues/5131

相关问题