When I add Retrofit 2.4.0 library to android project >
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
and set minifyEnabled {true}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
and then add these rules to proguard-rules.pro
-keep class com.squareup.** { *; }
-keep interface com.squareup.** { *; }
-keep class retrofit2.** { *; }
-keep interface retrofit2.** { *;}
-keep interface com.squareup.** { *; }
-keepclasseswithmembers class * {
@retrofit2.http.* <methods>;
}
-dontwarn rx.**
-dontwarn retrofit2.**
-dontwarn okhttp3.**
-dontwarn okio.**
Finally built and generated signed apk successfully but the issue is when run the ( release apk) > Retrofit requests not send and return { null } .. Whats the solution Please!
maybe because of other library work with retrofit like your downloader or parser.
add rule to keep your model classes and subjects that work with parser like :
-keep class com.address_package.** { *; }
if you use okhttp or Okhttp3 with retrofit added below rules
note :and check your parser proguard rules too
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-dontwarn com.squareup.okhttp.**
-dontwarn okio.**
-keepattributes Signature
-keepattributes *Annotation*
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**
Your Proguard rules work for Retrofit, but they're also obfuscating the model classes that you use to serialize/deserialize your data. Their names are important as Retrofit/Gson matches them to do serializing/deserializing. Proguard turns them into gibberish like a
and b
so Retrofit/Gson cannot make sense of them.
Depending on your package setup, you need to add the following like amin mahmodi mentioned.
-keep class your.package.name.models.** { *; }
This answer may be quite late, but it could help someone else.
Instead of keeping an entire classes with:
-keep class your.package.name.models.** { *; }
You can use:
-keepclassmembers class * {
@com.google.gson.annotations.SerializedName <fields>;
}
This would allow you to place your models anywhere you'd like and not include them in the ProGuard file as a separate rule each time you change their project location.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With