gpt4 book ai didi

android - 在模块(android 库)中使用改造 2.4.0 时出现 NoClassDefFoundError

转载 作者:行者123 更新时间:2023-12-03 21:22:35 28 4
gpt4 key购买 nike

一直致力于在一个项目的 android 库中使用改造。同时,如果在应用程序的 gradle 中进行了配置,或者如果库的源代码是应用程序项目的一部分,那么一切似乎都运行良好。

但是,如果库项目被保护并作为@aar 包含在应用程序中。似乎有一个混淆问题。

这是我在 android 模块项目中进行改造的 proguard:

-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Exceptions
-keepattributes RuntimeVisibleAnnotations
-keepattributes RuntimeInvisibleAnnotations
-keepattributes RuntimeVisibleParameterAnnotations
-keepattributes RuntimeInvisibleParameterAnnotations

-keepattributes EnclosingMethod
-keepclasseswithmembers class * {
@retrofit2.http.* <methods>;
}
-keepclasseswithmembers interface * {
@retrofit2.* <methods>;
}

# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on RoboVM on iOS. Will not be used at runtime.
-dontnote retrofit2.Platform$IOS$MainThreadExecutor
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions

# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain service method parameters.
-keepclassmembernames,allowobfuscation interface * {
@retrofit2.http.* <methods>;
}
# Ignore annotation used for build tooling.
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

-keep public interface com.mylibrary.interfaces.**
-keep public class com.mylibrary.rest.RetrofitClientInstance
-keep public class com.mylibrary.models.** {*;}

-dontwarn org.xmlpull.v1.**
-dontwarn javax.annotation.**


### OkHttp3
-dontwarn okhttp3.**
-dontwarn okio.**
-dontwarn javax.annotation.**
# A resource is loaded with a relative path so the package of this class must be preserved.
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase

而且,这就是我的 RetrofitClientInstance 类的样子:
public class RetrofitClientInstance {

private static Retrofit retrofit;

public static Retrofit getRetrofitInstance() {
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(Constants.BASE_API_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}

我得到的错误是:
java.lang.NoClassDefFoundError: Failed resolution of: Lretrofit2/Retrofit$Builder;
at com.mylibrary.rest.RetrofitClientInstance.getRetrofitInstance(RetrofitClientInstance.java:15)
at com.mylibrary.rest.LoginAPIHelper.<init>(LoginAPIHelper.java:29)
at com.mylibrary.rest.LoginAPIHelper.getInstance(LoginAPIHelper.java:36)
at com.view.LoginActivity.onCreate(LoginActivity.java:124)
at android.app.Activity.performCreate(Activity.java:6772)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2715)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2823)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6349)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)
Caused by: java.lang.ClassNotFoundException: Didn't find class "retrofit2.Retrofit$Builder" on path: DexPathList[[zip file "/data/app/com.inventaapp-2/base.apk"],nativeLibraryDirectories=[/data/app/com.inventaapp-2/lib/arm, /data/app/com.inventaapp-2/base.apk!/lib/armeabi-v7a, /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 com.mylibrary.rest.RetrofitClientInstance.getRetrofitInstance(RetrofitClientInstance.java:15) 
at com.mylibrary.rest.LoginAPIHelper.<init>(LoginAPIHelper.java:29) 
at com.mylibrary.rest.LoginAPIHelper.getInstance(LoginAPIHelper.java:36) 
at com.inventaapp.view.LoginActivity.onCreate(LoginActivity.java:124) 
at android.app.Activity.performCreate(Activity.java:6772) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2715) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2823) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6349) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783) 

我已经尝试对导入库的主应用程序进行多重索引。不影响行为。事实上,如果源代码是作为应用程序项目中的模块导入的,它可以在没有多重索引的情况下工作。

此外,如果 aar 作为库导入。但是,如果我在主项目的 gradle 中添加改造 gradle 导入。它也适用于这种情况。

我在这里想念什么?似乎与此类似的问题: https://github.com/square/retrofit/issues/1811

仅供引用,我的 gradle 看起来像:
 implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.4.0'

非常感谢任何帮助。

提前致谢,
阿纳布

最佳答案

在 android 库模块项目中使用改造时遇到了这个问题。尝试了各种方法,下面对我有用
第 1 步:我在 app 和库模块 build.gradle 文件中添加并更新了改造依赖项(2.9.0)

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.7.1'
implementation 'com.squareup.okhttp3:logging-interceptor:4.8.0'
第 2 步:在应用程序的 gradle 中添加
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
第 3 步:使缓存无效并重新启动/清理构建和重建。

关于android - 在模块(android 库)中使用改造 2.4.0 时出现 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50679999/

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