2017-02-15 34 views
4

我正在嘗試爲我的項目運行Instrumental測試。但它們不能在版本低於5(API 21)的設備(仿真器)上運行。Android測試不能在低於5的設備上運行(API 21)NoClassDefFoundError

我一直在試圖解決這個問題,但仍然面臨着這個問題。 我得到以下異常。

02-15 10:46:08.965 1127-1143/? E/AndroidRuntime: FATAL EXCEPTION: Instr: android.support.test.runner.AndroidJUnitRunner 
               java.lang.ExceptionInInitializerError 
                at android.support.test.internal.runner.TestRequestBuilder.<init>(TestRequestBuilder.java:81) 
                at android.support.test.internal.runner.TestRequestBuilder.<init>(TestRequestBuilder.java:524) 
                at android.support.test.runner.AndroidJUnitRunner.createTestRequestBuilder(AndroidJUnitRunner.java:379) 
                at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:352) 
                at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:269) 
                at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584) 
                Caused by: java.lang.NoClassDefFoundError: org.junit.runner.manipulation.Filter$1 
                at org.junit.runner.manipulation.Filter.<clinit>(Filter.java:21) 
                at android.support.test.internal.runner.TestRequestBuilder.<init>(TestRequestBuilder.java:81)  
                at android.support.test.internal.runner.TestRequestBuilder.<init>(TestRequestBuilder.java:524)  
                at android.support.test.runner.AndroidJUnitRunner.createTestRequestBuilder(AndroidJUnitRunner.java:379)  
                at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:352)  
                at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:269)  
                at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)  

我已經嘗試過類似問題的所有解決方案。

這裏是我的項目結構的外觀。

project structure

而且gradle這個依賴於測試

/************* TEST STUFF ***************/ 
    androidTestCompile 'junit:junit:4.12' 
    androidTestCompile fileTree(include: ['robotium-solo-5.5.2.jar'], dir: 'libs') 
    //mockito dependencies 
    androidTestCompile 'org.mockito:mockito-core:2.7.6' 
    androidTestCompile files('libs/dexmaker-mockito-1.0.jar') 
    androidTestCompile files('libs/dexmaker-1.0.jar') 

    // Set this dependency to build and run Espresso tests 
    androidTestCompile('com.android.support.test.espresso:espresso-core:+') { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    } 

    androidTestCompile('com.android.support.test:runner:0.5') { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    } 
    // Set this dependency to use JUnit 4 rules 
    androidTestCompile('com.android.support.test:rules:+') { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    } 
    androidTestCompile('com.android.support.test.espresso:espresso-intents:+') { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    } 
    //MockWebServer - Version 2.2 of mockwebserver doesn't work because of an issue, so forcing v2.1 - https://github.com/square/okhttp/issues/1069 
    androidTestCompile('com.squareup.okhttp3:mockwebserver:+') { 
     exclude module: 'okhttp' 
    } 
    androidTestCompile "org.slf4j:slf4j-api:1.7.12" 
    //WireMock 
    androidTestCompile("com.github.tomakehurst:wiremock:2.5.0") { 
     //Using Android Version Instead 
     exclude group: 'org.apache.httpcomponents', module: 'httpclient' 

     //Version conflict with our app's slf4j version 
     exclude group: 'org.slf4j', module: 'slf4j-api' 

     //Was getting a classpath conflict for org.objectweb.asm.AnnotationVisitor which is a part of 'net.minidev:asm' 
     exclude group: 'org.ow2.asm', module: 'asm' 

     //Was getting this warning, so decided to ignore this version included by WireMock. 
     //Warning:Dependency org.json:json:20090211 is ignored as it may be conflicting with the internal version provided by Android. 
     //In case of problem, please repackage with jarjar to change the class packages 
     exclude group: 'org.json', module: 'json' 
    } 
    androidTestCompile 'org.apache.httpcomponents:httpclient-android:4.3.5.1' 

而且BaseTestClass具有以下結構

@RunWith(AndroidJUnit4.class) 
public abstract class InstrumentalSuperTest { 

    private SystemAnimations mSystemAnimations; 

    @Rule 
    public IntentsTestRule rule = provideActivity(); 

    @Rule 
    public WireMockRule wireMockRule = new WireMockRule(WireMockConfiguration.wireMockConfig().port(BuildConfig.PORT), false); 

    protected abstract IntentsTestRule provideActivity(); 

} 

而且這種情況不僅與我的項目,但Wiremock examples爲好,同樣的錯誤。

也許我運行測試不正確,我只需點擊一個測試課,然後選擇Run ... Test

請幫忙解決這個問題,我不知道什麼是錯的。

+0

是'provideActivity()'一個自定義方法嗎?你能提供它的內容嗎? – sebokopter

+0

可能是重複的:https://stackoverflow.com/questions/40867407/android-espresso-multidex-fail 嘗試定義 在 multiDexKeepProguard文件( '../ proguardRules/multidex-proguard.pro')你的風格配置和文件中使用proguard類似的語法,以保持在你的測試中找不到的類,multidex是一個有時在測試運行中得到正確的惡魔 – originx

+0

在Lollipop之前的測試Instrumentation APK不能夠多重dex,即使你試試。 WireMock有一個巨大的方法計數足跡,我的猜測是你的測試APK是多指標。第一個dex文件將被拾取,但是後續dex文件中的任何內容都將不可用,並且如果您嘗試訪問其他dex文件中的任何類,將會得到ClassNotFoundException。 –

回答

0

你可以嘗試更新版本的wiremock。晚於2.0.8-beta。 更多關於這個看看這個stackoverflow answer

相關問題