Android,Espresso检测测试仅适用于Android 5.0+

时间:2017-06-26 15:17:25

标签: android android-espresso

我有许多Espresso仪器测试。它们在Android 5.0上运行良好。

这是我的build.gradle:

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'io.fabric'
repositories {
    maven { url 'https://maven.fabric.io/public' }
    mavenCentral()
}
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    dexOptions {
        jumboMode = true
    }
    defaultConfig {
        applicationId "com.mycompany"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 3
        versionName "1.2.3"
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["resourcePackageName": android.defaultConfig.applicationId]
            }
        }
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled = true
    }
    testBuildType "dev"
    variantFilter { variant ->
        if (variant.buildType.name.equals('debug')) {
            variant.setIgnore(true);
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        dev {
            initWith debug
            applicationIdSuffix ".dev"
        }
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
    lintOptions {
        abortOnError false
    }
}
def AAVersion = '4.3.0'
dependencies {
    annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.volley:volley:1.0.0'
    compile 'com.baoyz.swipemenulistview:library:1.3.0'
    compile('com.crashlytics.sdk.android:crashlytics:2.6.0@aar') {
        transitive = true;
    }
    compile('com.digits.sdk.android:digits:1.11.0@aar') {
        transitive = true;
    }
    compile "org.androidannotations:androidannotations-api:$AAVersion"
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    compile 'com.google.android.gms:play-services-gcm:11.0.1'
    compile 'com.google.code.gson:gson:2.7'
    compile 'com.miguelcatalan:materialsearchview:1.4.0'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.7.3'
    compile 'com.squareup.okhttp:okhttp:2.7.3'
    compile 'com.theartofdev.edmodo:android-image-cropper:2.2.5'
    compile 'commons-codec:commons-codec:1.9'
    compile 'commons-io:commons-io:2.4'
    compile 'org.apache.commons:commons-lang3:3.4'
    compile 'org.apache.httpcomponents:httpcore:4.4.4'
    compile 'org.apache.httpcomponents:httpmime:4.3.6'
    compile 'org.jetbrains.anko:anko-sdk15:0.9.1'
    compile 'us.feras.mdv:markdownview:1.1.0'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':module1')
    compile project(':module2')
    testCompile 'junit:junit:4.12'
    androidTestCompile 'junit:junit:4.12'
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2') {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude group: 'com.google.guava', module: 'guava'
        exclude group: 'junit', module: 'junit'
    }
}

但如果我尝试在Android 4.3上启动Espresso测试,我会收到错误:

java.lang.NoClassDefFoundError: com/mycompany/activity/MainActivity$1
    Caused by: java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
    at dalvik.system.DexFile.defineClass(Native Method)
    at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:211)
    at dalvik.system.DexPathList.findClass(DexPathList.java:313)
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:51)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)

2 个答案:

答案 0 :(得分:0)

您可以尝试修改gradle文件的这一部分

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2') {
    exclude group: 'com.android.support', module: 'support-annotations'
    exclude group: 'com.google.guava', module: 'guava'
    exclude group: 'junit', module: 'junit'
}

并添加:

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2') {
    exclude group: 'com.android.support', module: 'support-annotations'
    exclude group: 'com.google.guava', module: 'guava'
    exclude group: 'junit', module: 'junit'
    exclude group: 'com.android.support', module: 'appcompat'
    exclude group: 'com.android.support', module: 'support-v4'
}

通常,如果多次添加引用,则会发生此错误。请参阅hereherehere

答案 1 :(得分:0)

这是Google报道的已知错误: https://issuetracker.google.com/issues/37324038

总而言之,如果你的测试apk不适合单个文件,那么在API< 21上不完全支持multidex,这最终会包含在Android工具中,multidex 1.0.2已经有了这个支持但是正如谷歌开发者所说:

  

确实,1.0.2包含所有必要的支持,但缺少插件中的支持。我将尝试在接下来的几个预览版本中发布插件更改,但是当它发生时我无法提供任何硬性保证。

这仍然不是当前版本:android gradle 3.0.0 alpha8插件。

只是为了确保这与此问题无关: Android Espresso multidex fail

定义一个multidex keep proguard文件并在那里放入在.apk的主dex文件中找不到的每个类

 debug {
            applicationIdSuffix '.debug'
            multiDexKeepProguard file('../proguardRules/multidex-proguard.pro')
        }

你的multidex proguard文件应该包含在主dex文件中找不到的类,在我的例子中它是RxJavaPlugin,所以我的multidexproguard文件包含:

-keep class com.mycompany.activity.MainActivity.** { *; }