I try to login my users using Retrofit 2. (Basically a GET to the login URL with a basic header) It works well but once I ProGuard it, the Header Authorization is not sent a
Add Retrofit 2 compatibility with Proguard code Obfuscator
-dontwarn retrofit.**
-keep class retrofit.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-dontwarn java.lang.invoke.*
-keep class com.elephantmobile.ui.remote.model.** { *; }
-dontwarn retrofit.appengine.UrlFetchClient
-keepclasseswithmembers class * {
@retrofit.http.* <methods>;
}
-keepclassmembernames interface * {
@retrofit.http.* <methods>;
}
-dontwarn retrofit2.Platform$Java8
Another simple solution use @keep from support annotation https://developer.android.com/reference/android/support/annotation/Keep.html
@Keep
interface APIService
{
@GET("/user/auth")
fun auth(@Header(Constants.AUTHORIZATION) authorization: String): Call<User>
}
You need to add
@SerializedName("yourInputParameter")
in your bean(model) class for your request body
For example
public class YourClass{
@SerializedName("yourInputParameter") String yourInputParameter;
...
}
it will work
because now retrofit unable to read your request body because proguard(minifyEnabled) is true
after adding @SerializedName in your request body it will definitely work for you
I finally managed to make it work. Here is the proguard configuration regarding Retrofit 2
# Retrofit
-dontwarn retrofit2.**
-dontwarn org.codehaus.mojo.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-keepattributes *Annotation*
-keepattributes RuntimeVisibleAnnotations
-keepattributes RuntimeInvisibleAnnotations
-keepattributes RuntimeVisibleParameterAnnotations
-keepattributes RuntimeInvisibleParameterAnnotations
-keepattributes EnclosingMethod
-keepclasseswithmembers class * {
@retrofit2.* <methods>;
}
-keepclasseswithmembers interface * {
@retrofit2.* <methods>;
}
Thanks @xudshen
UPDATE
The main problem: I used proguard-android-optimize So I should added:
-keepclasseswithmembers class * {
@retrofit2.http.* <methods>;
}
I also switched back to the regular Retrofit 2 proguard config provided by square :
# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on RoboVM on iOS. Will not be used at runtime.
-dontnote retrofit2.Platform$IOS$MainThreadExecutor
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions
for me works using annotation @SerializedName
public class YourJsonClass{
@SerializedName("name") String username;
...
}
Adding to @Romain's answer You Need to add to proguard file
-keepclasseswithmembers class * {
@retrofit2.http.* <methods>;
}
If you are using @Header, @Query...
Reference from here Retrofit2 proguard remove param