10

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

I simply use the ObjectMapper here:

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:

java.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?
  • 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.
JJD
  • 50,076
  • 60
  • 203
  • 339
  • 1
    Can you unzip the APK and run `dexdump classes.dex | grep 'Class descriptor'` and see if Lcom/fasterxml/jackson/databind/ObjectMapper ? – Ethan Oct 09 '13 at 00:16
  • @Ethan No, the class descriptor for `ObjectMapper` is **not** contained in the `classes.dex` of `Foobar-debug-unaligned.apk`. – JJD Oct 09 '13 at 07:34
  • 1
    can you do a `gradle clean installDebug`, see if that fails, if it does post the output of `gradle dependencies` – Ethan Oct 09 '13 at 15:26
  • 1
    @Ethan Found the same hint [here](http://stackoverflow.com/a/18786851/356895), simultaneously. Running `gradle clean` is the solution to the problem. Please extract your comment into an answer. Maybe combine both comments since both were helpful! – JJD Oct 09 '13 at 15:33
  • good to hear that worked for you. Comments extracted. – Ethan Oct 09 '13 at 16:02
  • Awesome. Thank you again. This was eaten a lot of my time. Have a nice week. – JJD Oct 09 '13 at 16:10
  • I have some thoughts on your thoughts. Gradle will pull in any dependency needed by your defined dependencies. You can see this by running `gradle dependencies`. The top level of the graph is your dependencies that are defined, anything lower that that (indented) are the 'transient' dependencies. There are three phases on setting up the gradle script. Link here: http://www.gradle.org/docs/current/userguide/build_lifecycle.html – Ethan Oct 10 '13 at 14:16

2 Answers2

11

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'

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

Ethan
  • 6,883
  • 3
  • 33
  • 41
  • 1
    +1 for clean, got similar issue few days back when test been failing because gradle did not assesd situation correctly. Clean FTW – peter_budo Oct 10 '13 at 07:04
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

JJD
  • 50,076
  • 60
  • 203
  • 339
Raj Kumar
  • 688
  • 8
  • 18