使用Proguard进行会议室数据库检测测试失败

时间:2018-07-03 11:56:55

标签: android unit-testing android-room android-instrumentation

我有这个测试:

@RunWith(AndroidJUnit4.class)
public class TestDataDaoTest {

    private ApplicationDatabase dataBase;
    private TestDataDao testDataDao;

    @Before
    public void initDb() {
        dataBase = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(),
                ApplicationDatabase.class)
                // allowing main thread queries, just for testing
                .allowMainThreadQueries()
                .build();
        testDataDao = dataBase.testDataDao();
    }

    @After
    public void tearDown() {
        dataBase.close();
    }

    @Test
    public void someTest() {

    }
}

运行此测试时,我得到了NoSuchMethodError异常:

java.lang.NoSuchMethodError: No virtual method close()V in class Lcom/example/example/database/ApplicationDatabase; or its super classes (declaration of 'com.example.example.database.ApplicationDatabase' appears in /data/app/com.example.exampleModule-2/base.apk)
at com.example.example.database.dao.TestDataDaoTest.tearDown(Unknown Source)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(Unknown Source)
at org.junit.internal.runners.model.ReflectiveCallable.run(Unknown Source)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(Unknown Source)
at android.support.test.internal.runner.junit4.statement.RunAfters.evaluate(Unknown Source)
at org.junit.runners.ParentRunner.runLeaf(Unknown Source)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(Unknown Source)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(Unknown Source)
at org.junit.runners.ParentRunner$3.run(Unknown Source)
at org.junit.runners.ParentRunner$1.schedule(Unknown Source)
at org.junit.runners.ParentRunner.runChildren(Unknown Source)
at org.junit.runners.ParentRunner.access$000(Unknown Source)
at org.junit.runners.ParentRunner$2.evaluate(Unknown Source)
at org.junit.runners.ParentRunner.run(Unknown Source)
at android.support.test.runner.AndroidJUnit4.run(Unknown Source)
at org.junit.runners.Suite.runChild(Unknown Source)
at org.junit.runners.Suite.runChild(Unknown Source)
at org.junit.runners.ParentRunner$3.run(Unknown Source)
at org.junit.runners.ParentRunner$1.schedule(Unknown Source)
at org.junit.runners.ParentRunner.runChildren(Unknown Source)
at org.junit.runners.ParentRunner.access$000(Unknown Source)
at org.junit.runners.ParentRunner$2.evaluate(Unknown Source)
at org.junit.runners.ParentRunner.run(Unknown Source)
at org.junit.runner.JUnitCore.run(Unknown Source)
at org.junit.runner.JUnitCore.run(Unknown Source)
at android.support.test.internal.runner.TestExecutor.execute(Unknown Source)
at android.support.test.runner.AndroidJUnitRunner.onStart(Unknown Source)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1886)

我认为这与proguard有关,所以这是我的proguard配置:

等级:

    buildTypes {
        debug {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
            testProguardFile 'proguard-test-rules.pro'
            proguardFile 'proguard-project.txt'
        }
}

proguard-test-rules.pro:

    # Proguard rules that are applied to your test apk/code.
-ignorewarnings

-keepattributes *Annotation*

-dontnote junit.framework.**
-dontnote junit.runner.**

-dontwarn android.test.**
-dontwarn android.support.test.**
-dontwarn org.junit.**
-dontwarn org.hamcrest.**
-dontwarn com.squareup.javawriter.JavaWriter

-keepclassmembers class android.arch.** { *; }
-keep class android.arch.** { *; }
# Uncomment this if you use Mockito
#-dontwarn org.mockito.**

proguard-project.txt:

-keep class com.fasterxml.jackson.databind.ObjectMapper {
    public <methods>;
    protected <methods>;
}
-keep class com.fasterxml.jackson.databind.ObjectWriter {
    public ** writeValueAsString(**);
}
-keepnames class com.fasterxml.jackson.** { *; }
-dontwarn com.fasterxml.jackson.databind.**
-dontwarn com.flurry.**
-dontwarn javax.**

-dontwarn okhttp3.**
-dontwarn okio.**
-dontwarn javax.annotation.**
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
-dontwarn org.joda.convert.**

-dontobfuscate
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keepattributes *Annotation*
-keepclassmembers class * {
    @org.greenrobot.eventbus.Subscribe *;
}

无法从调试版本中删除proguard和minify。
有任何想法吗?
谢谢!

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,并且能够通过设置我的proguard项目规则来解决此问题:

发布Android.X迁移

select results.*
from my_spec.my_func(2, 2366800) json,
     lateral json_to_recordset(json)
          as results (col_a         TEXT,
                      col_b         TEXT,
                      col_c         NUMERIC(10,2));

Android.X之前的版本

-keep class androidx.room.**{
    public protected private *;
}

这可能有点超出范围,但我将其放在proguard规则文件中,仅在调试版本中使用,而不在我们的发行版本中使用。请注意,将其放在您的proguard-test-rules.pro文件中还不够,它需要在主proguard文件中。