2

I have heavy difficulties with running an Espresso instrumentation test in Android Studio.

I think it has something to do with my build.gradle:

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

android {
    compileSdkVersion 21
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 18
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
    }
    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'
    }
    dexOptions {
        jumboMode true
    }
}

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

configurations {
    apt
}

apt {
    arguments {
        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: 'javax.inject'
         exclude group: 'com.google.guava', module: 'guava'
    }

    androidTestCompile("junit:junit:${junitVersion}") {
        exclude module: 'hamcrest-core'
    }

    androidTestCompile("org.robobinding:robobinding:${robobindingVersion}") {
        exclude group: 'com.google.guava', module: 'guava'
    }
    androidTestCompile "com.squareup:otto:${ottoVersion}"
    androidTestCompile "com.squareup.dagger:dagger:${daggerVersion}"
    androidTestCompile "javax.inject:javax.inject:${javaxinjectVersion}"

    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'
    }

    androidTestCompile 'com.squareup:fest-android:1.0.+'

    apt "org.robobinding:codegen:$robobindingVersion"
    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}"
    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
        ]
    }
}

When I use this version of build.gradle to run the instrumentation tests, I get following error:

5508-5508/com.example.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
            at com.example.myapp.MyApplication.onCreate(MyApplication.java:26)
            at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1024)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4684)
            at android.app.ActivityThread.access$1400(ActivityThread.java:159)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1376)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            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.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
            at dalvik.system.NativeStart.main(Native Method)

When I remove androidTestCompile "javax.inject:javax.inject:${javaxinjectVersion}", I get another error:

3196-3196/com.example.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NoClassDefFoundError: com/example/myapp/MainPresentationModel
            at com.example.myapp.MainActivity.onCreate(FilterWizardActivity.java:54)
            at android.app.Activity.performCreate(Activity.java:5372)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
            at com.google.android.apps.common.testing.testrunner.GoogleInstrumentation.callActivityOnCreate(GoogleInstrumentation.java:428)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
            at android.app.ActivityThread.access$700(ActivityThread.java:159)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            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.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.myapp.MainPresentationModel" on path: /system/framework/android.test.runner.jar:/data/app/com.example.myapp.test-1.apk:/data/app/com.example.myapp-1.apk
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:64)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
            at com.example.myapp.MainActivity.onCreate(FilterWizardActivity.java:54)
            at android.app.Activity.performCreate(Activity.java:5372)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
            at com.google.android.apps.common.testing.testrunner.GoogleInstrumentation.callActivityOnCreate(GoogleInstrumentation.java:428)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
            at android.app.ActivityThread.access$700(ActivityThread.java:159)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            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.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
            at dalvik.system.NativeStart.main(Native Method)

Many thanks in advance, if someone can give me just a hint.

Akeshwar Jha
  • 4,516
  • 8
  • 52
  • 91
  • Espresso 2.0 has been released, you can find the instructions on how to use it on their website (for now). The release notes say that they resolved some dependency issues when using dagger etc alongside it. https://code.google.com/p/android-test-kit/wiki/EspressoSetupInstructions – aried3r Dec 21 '14 at 01:42
  • @aried3r: Many thanks for your reply. Unfortunately I still get errors. Current problem: "com.android.dex.DexException: Multiple dex files define Ljunit/extensions/ActiveTestSuite$1;" No idea, how to solve this. –  Dec 21 '14 at 11:41
  • 1
    Hmm, I had to remove JUnit dependency. –  Dec 21 '14 at 18:04

1 Answers1

2

When using Espresso 2.0 if you need junit you need to change your dependency to use junit-dep.

androidTestCompile 'junit:junit-dep:4.11'
Nicholas Hall
  • 161
  • 13