0

Why do I get this exception, when I run an Android instrumentation test?

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.

This is my 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
        ]
    }
}

And this is the line in my code where the exception is thrown:

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

It might have something to do with guava, but I don't know how to include and/or exclude it properly.

Many thanks in advance!

tomrozb
  • 25,773
  • 31
  • 101
  • 122

1 Answers1

0

First of all, Espresso 2.0 is out and there are numerous reasons to upgrade (among them Android L support).. but that's not your problem.

It doesn't look like you're proguarding your debug build so that shouldn't be the problem.

It does look like you're excluding guava from numerous packages. If you run

./gradlew dependencies

at the command line in your project directory, do you see guava being included anywhere? Seems like it should be coming from your inclusion of otto and dagger (since the exclude line is commented out in dagger).

rob.todd
  • 71
  • 1
  • 5
  • Yes, thanks. Meanwhile I switched to Espresso 2.0 and removed the JUnit dependency. Then it worked mostly - not always, but most of the time. –  Jan 15 '15 at 12:42