0

当我运行 Android 仪器测试时,为什么会出现此异常?

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());

它可能与番石榴有关,但我不知道如何正确包含和/或排除它。

提前谢谢了!

4

1 回答 1

0

首先,Espresso 2.0 已经发布,升级的原因有很多(其中包括 Android L 支持).. 但这不是您的问题。

它看起来不像你在保护你的调试版本,所以这不应该是问题。

看起来您确实从众多软件包中排除了番石榴。如果你跑

./gradlew dependencies

在项目目录的命令行中,您是否看到任何地方都包含了番石榴?似乎它应该来自您包含 otto 和 dagger (因为排除行在 dagger 中被注释掉了)。

于 2015-01-15T04:35:54.803 回答