【问题标题】:Invalid Request body in retrofit while enabling the Progaurd启用 Proguard 时改造中的无效请求正文
【发布时间】:2020-10-30 17:41:45
【问题描述】:

当我在发布版本中添加 progaurd 文件时,不断收到错误“Invalid Request Body”。检查后我发现,请求正文传递为 {},为什么会这样?

Proguard-rules.pro 文件

-keep class com.android.vending.billing.**

#event bus
-keepattributes *Annotation*
-keepclassmembers class * {
    @org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }

# And if you use AsyncExecutor:
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
    <init>(java.lang.Throwable);
}

#timber
-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
    public static *** e(...);
}

 
#google play billing
-keep class com.android.vending.billing.**

#no need to add the progaurd rule for rx-java 2- https://github.com/ReactiveX/RxJava/issues/6243

#for youtube player
# Needed to keep generic types and @Key annotations accessed via reflection
-keepattributes Signature,RuntimeVisibleAnnotations,AnnotationDefault
# Needed by google-http-client-android when linking against an older platform version
-dontwarn com.google.api.client.extensions.android.**
# Needed by google-api-client-android when linking against an older platform version
-dontwarn com.google.api.client.googleapis.extensions.android.**
# Needed by google-play-services when linking against an older platform version
-dontwarn com.google.android.gms.**
# com.google.client.util.IOUtils references java.nio.file.Files when on Java 7+
-dontnote java.nio.file.Files, java.nio.file.Path
# Suppress notes on LicensingServices
-dontnote **.ILicensingService
# Suppress warnings on sun.misc.Unsafe
-dontnote sun.misc.Unsafe
-dontwarn sun.misc.Unsafe

-dontwarn com.google.common.**
-dontwarn com.google.api.client.json.**

#event bus
-keepattributes *Annotation*
-keepclassmembers class * {
    @org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }

# And if you use AsyncExecutor:
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
    <init>(java.lang.Throwable);
}

#pinview no need

#retrofit

# Retrofit does reflection on generic parameters. InnerClasses is required to use Signature and
# EnclosingMethod is required to use InnerClasses.
-keepattributes Signature, InnerClasses, EnclosingMethod

# Retrofit does reflection on method and parameter annotations.
-keepattributes RuntimeVisibleAnnotations, RuntimeVisibleParameterAnnotations

# Retain service method parameters when optimizing.
-keepclassmembers,allowshrinking,allowobfuscation interface * {
    @retrofit2.http.* <methods>;
}

# Ignore annotation used for build tooling.
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

# Ignore JSR 305 annotations for embedding nullability information.
-dontwarn javax.annotation.**

# Guarded by a NoClassDefFoundError try/catch and only used when on the classpath.
-dontwarn kotlin.Unit

# Top-level functions that can only be used by Kotlin.
#-dontwarn retrofit2.KotlinExtensions
#-dontwarn retrofit2.KotlinExtensions$*

# With R8 full mode, it sees no subtypes of Retrofit interfaces since they are created with a Proxy
# and replaces all potential values with null. Explicitly keeping the interfaces prevents this.
-if interface * { @retrofit2.http.* <methods>; }
-keep,allowobfuscation interface <1>

#keep your models
-keep class packagename.parent.models.** { *; }
-keep class packagename.child.models.** { *; }
-keep class packagename.rest.service.** { *; }
-keep class packagename.rest.service.responce.** { *; }
-keep class packagename.child.service.statics.model.** { *; }


#okhttps3
-dontwarn okhttp3.**
-keep class okhttp3.* { *;}
-dontwarn okio.

#glide
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep class * extends com.bumptech.glide.module.AppGlideModule {
 <init>(...);
}
-keep public enum com.bumptech.glide.load.ImageHeaderParser$** {
  **[] $VALUES;
  public *;
}
-keep class com.bumptech.glide.load.data.ParcelFileDescriptorRewinder$InternalRewinder {
  *** rewind();
}
# Uncomment for DexGuard only
#-keepresourcexmlelements manifest/application/meta-data@value=GlideModule

#room
-keep class * extends androidx.room.RoomDatabase
-dontwarn androidx.room.paging.**

#timber
-dontwarn org.jetbrains.annotations.**

#crashlytices
-keepattributes *Annotation*
-keepattributes SourceFile,LineNumberTable
-keep public class * extends java.lang.Exception

为什么我收到“无效的请求正文”错误?为什么没有创建请求正文?为什么 pro-guard 不考虑模型类?

【问题讨论】:

    标签: android retrofit2 proguard


    【解决方案1】:

    使用 @Keep 注释来注释您的每个数据模型类,这样它就不会缩小您的数据类引用。 你也可以在这里阅读文档https://developer.android.com/reference/androidx/annotation/Keep

    【讨论】:

      【解决方案2】:

      改造

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

      【讨论】:

        猜你喜欢
        • 2015-11-25
        • 2016-11-17
        • 2016-06-01
        • 1970-01-01
        • 2017-07-18
        • 1970-01-01
        • 1970-01-01
        • 2018-01-24
        • 1970-01-01
        相关资源
        最近更新 更多