我正在androidstudio上开发一个带有jersey 3.0rest客户端的sdk,在构建源代码的过程中遇到了一些问题。
从桌面版本移植的同一个sdk运行良好,是使用vscode和maven与jdk1.8开发的。
这是我的gradle.build文件,对于导入maven工件,我在存储库{}中添加了一个mavencentral():
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.example.jersey_v01"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_1_8
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/ASL2.0'
exclude 'META-INF/NOTICE.md'
exclude 'META-INF/LICENSE.md'
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/NOTICE.markdown'
exclude("META-INF/*.kotlin_module")
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
//implementation 'jakarta.xml.bind:jakarta.xml.bind-api:3.0.0'
implementation 'org.glassfish.jersey.inject:jersey-cdi2-se:3.0.0-M6'
implementation 'org.glassfish.jersey.media:jersey-media-json-jackson:3.0.0-M6'
implementation 'org.glassfish.jersey.core:jersey-client:3.0.0-M6'
implementation 'org.glassfish.jersey.media:jersey-media-multipart:3.0.0-M6'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.10.0.pr3'
implementation 'com.android.support:multidex:1.0.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
在上一次构建之前,我在这个文件中添加了一个multidex支持,并排除了meta-inf。
活动代码非常简单:
package com.example.jersey_v01;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import org.glassfish.jersey.media.multipart.internal.MultiPartWriter;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.Form;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.MessageBodyWriter;
public class MainActivity extends AppCompatActivity {
public static final String serviceUri = "http://address/service";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startClient();
}
public void startClient(){
Client client = ClientBuilder.newClient()
.register(MessageBodyWriter.class) // tryed to add for resolve a build error
// but still not working.
.register(MultiPartWriter.class);
WebTarget target = client.target(serviceUri);
WebTarget endpoint = target
.path("/auth/authenticate");
Form form = new Form();
form.param("email", "mail");
form.param("password", "pw");
try {
Response response = endpoint
.request()
.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED));
System.out.println(response.readEntity(String.class));
}
catch (Exception e){
e.printStackTrace();
}
}
}
控制台日志:
11/21 11:50:53: Launching 'app' on Pixel 2 API 26.
Install successfully finished in 1 s 706 ms.
$ adb shell am start -n "com.example.jersey_v01/com.example.jersey_v01.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: com.example.jersey_v01.test | com.example.jersey_v01
Waiting for application to come online: com.example.jersey_v01.test | com.example.jersey_v01
Waiting for application to come online: com.example.jersey_v01.test | com.example.jersey_v01
Connecting to com.example.jersey_v01
Connected to the target VM, address: 'localhost:44203', transport: 'socket'
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/zygote: Not late-enabling -Xcheck:jni (already on)
W/zygote: Unexpected CPU variant for X86 using defaults: x86
W/ActivityThread: Application com.example.jersey_v01 is waiting for the debugger on port 8100...
I/System.out: Sending WAIT chunk
I/zygote: Debugger is active
I/System.out: Debugger has connected
waiting for debugger to settle...
I/chatty: uid=10086(u0_a86) com.example.jersey_v01 identical 6 lines
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1317)
I/MultiDex: VM with version 2.1.0 has multidex support
Installing application
VM has multidex support, MultiDex support library is disabled.
I/zygote: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;
at void androidx.core.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2678)
at void androidx.appcompat.widget.ActionBarContainer.<init>(android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:63)
....
....
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.View$OnUnhandledKeyEventListener" on path: DexPathList[[zip file "/data/app/com.example.jersey_v01-k0ai8VmmoQ4FVaZJrxkn0g==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.jersey_v01-k0ai8VmmoQ4FVaZJrxkn0g==/lib/x86, /system/lib, /vendor/lib]]
at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:93)
at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:379)
...
...
at void com.android.internal.os.Zygote$MethodAndArgsCaller.run() (Zygote.java:240)
at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:767)
Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;
at void androidx.core.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2678)
at void androidx.appcompat.widget.ActionBarContainer.<init>(android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:63)
at java.lang.Object java.lang.reflect.Constructor.newInstance0(java.lang.Object[]) (Constructor.java:-2)
at java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[]) (Constructor.java:334)
...
...
at void com.android.internal.os.Zygote$MethodAndArgsCaller.run() (Zygote.java:240)
at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:767)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.View$OnUnhandledKeyEventListener" on path: DexPathList[[zip file "/data/app/com.example.jersey_v01-k0ai8VmmoQ4FVaZJrxkn0g==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.jersey_v01-k0ai8VmmoQ4FVaZJrxkn0g==/lib/x86, /system/lib, /vendor/lib]]
at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:93)
at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:379)
at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
at void androidx.core.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2678)
I/zygote: at void androidx.appcompat.widget.ActionBarContainer.<init>(android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:63)
at java.lang.Object java.lang.reflect.Constructor.newInstance0(java.lang.Object[]) (Constructor.java:-2)
at java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[]) (Constructor.java:334)
...
...
at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:767)
Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;
at void androidx.core.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2678)
at void androidx.appcompat.widget.ActionBarContainer.<init>(android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:63)
at java.lang.Object java.lang.reflect.Constructor.newInstance0(java.lang.Object[]) (Constructor.java:-2)
...
...
at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:767)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.View$OnUnhandledKeyEventListener" on path: DexPathList[[zip file "/data/app/com.example.jersey_v01-k0ai8VmmoQ4FVaZJrxkn0g==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.jersey_v01-k0ai8VmmoQ4FVaZJrxkn0g==/lib/x86, /system/lib, /vendor/lib]]
at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:93)
at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:379)
...
...
at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:767)
I/zygote: Do partial code cache collection, code=29KB, data=20KB
I/zygote: After code cache collection, code=29KB, data=20KB
Increasing code cache capacity to 128KB
W/MessagingBinders: A class java.awt.image.RenderedImage for a default provider MessageBodyWriter<java.awt.image.RenderedImage> was not found. The provider is not available.
W/System.err: java.lang.IllegalStateException: WELD-ENV-000016: Missing beans.xml file in META-INF
at org.jboss.weld.environment.se.Weld.initialize(Weld.java:773)
W/System.err: at org.jboss.weld.environment.se.Weld.initialize(Weld.java:176)
W/System.err: at org.glassfish.jersey.inject.cdi.se.CdiSeInjectionManager.completeRegistration(CdiSeInjectionManager.java:241)
W/System.err: at org.glassfish.jersey.client.ClientConfig$State.initRuntime(ClientConfig.java:449)
W/System.err: at org.glassfish.jersey.client.ClientConfig$State.lambda$d1oxBBJfVlzW1xZThvyU0tJbmNM(Unknown Source:0)
W/System.err: at org.glassfish.jersey.client.-$$Lambda$ClientConfig$State$d1oxBBJfVlzW1xZThvyU0tJbmNM.get(Unknown Source:2)
W/System.err: at org.glassfish.jersey.internal.util.collection.Values$LazyValueImpl.get(Values.java:317)
W/System.err: at org.glassfish.jersey.client.ClientConfig.getRuntime(ClientConfig.java:807)
W/System.err: at org.glassfish.jersey.client.ClientRequest.getClientRuntime(ClientRequest.java:219)
W/System.err: at org.glassfish.jersey.client.ClientRequest.getInjectionManager(ClientRequest.java:610)
W/System.err: at org.glassfish.jersey.client.JerseyWebTarget.onBuilder(JerseyWebTarget.java:364)
W/System.err: at org.glassfish.jersey.client.JerseyWebTarget.request(JerseyWebTarget.java:192)
at org.glassfish.jersey.client.JerseyWebTarget.request(JerseyWebTarget.java:36)
W/System.err: at com.example.jersey_v01.MainActivity.startClient(MainActivity.java:45)
at com.example.jersey_v01.MainActivity.onCreate(MainActivity.java:26)
W/System.err: at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
W/System.err: at android.app.ActivityThread.-wrap11(Unknown Source:0)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
W/System.err: at android.os.Looper.loop(Looper.java:164)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Disconnected from the target VM, address: 'localhost:44203', transport: 'socket'
我对“beans.xml not found”进行了调查,并尝试添加它,但没有得到积极的结果。关于缺少java.awt.image类:这在android上是不受支持的(awt)。
有一些解决方法可以跳过此错误吗?我做了一些关于android上jersey的兼容性的研究,它看起来是兼容的:oracle blog
暂无答案!
目前还没有任何答案,快来回答吧!