【问题标题】:java.lang.NoClassDefFoundError on Espresso Test when including Robolectric包含 Robolectric 时 Espresso 测试中的 java.lang.NoClassDefFoundError
【发布时间】:2014-08-01 20:50:16
【问题描述】:

我正在尝试将 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)

【问题讨论】:

  • 请粘贴错误日志以查看类名。
  • @Sachin Thapa - 完成。请注意,它在抱怨我的项目中名为 GettingStarted 的类。从 gradle 中删除 Robolectric 可以修复该错误。不知道为什么。

标签: android robolectric android-espresso


【解决方案1】:

当您将一个类编译到您的测试 APK 并且您应用 APK 时,就会发生这种情况。

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

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

这已经在很多地方讨论过:

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

java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation getting while running test project?

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

【讨论】:

  • 我能够通过从与 espresso 测试相同的文件夹中删除 robolectric 测试来进行修复。将 robolectric 测试粘贴在 app 文件夹之外(使用 gradle 文件夹结构)。但是,我将此标记为正确答案,因为这些链接有助于回答这个特定问题。
猜你喜欢
  • 2018-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-13
相关资源
最近更新 更多