【问题标题】:Android, Espresso instrumented tests work only from Android 5.0+Android,Espresso 仪器测试仅适用于 Android 5.0+
【发布时间】:2017-06-26 15:17:24
【问题描述】:

我有许多 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)

【问题讨论】:

  • 你能分享你的 gradle 文件吗?
  • 我把 build.gradle 放到我的帖子中(上图)

标签: android android-espresso


【解决方案1】:

你可以尝试修改你的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

【讨论】:

    【解决方案2】:

    这是 google 的一个已知报告错误: https://issuetracker.google.com/issues/37324038

    总而言之,如果您的测试 apk 不适合单个文件,则 API

    确实,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.** { *; }
    

    【讨论】:

    • 在我将 Gradle 更新到 4.0 后,所有 Espresso 检测测试都开始成功(没有 multiDexEnabled = true)。
    • 那么我想你受到了这种行为变化的影响,它可能已修复:“它适用于 2.3.1,因为我们不合并单个 dex 文件,因此每个测试依赖项都会单独进行 dexed,并且打包在测试 APK 中。在 2.4.0-alpha7 中,我们将所有测试依赖项合并到一个 DEX 文件中,在这种情况下“issuetracker.google.com/issues/37324038#comment16
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多