0

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?

hardysim
  • 2,756
  • 2
  • 25
  • 52
  • Who's downvoting? This might be OK when there is an easy to find solution but the answers right now are not solving this :-/ – hardysim Feb 28 '18 at 13:54
  • 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 – hardysim Feb 28 '18 at 14:09
  • But I don't get why I need to expose `converter-gson` to consumers of my library when only the library calls `GsonBuilder`? – hardysim Feb 28 '18 at 14:10

1 Answers1

0

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.

Milan Hirpara
  • 534
  • 4
  • 18
  • The official rules for Retrofit seem different then mine (see edit in the question). But the "new" rules do not work either. Maybe I need something else for `converter-gson`? – hardysim Feb 28 '18 at 13:46
  • 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 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`? – hardysim Mar 01 '18 at 07:28