使用@RestService时的类加载异常-dalvik.system.BaseDexClassLoader.findClass上的java.lang.ClassNotFoundException(...) [英] Class Load Exception when using @RestService - java.lang.ClassNotFoundException (...) at dalvik.system.BaseDexClassLoader.findClass

查看:170
本文介绍了使用@RestService时的类加载异常-dalvik.system.BaseDexClassLoader.findClass上的java.lang.ClassNotFoundException(...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的项目正在正常编译并很好地运行,我将AndroidAnnotations @RestService注入了MainActivity

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:

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有关,但似乎不是问题。
栈上已经有一些类似的问题,但是没有一个给我带来解决方案。

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.

错误Stacktrace:

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

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
}

和另一个build> gradle

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

在Spring Converters中看不到类javax.xml.transform.stax.StAXSource。我在这堂课上没有做任何直接的事情。

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.

推荐答案

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

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'

您还应该包括转换器的基础库(例如GSON,Simple XML,Jackson)等。其余的转换器本身已包含在内模板工件。

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.

这篇关于使用@RestService时的类加载异常-dalvik.system.BaseDexClassLoader.findClass上的java.lang.ClassNotFoundException(...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆