Home > Back-end >  I'm using dagger2 ,while genrating signed apk ,retrofit api calls are not working, in debug mod
I'm using dagger2 ,while genrating signed apk ,retrofit api calls are not working, in debug mod

Time:01-31

Retrofit and moshi-kotlin Api's are not working in android. I'm using dagger2 ,while genrating signed apk ,retrofit api calls are not working, in debug mode its working fine . while we enabled minifyenable true and shrinkresource true. @JsonClass(generateAdapter = true) @SuppressLint("ParcelCreator") @Keep data class DashboardListRes( @Json(name = "data") vardata: 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 Signature, 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

CodePudding user response:

There have historically been some difficulties when using moshi-kotli. Updating to a newer Kotlin version updating some proguard rules fixes it. You can check out this GitHub comment.

CodePudding user response:

If you are converting the data received into a POJO object using some built in function and then populating a data class or something, you need to make sure you exclude the data classes from proguard.

What I generally do is I put all my data classes in one package and then exclude everything in that package like so:

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

The line above should be added to the file proguard-rules.pro and dataclasses is the name of the package that has all the data classes in it.

The reason why this is important is that when proguard obfuscates your code, it basically changes all class names and everything into arbitrary things and so after obfuscation data classes cannot be populated.

  •  Tags:  
  • Related