繁体   English   中英

包含Robolectric时在Espresso Test上的java.lang.NoClassDefFoundError

[英]java.lang.NoClassDefFoundError on Espresso Test when including Robolectric

我正在尝试将Robolectric引入到现有的android项目中。 该项目已经进行了大量的意式浓缩咖啡测试。 一旦我尝试引入robolectric,测试就会失败

java.lang.NoClassDefFoundError

有人有什么想法吗? 我使用下面的gradle这个文件,并使用Decard项目作为在Robolectric盐渍的例子。

apply plugin: 'android'
apply plugin: 'robolectric'
apply from : "$rootDir/gradle/signing.gradle"
apply from : "$rootDir/gradle/build_extras.gradle"
apply from : "$rootDir/gradle/environments.gradle"

loadConfiguration()

apply from : "$rootDir/gradle/checkstyle.gradle"
apply from : "$rootDir/gradle/pmd.gradle"
apply from : "$rootDir/gradle/findbugs.gradle"
apply from : "$rootDir/gradle/espresso.gradle"

android {
    compileSdkVersion 19
    buildToolsVersion '19.1'

    useOldManifestMerger true

    defaultConfig {
        applicationId ‘com.example.app'
        minSdkVersion 14
        targetSdkVersion 19
        versionCode getVersionCode()
        testInstrumentationRunner 'com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner'
    }

    packagingOptions {
        exclude 'LICENSE.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
    }

    lintOptions {
        abortOnError true
    }

    jacoco {
        version = '0.6.2.201302030002'
    }

    sourceSets {

        androidTest {
            setRoot('src/test')
        }

        debug.setRoot('build-types/debug')

    }

    buildTypes {
        debug {
            applicationIdSuffix '.debug'
            debuggable true
            testCoverageEnabled false
        }
    }

    applicationVariants.all { variant ->
        removeSetAnimationScaleFromManifest(variant)

        task("generate${variant.name}Javadoc", type: Javadoc) {
            description "Generates Javadoc for $variant.name."
            group "Code Quality"

            def Properties localProperties = loadPropertiesFile('../local.properties')

            source = variant.javaCompile.source
            classpath = files(variant.javaCompile.classpath.files) +
                    files(localProperties['sdk.dir'] + "/platforms/android-4.4.2/android.jar")
        }

        copyAndRenameZipAlignedPackage(variant)
    }
}

apply plugin: 'monkey'

monkey {
    teamCityLog = false
    eventCount = 5000
    seed = 42
    failOnFailure = false
    install = false
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile project(':SlidingMenuLibrary')

    compile 'com.android.support:support-v4:19.1.+'
    compile 'com.google.android.gms:play-services:5.0.77'
    compile 'org.apache.james:apache-mime4j:0.6'
    compile 'org.apache.httpcomponents:httpmime:4.2.3'
    compile 'org.jsoup:jsoup:1.6.3'
    compile 'com.nineoldandroids:library:2.4.0'
    compile 'com.googlecode.ez-vcard:ez-vcard:0.9.3'
    compile 'com.googlecode.libphonenumber:libphonenumber:5.9'

    // Espresso
    provided 'com.jakewharton.espresso:espresso:1.1-r3'
    androidTestCompile ('com.jakewharton.espresso:espresso:1.1-r3') {

    }
    androidTestCompile 'com.google.guava:guava:14.0.1'
    androidTestCompile 'com.squareup.dagger:dagger:1.1.0'
    androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
    //androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
    androidTestCompile 'org.hamcrest:hamcrest-library:1.1'

    androidTestCompile('junit:junit:4.11') {
        exclude module: 'hamcrest-core'
    }
    androidTestCompile('org.robolectric:robolectric:2.3') {
        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.+'
}

apply plugin: 'idea'

idea {
    module {
        testOutputDir = file('build/test-classes/debug')
    }
}

错误日志:

java.lang.NoClassDefFoundError: com/example/android/ui/GettingStarted

...

Caused by: java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
at dalvik.system.DexFile.defineClassNative(Native Method)
at dalvik.system.DexFile.defineClass(DexFile.java:222)
at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:215)
at dalvik.system.DexPathList.findClass(DexPathList.java:322)
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)

当您将一个班级编入测试APK且您的应用APK时,就会发生这种情况。

运行./gradlew:app:dependencies并查找重复项。

您可能需要在某些地方使用“提供”而不是“编译”。

在许多地方对此进行了讨论:

http://www.sinking.in/blog/android-dependency-double-trouble/

java.lang.IllegalAccessError:在运行测试项目时,预验证类中的类ref被解析为意外的实现?

https://www.google.com/#q=java.lang.IllegalAccessError%3A+Class+ref+in+pre-verified+class+resolved+to+unexpected+implementation

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM