Retrofit 2 not sending data when ProGuard is enabled
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
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
finally i find.
just try this if you use Gson
,
add this to retrofit pro-guard :
-keepclassmembers,allowobfuscation class * {
@com.google.gson.annotations.SerializedName <fields>;
}
then use @SerializedName("name")
in your model .kotlin
example:
class PaymentRequestModel (
@SerializedName("name")
@Expose
var name : String = "",
}