2

在 Android Studio 中运行 Espresso 仪器测试时遇到了很大困难。

我认为这与我的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
        ]
    }
}

当我使用此版本的 build.gradle 运行仪器测试时,出现以下错误:

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)

当我删除androidTestCompile "javax.inject:javax.inject:${javaxinjectVersion}",我得到另一个错误:

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)

非常感谢,如果有人可以给我一个提示。

4

1 回答 1

2

使用 Espresso 2.0 时,如果需要 junit,则需要更改依赖项以使用 junit-dep。

androidTestCompile 'junit:junit-dep:4.11'
于 2015-01-12T16:18:26.007 回答