我正在使用 dagger2,在生成签名的 apk 时,改造 api 调用不起作用,在调试模式下它工作正常

I'm using dagger2 ,while genrating signed apk ,retrofit api calls are not working, in debug mode its working fine

Retrofit 和 moshi-kotlin Api 在 android 中不工作。 我正在使用 dagger2,虽然生成签名的 apk,改造 api 调用不起作用,但在调试模式下它工作正常。 而我们启用了 minifyenable true 和 shrinkresource true。 @JsonClass(generateAdapter = true) @SuppressLint("ParcelCreator") @Keep data class DashboardListRes( @Json(name = "data") var数据: List<Data>?=ArrayList(), @Json(name = "lead_data") var leadData: LeadData= LeadData(), @Json(name = "message") var message: String="", @Json(name = "status") var status: String="", @Json(name = "status_code") var statusCode: Int=0, @Json(name = "user") var user: User=User() ):Serializable

1。 2.-keepattributes 签名、InnerClasses、EnclosingMethod

     -keepattributes RuntimeVisibleAnnotations,
    RuntimeVisibleParameterAnnotations    -keepattributes
    AnnotationDefault   
    -keepclassmembers,allowshrinking,allowobfuscation interface * {
         @retrofit2.http.* <methods>;  }    -dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement    -dontwarn
    javax.annotation.**    -dontwarn kotlin.Unit    -dontwarn
    retrofit2.KotlinExtensions    -dontwarn retrofit2.KotlinExtensions$*
    -if interface * { @retrofit2.http.* <methods>; }    -keep,allowobfuscation interface <1>    -keep,allowobfuscation,allowshrinking interface retrofit2.Call    -keep,allowobfuscation,allowshrinking class retrofit2.Response    -keep,allowobfuscation,allowshrinking class kotlin.coroutines.Continuation    -dontwarn javax.annotation.**
    
     -keepclasseswithmembers class * {
         @com.squareup.moshi.* <methods>;  }
    
     -keep @com.squareup.moshi.JsonQualifier @interface *   
    -keepclassmembers @com.squareup.moshi.JsonClass class * extends java.lang.Enum {
         <fields>;
         **[] values();  }    -keepclassmembers class com.squareup.moshi.internal.Util {
         private static java.lang.String getKotlinMetadataClassName();  }    -keepclassmembers class * {   
@com.squareup.moshi.FromJson
    <methods>;    @com.squareup.moshi.ToJson <methods>;  }
         -keep class kotlin.Metadata
     
    
     -keep class com.package.models.**{*;}
     
    
     -keepclassmembers  class com.package.dataclasses.** { *; }
         -keepattributes Signature    -keepattributes Annotation    -dontwarn sun.misc.**    -keep class com.google.gson.stream.** { *; }    -keep class com.google.gson.examples.android.model.** {
    <fields>; }    -keep class com.credright.nikhil.models.** { *; }   
    -keep class com.package.ui.** { *; }    -keep class * extends com.google.gson.TypeAdapter    -keep class * implements
    com.google.gson.TypeAdapterFactory    -keep class * implements
    com.google.gson.JsonSerializer    -keep class * implements
    com.google.gson.JsonDeserializer
        Prevent R8 from leaving Data object members always null
        -keepclassmembers,allowobfuscation class * {
    
       @com.google.gson.annotations.SerializedName <fields>;  }   
    -keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken   
    -keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken    -dontwarn javax.annotation.**  
    -keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase    -dontwarn
org.codehaus.mojo.animal_sniffer.*    -dontwarn
okhttp3.internal.platform.ConscryptPlatform
     
    
     -keep class org.apache.commons.logging.**
     
    
     -keepattributes Annotation
     
    
     -dontwarn org.apache.**
     
    
     -keepnames @dagger.hilt.android.lifecycle.HiltViewModel class *
    extends androidx.lifecycle.ViewModel

如果您使用某些内置函数将接收到的数据转换为 POJO 对象,然后填充数据 class 或其他内容,您需要确保从中排除数据 classes proguard.

我通常做的是将所有数据 classes 放在一个包中,然后排除该包中的所有内容,如下所示:

-keepclassmembers  class com.credright.nikhil.dataclasses.** { *; }

上面的行应该添加到文件 proguard-rules.pro 并且 dataclasses 是包含所有数据 class 的包的名称。

这很重要的原因是,当混淆器混淆您的代码时,它基本上将所有 class 名称和所有内容更改为任意内容,因此混淆后数据 classes 无法填充。

在使用 moshi-kotli 时一直存在一些困难。更新到更新的 Kotlin 版本 + 更新一些 proguard 规则可以修复它。 你可以看看这个 GitHub comment.