我正在尝试构建一个使用 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
# 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
over api
。因此依赖项不会暴露给消费者。请参阅https://stackoverflow.com/a/44493379/2170109。
但是我不明白为什么converter-gson
只有图书馆调用时我需要向图书馆的消费者公开GsonBuilder
?