0

Почему я получаю это исключение, когда запускаю тестовый тест для Android?Тест на инструментальные средства для Android: java.lang.RuntimeException: java.lang.NoClassDefFoundError: com.google.common.base.Optional

30416-30566/com.example.myapp I/TestRunner﹕ java.lang.RuntimeException: java.lang.NoClassDefFoundError: com.google.common.base.Optional 
     at com.google.android.apps.common.testing.ui.espresso.base.ThreadPoolExecutorExtractor.getAsyncTaskThreadPool(ThreadPoolExecutorExtractor.java:54) 
     at com.google.android.apps.common.testing.ui.espresso.base.BaseLayerModule.provideSdkAsyncTaskMonitor(BaseLayerModule.java:83) 
     at com.google.android.apps.common.testing.ui.espresso.base.BaseLayerModule$$ModuleAdapter$ProvideSdkAsyncTaskMonitorProvidesAdapter.get(BaseLayerModule$$ModuleAdapter.java:326) 
     at com.google.android.apps.common.testing.ui.espresso.base.BaseLayerModule$$ModuleAdapter$ProvideSdkAsyncTaskMonitorProvidesAdapter.get(BaseLayerModule$$ModuleAdapter.java:290) 
     at dagger.internal.Linker$SingletonBinding.get(Linker.java:364) 
     at com.google.android.apps.common.testing.ui.espresso.base.UiControllerImpl$$InjectAdapter.get(UiControllerImpl$$InjectAdapter.java:64) 
     at com.google.android.apps.common.testing.ui.espresso.base.UiControllerImpl$$InjectAdapter.get(UiControllerImpl$$InjectAdapter.java:19) 
     at dagger.internal.Linker$SingletonBinding.get(Linker.java:364) 
     at com.google.android.apps.common.testing.ui.espresso.base.BaseLayerModule$$ModuleAdapter$ProvideUiControllerProvidesAdapter.get(BaseLayerModule$$ModuleAdapter.java:176) 
     at com.google.android.apps.common.testing.ui.espresso.base.BaseLayerModule$$ModuleAdapter$ProvideUiControllerProvidesAdapter.get(BaseLayerModule$$ModuleAdapter.java:140) 
     at com.google.android.apps.common.testing.ui.espresso.ViewInteraction$$InjectAdapter.get(ViewInteraction$$InjectAdapter.java:67) 
     at com.google.android.apps.common.testing.ui.espresso.ViewInteraction$$InjectAdapter.get(ViewInteraction$$InjectAdapter.java:19) 
     at dagger.ObjectGraph$DaggerObjectGraph.get(ObjectGraph.java:272) 
     at com.google.android.apps.common.testing.ui.espresso.Espresso.onView(Espresso.java:51) 
     at com.example.espresso.myapp.filtering.MyTest.testSomething(MyTest.java:51) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:525) 
     at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214) 
     at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199) 
     at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192) 
     at junit.framework.TestCase.runBare(TestCase.java:134) 
     at junit.framework.TestResult$1.protect(TestResult.java:115) 
     at junit.framework.TestResult.runProtected(TestResult.java:133) 
     at junit.framework.TestResult.run(TestResult.java:118) 
     at junit.framework.TestCase.run(TestCase.java:124) 
     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191) 
     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176) 
     at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554) 
     at com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.onStart(GoogleInstrumentationTestRunner.java:167) 
     at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1738) 
Caused by: java.lang.NoClassDefFoundError: com.google.common.base.Optional 
     at com.google.android.apps.common.testing.ui.espresso.base.ThreadPoolExecutorExtractor$5.call(ThreadPoolExecutorExtractor.java:135) 
     at com.google.android.apps.common.testing.ui.espresso.base.ThreadPoolExecutorExtractor$5.call(ThreadPoolExecutorExtractor.java:130) 
     at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
     at com.google.android.apps.common.testing.ui.espresso.base.ThreadPoolExecutorExtractor$1.run(ThreadPoolExecutorExtractor.java:76) 
     at android.os.Handler.handleCallback(Handler.java:730) 
     at android.os.Handler.dispatchMessage(Handler.java:92) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:5419) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:525) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209) 
     at com. 

Это мой build.gradle:

apply plugin: 'com.android.application' 
apply plugin: 'com.neenbedankt.android-apt' 
apply plugin: 'android' 
apply plugin: 'robolectric' 

android { 
    compileSdkVersion 21 
    buildToolsVersion "21.1.0" 

    defaultConfig { 
     applicationId "com.example.myapp" 
     minSdkVersion 18 
     targetSdkVersion 21 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner" 
     testApplicationId 'com.example.myapptest' 
     testHandleProfiling true 
     testFunctionalTest true 
     multiDexEnabled true 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    sourceSets { 
     androidTest { 
      setRoot('src/test') 
     } 
    } 

    packagingOptions { 
     exclude 'META-INF/LICENSE.txt' 
     exclude 'LICENSE.txt' 
     exclude 'META-INF/LICENSE' 
     exclude 'META-INF/NOTICE' 
     exclude 'META-INF/NOTICE.txt' 
    } 
    dexOptions { 
     jumboMode true 
    } 

} 

robolectric { 
    include '**/*Test.class' 
    exclude '**/espresso/**/*Test.class' 
} 

configurations { 
    apt 
} 

apt { 
    arguments { 
     // resourcePackageName android.defaultConfig.packageName 
     resourcePackageName 'com.example.myapp' 
     androidManifestFile variant.outputs[0].processResources.manifestFile 
    } 
} 

ext { 
    daggerVersion = '1.2.2'; 
    androidAnnotationsVersion = '3.2'; 
    robobindingVersion = '0.8.9'; 
    jodatimeVersion = '2.5.1'; 
    ormliteVersion = '4.48'; 
    ottoVersion = '1.3.5'; 
    commonsioVersion = '2.0.1'; 
    playservicesVersion = '6.1.71'; 
    supportv4Version = '21.0.2'; 
    javaxinjectVersion = '1'; 
    junitVersion = '4.11'; 
    robolectricVersion = '2.4'; 
} 

dependencies { 
    repositories { 
     mavenCentral() 
    } 

    androidTestCompile('com.jakewharton.espresso:espresso:1.1-r3') { 
     exclude group: 'com.squareup.dagger' 
     exclude group: 'com.google.guava', module: 'guava' 
     exclude group: 'com.android.support' 
     exclude group: 'org.hamcrest', module: 'hamcrest-integration' 
    } 
    androidTestCompile ('com.jakewharton.espresso:espresso-support-v4:1.1-r3') { 
     exclude group: 'com.squareup.dagger' 
     exclude group: 'com.google.guava', module: 'guava' 
     exclude group: 'com.android.support' 
     exclude group: 'org.hamcrest', module: 'hamcrest-integration' 
    } 

    androidTestCompile("junit:junit:${junitVersion}") { 
     exclude group: 'org.hamcrest' 
    } 

    androidTestCompile('com.squareup:fest-android:1.0.7') { 
     exclude group: 'com.android.support' 
    } 

    // androidTestCompile group: 'com.google.guava', name: 'guava', version: '16.0' 

    androidTestCompile("org.robolectric:robolectric:${robolectricVersion}") { 
     exclude module: 'classworlds' 
     exclude module: 'commons-logging' 
     exclude module: 'httpclient' 
     exclude module: 'maven-artifact' 
     exclude module: 'maven-artifact-manager' 
     exclude module: 'maven-error-diagnostics' 
     exclude module: 'maven-model' 
     exclude module: 'maven-project' 
     exclude module: 'maven-settings' 
     exclude module: 'plexus-container-default' 
     exclude module: 'plexus-interpolation' 
     exclude module: 'plexus-utils' 
     exclude module: 'wagon-file' 
     exclude module: 'wagon-http-lightweight' 
     exclude module: 'wagon-provider-api' 
    } 

    compile 'com.android.support:multidex:1.0.0' 
    apt("org.robobinding:codegen:$robobindingVersion") { 
     exclude group: 'com.google.guava', module: 'guava' 
    } 
    compile("org.robobinding:robobinding:$robobindingVersion:with-dependencies") { 
     exclude group: 'com.google.guava', module: 'guava' 
    } 
    apt "org.androidannotations:androidannotations:${androidAnnotationsVersion}" 
    compile "org.androidannotations:androidannotations-api:${androidAnnotationsVersion}" 
    apt("com.squareup.dagger:dagger-compiler:${daggerVersion}") { 
     // exclude group: 'com.google.guava', module: 'guava' 
    } 
    compile "com.squareup.dagger:dagger:${daggerVersion}" 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile "com.google.android.gms:play-services:${playservicesVersion}" 
    compile "com.android.support:support-v4:${supportv4Version}" 
    compile "com.squareup:otto:${ottoVersion}" 
    compile "javax.inject:javax.inject:${javaxinjectVersion}" 
    compile "com.j256.ormlite:ormlite-core:${ormliteVersion}" 
    compile "com.j256.ormlite:ormlite-android:${ormliteVersion}" 
    compile group: 'commons-io', name: 'commons-io', version: "${commonsioVersion}" 
    compile "net.danlew:android.joda:${jodatimeVersion}" 
} 

apply plugin: 'idea' 
idea { 
    module { 
     testOutputDir = file('build/test-classes/debug') 
    } 
} 

android.applicationVariants.all { variant -> 
    def aptOutput = file("${project.buildDir}/generated/source/apt/${variant.dirName}") 
    println "****************************" 
    println "variant: ${variant.name}" 
    println "manifest: ${variant.outputs[0].processResources.manifestFile}" 
    println "aptOutput: ${aptOutput}" 
    println "****************************" 

    variant.javaCompile.doFirst { 
     println "*** compile doFirst ${variant.name}"  
     aptOutput.mkdirs() 
     variant.javaCompile.options.compilerArgs += [ 
       '-processorpath', configurations.apt.getAsPath(), 
       '-AandroidManifestFile=' + variant.outputs[0].processResources.manifestFile, 
       '-s', aptOutput 
     ] 
    } 
} 

И это линия в моем коде, где брошено исключение:

onView(withId(R.id.a_button)).perform(click()); 

Это может иметь что-то делать с гуавы , но я не знаю, как включать и/или исключать его должным образом.

Большое спасибо!

ответ

0

Прежде всего, Espresso 2.0 отсутствует, и есть множество причин для обновления (в том числе поддержка Android L) .. но это не ваша проблема.

Не похоже, что вы выполняете сборку отладки, так что это не должно быть проблемой.

Похоже, что вы исключаете гуаву из многочисленных пакетов. Если вы запустите

./gradlew dependencies 

в командной строке в каталоге проектов, вы видите, что guava включен где-нибудь? Похоже, что это должно произойти от вашего включения отто и кинжала (так как строка исключений прокомментирована в кинжале).

+0

Да, спасибо. Тем временем я переключился на Espresso 2.0 и удалил зависимость JUnit. Затем он работал в основном - не всегда, но большую часть времени. –