NoClassDefFoundError使用杰克逊2.2。x在安卓系统上

时间:2022-04-09 18:05:24

For my Android project I set up Gradle with Jackson 2.2.x as follows:

在我的Android项目中,我与Jackson 2.2一起建立了Gradle。x如下:

// 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.+'
}

I simply use the ObjectMapper here:

我只是在这里使用ObjectMapper:

import com.fasterxml.jackson.databind.ObjectMapper;

// ...

ObjectMapper objectMapper = new ObjectMapper();
try {
    Content content = objectMapper.readValue(inputStream, Content.class);
} catch (IOException e) {
    e.printStackTrace();
}

When I run gradle installDebug and launch the relevant part of the application it crashes:

当我运行gradle installDebug并启动应用程序的相关部分时,它会崩溃:

java.lang.NoClassDefFoundError: com.fasterxml.jackson.databind.ObjectMapper

. lang。NoClassDefFoundError:com.fasterxml.jackson.databind.ObjectMapper


Thoughts:

  • I noticed that in the error message com.fasterxml.jackson... is mentioned while com.fasterxml.jackson.core is defined in build.gradle. Is there a mismatch causing the problem?
  • 我注意到在错误消息中。fasterxml.jackson…com.fasterxml.jackson时提到。core在build.gradle中定义。是否有错配导致了问题?
  • I temporarily moved the dependencies block into the android block since I found other build.gradle configurations structured this way. However it seems to make no difference.
  • 我临时将依赖项块移动到android块中,因为我找到了其他构建。等级配置就是这样构造的。然而,这似乎没有什么区别。

2 个解决方案

#1


11  

Gradle and Android don't always place nicely with dependencies(yet). Running

Gradle和Android并不能很好地处理依赖关系。运行

 gradle clean

seems to fix most problems for me.

似乎能解决我的大部分问题。

Note: If that didn't work, you can run

注意:如果不成功,你可以运行

dexdump classes.dex | grep 'Class descriptor'

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).

在APK的类。dex文件上。这将检查类是否包含在class .dex文件中。(有时候,如果你想仔细检查一下,这是很有用的)。

#2


3  

Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.app.test"
        minSdkVersion 9
        targetSdkVersion 22
        versionCode 4
        versionName "1.3"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
    }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')  
    compile 'com.android.support:appcompat-v7:22.2.0'  `enter code here`
    compile 'com.fasterxml.jackson.core:jackson-databind :2.5.3'

}

Add permission and dependency into you Gradle and then build gradle you will get Object wrapper class

向渐变中添加权限和依赖项,然后构建渐变,您将获得对象包装类

#1


11  

Gradle and Android don't always place nicely with dependencies(yet). Running

Gradle和Android并不能很好地处理依赖关系。运行

 gradle clean

seems to fix most problems for me.

似乎能解决我的大部分问题。

Note: If that didn't work, you can run

注意:如果不成功,你可以运行

dexdump classes.dex | grep 'Class descriptor'

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).

在APK的类。dex文件上。这将检查类是否包含在class .dex文件中。(有时候,如果你想仔细检查一下,这是很有用的)。

#2


3  

Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.app.test"
        minSdkVersion 9
        targetSdkVersion 22
        versionCode 4
        versionName "1.3"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
    }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')  
    compile 'com.android.support:appcompat-v7:22.2.0'  `enter code here`
    compile 'com.fasterxml.jackson.core:jackson-databind :2.5.3'

}

Add permission and dependency into you Gradle and then build gradle you will get Object wrapper class

向渐变中添加权限和依赖项,然后构建渐变,您将获得对象包装类