如何在 Retrofit 2 Android 的 Request Header 模型中避免 ProGuard 混淆?

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

1.without proguard 一切正常。在启用 Proguard 的同时,所有 header 和 body 模型变得模糊不清。

2.Retrofit2 无法通过模型 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)

下面提到的 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.** { *; }

更新: 我的请求 header 和数据

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

        }

使用以下。

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

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

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

这将适用于混淆器。

其他不需要的东西

使用以下。

# 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.**

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

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

希望对您有所帮助。 谢谢

假设您的包名称是 com.example.android,并且您的所有 API 请求模型 类 都在一个名为 'requests'

的包中

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

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

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

编码愉快!