How to Avoid ProGuard obfuscation in Request Header Model in Retrofit 2 Android?

3.4k Views Asked by At

1.without proguard Everything working fine. while enabling the Proguard, all the header and body models are getting obfuscated.

2.Retrofit2 not able to pass data with model objects.

D/OkHttp: Content-Type: application/json
Content-Length: 80
 {"a":{"a":"123","b":"***","c":"","d":"Data","f":"1.0"},"b":{"a":""}}
--> END POST (80-byte body)

below mentioned Proguard rules are already added in my code still getting same problem, kindly help me to fix this issue.

# 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>;
}

# 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


-keepclasseswithmembers class * {
    @retrofit2.http.* <methods>;
}

-keepclassmembers class * {
    *** get*();
    void set*(***);
}

-keepattributes Signature

-dontwarn retrofit.**
-keep class retrofit.** { *; }
-keepattributes Signature
-keepattributes Exceptions

-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }

Update: My Request header and data

RequestPojo requestPojo = null;
        try {
            requestPojo = new RequestPojo();

            RequestHeader requestHeader = new RequestHeader();
            requestHeader.setID("123");
            requestHeader.setNo("*******");
            requestHeader.setName("XYZ");
            requestHeader.setCode("ABC");
            requestHeader.setAge("1");


            /*Request DATA*/
            RequestData requestData = new RequestData();
            requestData.setData("Hai");

            requestPojo.requestHeader = requestHeader;
            requestPojo.requestData = requestData;
        } catch (Exception e) {
            e.printStackTrace();

        }
4

There are 4 best solutions below

0
Surender Kumar On BEST ANSWER

When proguard is enabled then it will obfuscate all the request parameters. So to overcome this issue you have to do the following changes in your Request class: Suppose this is your request class:

class DemoRequest{
         @SerializedName("name")//here this SerializedName will avoid obfuscate of variables
         private String name;
         .
         .
         .
}

Hope it will help you. Thanks

1
Rohit On

Use following.

-keep class retrofit2.** { *; }
-keep class okhttp3.internal.** { *; }

-dontwarn okhttp3.internal.**
-dontwarn retrofit2.**

-keepclasseswithmembers class * {
    @retrofit2.http.* <methods>;
}

This will work for proguard.

Rest of the things not needed

1
Sana Ebadi On

Use following.

# Retrofit
-dontnote retrofit2.Platform # Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform$IOS$MainThreadExecutor # Platform used when running on RoboVM on iOS. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8 # Platform used when running on Java 8 VMs. Will not be used at runtime.

# OkHttp 3
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**
0
Sandesh Baliga On

lets say your package name is com.example.android, and you have all your API request model classes in a package named 'requests'

then add this line to the proguard of your app module:

-keep class com.example.android.requests.** { *; }

add this line to all similar packages which might have the request models. So that those classes wont be obfuscated.

Happy coding!