ProGuard for Android and Retrofit2 Converter Gson?

I use ProGuard in my project, but it gives incorrect data in the new Gson (). toJson (Request);

I'm going out

{"a":"manage","b":"689184d4418b6d975d9a8e53105d3382","c":"10","d":"76"}

instead

{"username":"manage","password":"689184d4418b6d975d9a8e53105d3382","value":"10","store":"76"}

My ProGuard Rule

-dontwarn okio.**
-dontwarn retrofit2.Platform$Java8
-dontwarn sun.misc.Unsafe
-dontwarn org.w3c.dom.bootstrap.DOMImplementationRegistry
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-keepclassmembers class rx.internal.util.unsafe.** {
    long producerIndex;
    long consumerIndex;
}

-keepclasseswithmembers class * {
    @retrofit2.http.* <methods>;
}
-keep class com.google.gson.** { *; }
-keep class com.google.inject.** { *; }

and i use

 compile 'com.squareup.retrofit2:converter-gson:2.0.0'

Is there a new recommended ProGuard configuration for retrofit2: converter-gson in Android?

+4
source share
3 answers

you either want to save the class that you use with gson, or use the @SerializedNameannotation.

option 1 (save class)

// all classes in a package
-keep class com.example.app.json. ** {*; }
// or a specific class
-keep class com.example.app.json.SpecificClass {*; }

2 ( @SerializedName):

public class YourJsonClass{
   @SerializedName("name") String username;

   public MyClass(String username) {
     this.username = username;
   }
 }

proguard , gosn ,

+9

JSON @Keep.

+3

, Moshi JSON Retrofit, . , ProGuard RecyclerView, , NullPointerException, , Moshi . , .

:

@Json(name="username") String username;

In this way, ProGuard can trick variable names without breaking the conversion.

Another solution adds the “hold” option, such as the one suggested by Dodge in the file proguard-rules.pro

-keep public class com.example.model.User
0
source

Source: https://habr.com/ru/post/1649369/


All Articles