NoClassDefFoundError 在 Android 上使用 Jackson 2.2.x 和 Gradle [英] NoClassDefFoundError using Jackson 2.2.x on Android with Gradle
问题描述
对于我的 Android 项目,我使用 Jackson 2.2.x 设置 Gradle,如下所示:
For my Android project I set up Gradle with Jackson 2.2.x as follows:
// build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion "18.1.0"
defaultConfig {
minSdkVersion 8
targetSdkVersion 18
}
}
dependencies {
compile 'com.android.support:appcompat-v7:18.0.0'
compile 'com.android.support:support-v4:18.0.0'
compile 'com.google.android.gms:play-services:3.1.36'
compile 'com.fasterxml.jackson.core:jackson-databind:2.2.+'
compile 'com.fasterxml.jackson.core:jackson-core:2.2.+'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.2.+'
}
我只是在这里使用 ObjectMapper
:
import com.fasterxml.jackson.databind.ObjectMapper;
// ...
ObjectMapper objectMapper = new ObjectMapper();
try {
Content content = objectMapper.readValue(inputStream, Content.class);
} catch (IOException e) {
e.printStackTrace();
}
当我运行 gradle installDebug
并启动应用程序的相关部分时,它崩溃了:
When I run gradle installDebug
and launch the relevant part of the application it crashes:
java.lang.NoClassDefFoundError: com.fasterxml.jackson.databind.ObjectMapper
java.lang.NoClassDefFoundError: com.fasterxml.jackson.databind.ObjectMapper
<小时>
想法:
- 我注意到在错误消息中提到了
com.fasterxml.jackson...
而com.fasterxml.jackson.core
是在build 中定义的.gradle
.是否存在导致问题的不匹配? - 我暂时将
dependencies
块移动到android
块中,因为我找到其他以这种方式构建的build.gradle
配置.不过好像没什么区别. - I noticed that in the error message
com.fasterxml.jackson...
is mentioned whilecom.fasterxml.jackson.core
is defined inbuild.gradle
. Is there a mismatch causing the problem? - I temporarily moved the
dependencies
block into theandroid
block since I found otherbuild.gradle
configurations structured this way. However it seems to make no difference.
Thoughts:
推荐答案
Gradle 和 Android 并不总是与依赖项很好地放置在一起(目前).运行
Gradle and Android don't always place nicely with dependencies(yet). Running
gradle clean
似乎为我解决了大部分问题.
seems to fix most problems for me.
注意:如果这不起作用,您可以运行
Note: If that didn't work, you can run
dexdump classes.dex | grep 'Class descriptor'
在 APK 中的 classes.dex 文件上.这将检查该类是否包含在 classes.dex
文件中.(有时,如果您想仔细检查发生了什么,这很有用).
on the classes.dex file in the APK. That will check to see if the class is included in the classes.dex
file. (Sometimes it's useful if you want to double check whats going on).
这篇关于NoClassDefFoundError 在 Android 上使用 Jackson 2.2.x 和 Gradle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!