Android: library project with Retrofit results in NoClassDefFoundError

浪子不回头ぞ 提交于 2019-12-24 20:51:05

问题


I'm trying to build an android library project which uses OkHttp and Retrofit to grant access to our REST-backend.

It seems to be working as long as I use api instead of implementation on my gradle-dependency for com.squareup.retrofit2:converter-gson. It seems that this is the only dependency needing this (all others can used with implementation).

When using implementation here as well, the calling app will crash with NoClassDefFoundError:

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


I'm using consumerProguardFiles with a bunch of proguard-rule-files (e.g. one for okhttp3, retrofit2 etc. from this repo). All this will then be packed into an AAR, so rules from the dependencies should be added as well.

It seems that I'm missing a proguard-rule for converter-gson. Can somebody post the rules needed to use com.squareup.retrofit2:converter-gson in a library project?


Edit 1

My current proguard-rule for retrofit is:

# Retrofit 2.X
## https://square.github.io/retrofit/ ##

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

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

As mentioned, this seems not enough. Maybe I need something else for converter-gson!?

Edit 2

My dependencies look like this:

implementation "com.squareup.okhttp3:okhttp:$okHttpVersion"
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
api "com.squareup.retrofit2:converter-gson:$retrofitVersion"

And I'm trying to get rid of the api on the last one as I do not need it in the app using this library (or is this not avoidable)?


Edit 3

Hmm, maybe proguard is the wrong keyword. Actually the problem lies in the use of implementation over api. So dependencies will not be exposed to consumers. See https://stackoverflow.com/a/44493379/2170109.

But I don't get why I need to expose converter-gson to consumers of my library when only the library calls GsonBuilder?


回答1:


As per official retrofit github link :

https://github.com/square/retrofit

Retrofit requires at minimum Java 7 or Android 2.3.

If you are using ProGuard you need to add the following options:

# 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

please check once your configuration if there is any mistake :

app-> build.gradle

// retrofit gson

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

If there is more error you can share your error log to explain you what exactly problem is.



来源:https://stackoverflow.com/questions/49030115/android-library-project-with-retrofit-results-in-noclassdeffounderror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!