Search code examples
javaandroidandroid-gradle-plugindalvikandroid-annotations

Class Load Exception when using @RestService - java.lang.ClassNotFoundException (...) at dalvik.system.BaseDexClassLoader.findClass


So my project is compiling and running well unitll I inject AndroidAnnotations @RestService into my MainActivity

@EActivity(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {

@RestService
RestInterface restInterface;
...
}

MyRestClient:

@Rest(rootUrl = "http://153.19.215.46:8080/api", converters = {MappingJackson2HttpMessageConverter.class})
public interface RestInterface {

@Post("/device/rgbled/light")
    String lightLed(@Body String color);
}

Some sources led me to think it may be releated with MultiDex but it seems its not the issue. There are also some similar question already asked on stack but none of them brought me solution.

Error Stacktrace:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.przem.ihm_mobile, PID: 9844
              java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/xml/transform/stax/StAXSource;
                  at org.springframework.http.converter.xml.SourceHttpMessageConverter.<clinit>(SourceHttpMessageConverter.java:74)
                  at org.springframework.web.client.RestTemplate.<init>(RestTemplate.java:158)
                  at com.example.przem.ihm_mobile.rest.RestInterface_.<init>(RestInterface_.java:25)
                  at com.example.przem.ihm_mobile.activity.MainActivity_.init_(MainActivity_.java:46)
                  at com.example.przem.ihm_mobile.activity.MainActivity_.onCreate(MainActivity_.java:38)
(...)
Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.xml.transform.stax.StAXSource" on path: DexPathList[[zip file "/data/app/com.example.przem.ihm_mobile-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.przem.ihm_mobile-1/lib/x86, /system/lib, /vendor/lib]]
                  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                  at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
                  at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                  at org.springframework.http.converter.xml.SourceHttpMessageConverter.<clinit>(SourceHttpMessageConverter.java:74) 

TopLevelGradle

buildscript {
repositories {
    mavenCentral()
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.2.2'
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
repositories {
    mavenCentral()
    mavenLocal()
}
allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

and another build>gradle

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

def AAVersion = '4.0.0'

android {
compileSdkVersion 25
buildToolsVersion "25.0.1"

defaultConfig {
    applicationId "com.example.przem.ihm_mobile"
    minSdkVersion 21
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}
dexOptions {
    javaMaxHeapSize "4g" //specify the heap size for the dex process
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

packagingOptions {
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/dependencies.txt'
    exclude 'META-INF/spring.schemas'
    exclude 'META-INF/spring.tooling'
    exclude 'META-INF/spring.handlers'
    exclude 'META-INF/LGPL2.1'
}
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:design:25.0.1'

testCompile 'junit:junit:4.12'

//PagerSlidingTabStrip
compile 'com.jpardogo.materialtabstrip:library:1.1.1'

//Basic AndroidAnnotations
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"

//AndroidAnnotation RestPlugin
apt "org.androidannotations:rest-spring:$AAVersion"
compile "org.androidannotations:rest-spring-api:$AAVersion"

//AndroidAnnotation Web (Converters)
compile group: 'org.springframework', name: 'spring-core', version: '4.3.4.RELEASE'
compile group: 'org.springframework', name: 'spring-web', version: '4.3.4.RELEASE'

//@Parcel Android Annotations
apt "org.parceler:parceler:1.1.5"
compile 'org.parceler:parceler-api:1.1.5'

//@ArcAnimator
compile 'com.github.asyl.animation:arcanimator:1.0.0'

//@Material Date-Time Picker
compile 'com.code-troopers.betterpickers:library:3.0.1'

//@Joda-Time
compile group: 'joda-time', name: 'joda-time', version: '2.3'

//Json Jackson core + annotations + databind
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.8.5'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.5'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.8.5'
}

It does not see class javax.xml.transform.stax.StAXSource" inside spring Converters. I am not doing anything direcltly with this class. Seems like it does not include it into .apk file.


Solution

  • You should not ues org.springframework:spring-core/web, these are artifacts meant for Java web development, not Android. This is the correct library:

    compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'

    You should also include underlying library for your converters (like GSON, Simple XML, Jackson) etc. The converters themselves are already included in the rest template artifact.