1

I have many Espresso instrumented tests. And they are working fine on Android 5.0.

Here is my 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'
    }
}

But if I try to start the Espresso tests on Android 4.3 I get error:

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)
stamanuel
  • 3,731
  • 1
  • 30
  • 45
Alex
  • 1,857
  • 4
  • 17
  • 34

2 Answers2

0

Can you try to modify this part of your gradle file

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

And add:

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

Generally this error happens if references are added multiple times. See here, here or here.

stamanuel
  • 3,731
  • 1
  • 30
  • 45
0

This is a known reported bug at google: https://issuetracker.google.com/issues/37324038

To summarize, multidex is not supported completely (yet) on API <21 if your test apk does not fit in a single file, this will come eventually incorporated in the Android toolings, multidex 1.0.2 already has this support but as google dev said:

Indeed, 1.0.2 contains all the necessary support, but the support in the plugin is missing. I will try to get the plugin changes published in the next few preview releases, but I cannot give any hard guarantees when it will happen.

This is still not in current release: android gradle 3.0.0 alpha8 plugin.

Just to make sure this is not related to this issue: Android Espresso multidex fail

Define a multidex keep proguard file and put in there every class that is not found in your main dex file of the .apk

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

Your multidex proguard file should contain classes which are not being found in the main dex file, in my case it was RxJavaPlugin, so my multidexproguard file contains:

-keep class com.mycompany.activity.MainActivity.** { *; }
originx
  • 496
  • 6
  • 16
  • After I update Gradle to 4.0, then all Espresso instrumented tests start success (without multiDexEnabled = true). – Alex Aug 01 '17 at 16:05
  • then I suppose you were affected by this behaviour change and it was probably fixed: "It works with 2.3.1 because we do not merge the individual dex files, so every test dependency gets dexed individually, and packaged in the test APK. In 2.4.0-alpha7, we are merging all test dependencies to a DEX file, and in this case" https://issuetracker.google.com/issues/37324038#comment16 – originx Aug 01 '17 at 16:08