0

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

只要我使用api而不是implementation依赖于com.squareup.retrofit2:converter-gson. 似乎这是唯一需要这个的依赖项(所有其他依赖项都可以与 一起使用implementation)。

implementation这里使用时,调用应用程序将崩溃NoClassDefFoundError

java.lang.NoClassDefFoundError:解析失败:Lcom/google/gson/GsonBuilder;


我正在使用consumerProguardFiles一堆 proguard-rule-files(例如,来自这个repo的一个用于 okhttp3、retrofit2 等)。所有这些都将被打包到一个 AAR 中,因此还应该添加依赖项中的规则。

似乎我缺少converter-gson. com.squareup.retrofit2:converter-gson有人可以发布在图书馆项目中使用的规则吗?


编辑 1

目前的改造 proguard 规则是:

# 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 是错误的关键字。其实问题在于使用implementationover api。因此依赖项不会暴露给消费者。请参阅https://stackoverflow.com/a/44493379/2170109

但是我不明白为什么converter-gson只有图书馆调用时我需要向图书馆的消费者公开GsonBuilder

4

1 回答 1

0

根据官方改造 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

如果有任何错误,请检查您的配置:

应用程序-> build.gradle

// retrofit gson

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

如果还有更多错误,您可以分享您的错误日志以向您解释究竟是什么问题。

于 2018-02-28T13:12:29.610 回答