NoClassDefFoundError using Jackson 2.2.x on Android with Gradle - android

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.

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

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

Related

Execution failed for task ':app:compileDebugKotlin'

I recently updated my kotlin and kotlin extensions plugin and while building I am getting the following error. I tried clean build or sync project with gradle but nothing works
e: Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
Error:Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details
The build.gradle is as follows
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
buildscript {
ext.kotlin_version = '1.0.0-beta-1038'
ext.anko_version = '0.7.2'
ext.android_support_version = '23.0.1'
ext.android_extensions_version = '1.0.0-beta-1038'
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
}
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.xxxxxxxxxx.app"
minSdkVersion 18
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
debug.java.srcDirs += 'build/generated/src/main/debug'
release.java.srcDirs += 'build/generated/src/main/release'
}
dexOptions {
preDexLibraries = false
javaMaxHeapSize "4g"
}
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
}
repositories {
jcenter()
mavenCentral()
jcenter { url "https://jitpack.io" }
}
dependencies {
provided fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'io.realm:realm-android:0.83.0'
compile 'com.github.PhilJay:MPAndroidChart:v2.1.3'
compile 'com.braintreepayments.api:braintree:1.+'
compile('com.mikepenz:materialdrawer:4.4.1#aar') {
transitive = true
}
compile 'com.mikepenz:google-material-typeface:1.2.0.1#aar'
compile project(':temperature')
compile project(':heart')
compile project(':lungs')
compile "com.android.support:cardview-v7:$android_support_version"
compile 'com.github.wendykierp:JTransforms:3.1'
compile 'fuel:fuel:0.57'
compile "org.jetbrains.anko:anko-sdk15:$anko_version"
compile "org.jetbrains.anko:anko-support-v4:$anko_version"
compile 'org.apache.directory.studio:org.apache.commons.io:2.4'
compile 'com.google.android.gms:play-services:8.1.0'
compile "com.android.support:recyclerview-v7:$android_support_version"
compile "com.android.support:appcompat-v7:$android_support_version"
compile "com.android.support:support-v4:$android_support_version"
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
can someone point out what is the issue with this?
Thanks
I solved this problem by building the project in terminal then run app in intellij(or android studio).
gradle clean build -> run app in ide
For me, the solution was to open the Gradle Console window in Android Studio and run the build with a StackTrace.
Then, reading through that, I realised that the new of doing some things in Kotlin required my code to change, but a normal Gradle build didn't catch those problems.
It was things like views being cast to TextView or whatever the case might be, that was not relevant anymore, and had to be changed to the findVieById format. e.g.:
val textView = snackbarView.findViewById(R.id.snackbar_text) as TextView
had to be changed to
val textView = snackbarView.findViewById<TextView>(R.id.snackbar_text)
In the case of a project I was recently working on, I was getting the same kind of error, which was caused by a call to a static method that no longer existed.
MessageService.java
public class MessageService extends FirebaseMessagingService {
/*
This method was commented out
private static String GetReviewTopic(String userId)
{
return "/topics/"+userId+"/review";
}
*/
}
AppActivity.java
#Override
public void onCreate() {
...
/*
No complication error was caused despite the
method not being defined.
*/
MessageService.GetReviewTopic(currentUser);
...
}
Unfortunately Android Studio wasn't reporting a "syntax error" for the missing static method, nor were there any meaningful complication errors that helped me track the issue down.
Hope this answer is able to save someone a few hours of troubleshooting!

Android Studio cannot find JUnit package

I just can't seem to get a test file to find Junit4. I've been trying forever and I'm just frustrated. It says it can't find symbol junit under org. Everything I've googled (for days), and even the android docs say this is how to do it. What am I doing wrong?
Here is my test class
import org.junit.Test;
import android.app.Application;
public class ApplicationTest {
// #Test
public ApplicationTest() {
}
}
Here is my build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
//buildToolsVersion "21.1.2"
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "stuff.morestuffs"
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:21.0.3'
compile files('libs/gson-2.3.1.jar')
testCompile 'junit:junit:4.12'
}
heres my project build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
I'm using Android Studio 1.4
For unit tests (JVM tests) the root source directory should be test, not androidTest as for Android instrument tests.
Under "Build Variants", I had "Android Instrument Tests" selected. When I changed this to "Unit Tests", Android Studio correctly recognized my file.
I solved this problem when I added following dependency to build.gradle:
androidTestCompile 'junit:junit:4.12'
If you have to test more than some separate methods and you want to test your Activity or Fragments you have to add it.

Android app is only running when compiled using older maven repository

I`m working on a project that compiles correctly but crashes on start when I set new maven repository on build.gradle file.
The project is using the lib holoeverywhere, and it crashes with the error: java.lang.NoClassDefFoundError: android.support.v4.app._HoloFragmentInflater$1
Until last month, everything was working fine, but suddenly the older company repository went offline and we created a new one. Now, if I set the older repository on my build.gradle file the app compiles and runs successfully, but if I remove the older repository, it still compiles but when I try to open it crash.
The main problem with that, is that new people trying to compile the code for the first time, using new or older repo, gets the crash on start.
So, I think this can be caused by some cache made by gradle, but looking at the .gradle folder I couldn't find anything.
Can some one help me with that?
Here is the build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
allprojects {
repositories {
mavenCentral()
maven {
url "http://<new_repository_url>/artifactory/simple/libs-release-local/"
}
// if I remove comment from these lines the project runs correctly
//maven {
//url "https://<old_repository_url>/content/groups/AndroidPublicRepository/"
//}
maven {
url "http://holoeverywhere.cf/repo"
}
}
}
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion '19.1'
defaultConfig {
minSdkVersion 9
targetSdkVersion 19
versionCode 131
versionName '1.5'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
signingConfig signingConfigs.release
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
packagingOptions {
exclude 'META-INF/license.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/NOTICE.txt'
}
android {
lintOptions {
abortOnError false
}
}
productFlavors {
}
}
configurations {
all*.exclude(group: 'org.springframework', module: 'spring-core')
all*.exclude(group: 'org.springframework', module: 'spring-web')
all*.exclude(group: 'org.hamcrest', module: 'hamcrest-core')
}
dependencies {
compile 'com.android.support:support-v4:19.1.0'
compile 'com.android.support:support-v13:19.1.0'
compile 'org.holoeverywhere:library:2.1.0'
compile 'com.viewpagerindicator:viewpagerindicator:2.4.1'
compile 'com.googlecode.libphonenumber:libphonenumber:5.9'
compile 'de.keyboardsurfer.android.widget:crouton:1.8.4'
compile 'com.google.code.gson:gson:2.3'
compile 'com.octo.android.robospice:robospice:1.4.14'
compile 'com.octo.android.robospice:robospice-cache:1.4.14'
compile 'com.octo.android.robospice:robospice-spring-android:1.4.14'
compile 'org.springframework.social:spring-social-core:1.0.2.PATCHED'
compile('org.springframework.android:spring-android-auth:1.0.1.RELEASE') {
exclude group: 'org.springframework.social', module: 'spring-social-core'
}
compile project(':addon-preferences-2.1.0')
}
I recommend you to remove HoloEverywhere from your app. It was a great library, but it isn't now. It's not very difficult: just change imports, some little adjustments and minSdkVersion = 15. Other option: Material Design.
Anyway, if you can't remove it or you can't do it right now:
HoloEverywhere had a dependency with a patched support-v4. I think the problem is that you don't have this patched version at your maven repository. You can find this code in the branch support-library.

Gradle aar library dependecy in module: failed to resolve

I'm a newbie with gradle and I'm having a dependecy problem. I have the follow project structure:
-MyApp
-MyAppLibrary
-MyAppPro
-MyAppFree
-ThirdPartyLibraryWrapper
--libs\ThirdPartyLibrary.aar
Both MyAppPro and MyAppFree depend on MyAppLibrary, which depends on ThirdPartyLibraryWrapper. As the name suggests, ThirdPartyLibraryWrapper is a wrapper on an external library, namely ThirdPartyLibrary.aar.
This is my configuration:
build.gradle MyAppPro
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.example"
minSdkVersion 8
targetSdkVersion 22
}
buildTypes {
release {
minifyEnabled true
proguardFiles 'proguard.cfg'
}
}
}
dependencies {
compile project(':MyAppLibrary')
}
build.gradle MyAppLibrary
apply plugin: 'com.android.library'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 8
targetSdkVersion 22
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
buildTypes {
release {
minifyEnabled true
proguardFiles 'proguard.cfg'
}
}
}
dependencies {
compile project(':ThirdPartyLibraryWrapper')
compile 'com.squareup.picasso:picasso:2.5.2'
}
build.gradle ThirdPartyLibraryWrapper
apply plugin: 'com.android.library'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 8
targetSdkVersion 22
}
buildTypes {
release {
minifyEnabled true
proguardFiles 'proguard.cfg'
}
}
}
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile(name: 'ThirdPartyLibrary-0.1.0', ext: 'aar')
compile "com.android.support:support-v4:22.0.0"
compile fileTree(dir: 'libs', include: 'volley.jar')
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
}
When gradle sync completes, I have got this error:
MyApp/MyAppFre/ build.gradle: failed to resolve ThirdPartyLibrary-0.1.0
MyApp/MyAppLibrary/ build.gradle: failed to resolve ThirdPartyLibrary-0.1.0
MyApp/MyAppPro/ build.gradle: failed to resolve ThirdPartyLibrary-0.1.0
Can someone help me figure out where is the issue?
The other projects are seeing that the :ThirdPartyLibraryWrapper project depends on an artifact called ThirdPartyLibrary-0.1.0:aar. Java (and Android) libraries do not bundle their own dependencies together - instead, they simply publish a list of their dependencies. The consuming project is then responsible for loading not only the library it directly depends on, but all of the libraries that library depends on.
The net effect of this is that :MyAppFree is loading in :ThirdPartyLibraryWrapper, then seeing that :ThirdPartyLibraryWrapper depends on ThirdPartyLibrary-0.1.0:aar and so thus trying to load that in as well. However, :MyAppFree doesn't know where ThirdPartyLibrary-0.1.0:aar lives.. and so it fails.
The solution will be to place similar repositories blocks in all your other projects. Try this:
repositories {
flatDir {
dirs project(':ThirdPartyLibraryWrapper').file('libs')
}
}
Using the project(...).file(...) method will free you from having to hardcode paths, and will instead use the Gradle DSL to resolve the filesystem path by looking up the project and having it do the resolution dynamically.

Gradle only downloads .aar.asc from Maven Central

I freshly deployed an Android library named TypedPreferences. I used it in another Android project for some days. Suddenly, it stopped working - dependencies cannot be found any longer. I noticed that Gradle only downloads one file when I clean the project:
Download http://repo1.maven.org/maven2/info/metadude/android/typed-preferences/ \
1.0.0/typed-preferences-1.0.0.aar.asc
These are build files of my pet project:
build.gradle:
// Top-level build file where you can add configuration
// options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
}
}
allprojects {
repositories {
mavenCentral()
maven {
url "${System.env.HOME}/.m2/repository"
}
maven {
url "https://github.com/novoda/public-mvn-repo/raw/master/releases"
}
}
}
app/build.gradle:
apply plugin: 'android'
apply plugin: 'idea'
idea {
module {
downloadJavadoc = true
downloadSources = true
}
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
}
dependencies {
compile 'com.android.support:support-v4:19.0.+'
compile 'com.android.support:appcompat-v7:19.0.+'
compile 'com.squareup.okhttp:okhttp:1.3.+'
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.+'
compile 'com.novoda:sqliteprovider-core:1.0.+'
compile 'com.androidmapsextensions:android-maps-extensions:2.1.+'
compile 'com.google.android.gms:play-services:3.2.+'
compile 'info.metadude.android:typed-preferences:1.0.0'
}
As you can see, I also enable Gradle to look into my local Maven cache here:
maven {
url "${System.env.HOME}/.m2/repository"
}
I deleted the relevant folders to avoid Gradle loading stuff from there.
There might be a misconfiguration in the build.gradle files of the library - please find them here:
Library files
Please tell me also whether I can test your fix locally without deploying to Maven Central.
Looks like the packaging element of the published POM has the wrong value. It should be aar, not aar.asc.
Also, you always can force the type of artifact to download. Just add dependency like:
compile "group:artifact:version#type"
And in your case it will be
compile "info.metadude.android:typed-preferences:1.0.0#aar"
That's it.

Categories

Resources