gpt4 book ai didi

android - Android:带翻新功能的库项目导致NoClassDefFoundError

转载 作者:行者123 更新时间:2023-12-03 05:48:06 24 4
gpt4 key购买 nike

我正在尝试构建一个使用OkHttp和Retrofit授予对REST后端的访问权限的android库项目。

只要我在gradt依赖性api上使用implementation而不是com.squareup.retrofit2:converter-gson,它似乎就可以正常工作。似乎这是唯一需要此依赖的依赖项(所有其他依赖项都可以与implementation一起使用)。

同样在此处使用implementation时,调用应用程序也会因NoClassDefFoundError而崩溃:

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/gson/GsonBuilder;



我正在将 consumerProguardFiles与一堆proguard-rule文件一起使用(例如,来自 repo的okhttp3,retrofit2等文件)。然后将所有这些内容打包到AAR中,因此也应添加依赖项中的规则。

看来我缺少 converter-gson的保护规则。有人可以在图书馆项目中发布使用 com.squareup.retrofit2:converter-gson所需的规则吗?

编辑1

我的 current proguard-rule用于 retrofit :
# Retrofit 2.X
## https://square.github.io/retrofit/ ##

-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions

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

如上所述,这似乎还不够。也许我还需要 converter-gson !!

编辑2

我的依赖关系如下所示:
implementation "com.squareup.okhttp3:okhttp:$okHttpVersion"
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
api "com.squareup.retrofit2:converter-gson:$retrofitVersion"

我正在尝试摆脱最后一个上的 api,因为在使用此库的应用程序中不需要它(或者这是不可避免的)吗?

编辑3

嗯,也许proguard是错误的关键字。实际上,问题在于 implementation而不是 api的使用。因此,依赖项不会暴露给消费者。参见 https://stackoverflow.com/a/44493379/2170109

但是我不明白为什么当只有库调用 converter-gson时才需要向我的库的使用者公开 GsonBuilder吗?

最佳答案

根据官方改造github链接:

https://github.com/square/retrofit

改造至少需要Java 7或Android 2.3。

如果您使用的是ProGuard,则需要添加以下选项:

# 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

# My personal proguard configuration for ratrofit2
-dontwarn okio.**
-dontwarn javax.annotation.**
-dontwarn retrofit2.Platform$Java8

请检查一下您的配置是否有任何错误:

app-> build.gradle
// retrofit gson

implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'

如果还有更多错误,您可以共享错误日志以解释到底是什么问题。

关于android - Android:带翻新功能的库项目导致NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49030115/

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