gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:37:10 27 4
gpt4 key购买 nike

所以我的项目编译和运行良好,我将 AndroidAnnotations @RestService 注入(inject)到我的 MainActivity

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

@RestService
RestInterface restInterface;
...
}

我的休息客户端:

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

错误堆栈跟踪:

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) 

顶级等级

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 转换器中看到类 javax.xml.transform.stax.StAXSource"。我没有直接对这个类做任何事情。似乎它没有将它包含到 .apk 文件中。

最佳答案

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

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

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

关于java - 使用@RestService 时的类加载异常 - dalvik.system.BaseDexClassLoader.findClass 处的 java.lang.ClassNotFoundException (...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40845926/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com