如何在 Retrofit 2 Android 中避免请求标头模型中的 ProGuard 混淆?

Kum*_*mar 2 android retrofit android-webservice retrofit2

1.without proguard 一切正常。在启用 Proguard 时,所有头部和身体模型都变得模糊不清。

2.Retrofit2 无法通过模型对象传递数据。

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)
Run Code Online (Sandbox Code Playgroud)

下面提到的 Proguard 规则已经添加到我的代码中,但仍然遇到同样的问题,请帮助我解决这个问题。

# 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.** { *; }
Run Code Online (Sandbox Code Playgroud)

更新: 我的请求标头和数据

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();

        }
Run Code Online (Sandbox Code Playgroud)

小智 6

当启用 proguard 时,它将混淆所有请求参数。因此,要解决这个问题,您必须在 Request 类中进行以下更改:假设这是您的请求类:

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

希望它会帮助你。谢谢


San*_*iga 5

假设您的包名称是 com.example.android,并且您将所有 API 请求模型类放在名为“requests”的包中

然后将此行添加到应用程序模块的混淆器中:

-keep class com.example.android.requests.** { *; }
Run Code Online (Sandbox Code Playgroud)

将此行添加到可能具有请求模型的所有类似包中。这样这些类就不会被混淆。

快乐编码!