[英]java.lang.NoClassDefFoundError on Espresso Test when including Robolectric
I am trying to introduce Robolectric to an already existing android project. 我正在尝试将Robolectric引入到现有的android项目中。 The project already runs a slew of espresso tests.
该项目已经进行了大量的意式浓缩咖啡测试。 As soon as I try to introduce robolectric, the tests fail with
一旦我尝试引入robolectric,测试就会失败
java.lang.NoClassDefFoundError
Anyone have any thoughts? 有人有什么想法吗? I'm using the gradle file below and used the Decard project as an example on brining in Robolectric.
我使用下面的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')
}
}
ErrorLog: 错误日志:
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)
This happens when you have a class compiled into your test APK AND you app APK. 当您将一个班级编入测试APK且您的应用APK时,就会发生这种情况。
Run ./gradlew :app:dependencies and look for duplicates. 运行./gradlew:app:dependencies并查找重复项。
You may need to use "provided" instead of "compile" somewhere. 您可能需要在某些地方使用“提供”而不是“编译”。
This has been discussed in lots of places: 在许多地方对此进行了讨论:
http://www.sinking.in/blog/android-dependency-double-trouble/ 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? java.lang.IllegalAccessError:在运行测试项目时,预验证类中的类ref被解析为意外的实现?
https://www.google.com/#q=java.lang.IllegalAccessError%3A+Class+ref+in+pre-verified+class+resolved+to+unexpected+implementation 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.