Class 使用@RestService 时加载异常 - java.lang.ClassNotFoundException (...) 在 dalvik.system.BaseDexClassLoader.findClass

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

所以我的项目正在编译并且 运行 好的,我将 AndroidAnnotations @RestService 注入到我的 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);
}

一些消息来源让我认为它可能与 MultiDex 有关,但它似乎不是问题所在。 堆栈上也有一些类似的问题,但 none 给我带来了解决方案。

错误堆栈跟踪:

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
}

和另一个版本>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'
}

它没有在 spring 转换器中看到 class javax.xml.transform.stax.StAXSource"。我没有直接用这个 class 做任何事情。好像它没有包含它.apk 文件。

您不应该使用 org.springframework:spring-core/web,这些是用于 Java 网络开发的工件,而不是 Android。这是正确的库:

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

您还应该包括转换器的底层库(如 GSON、Simple XML、Jackson)等。转换器本身已经包含在其余模板工件中。