Home > Mobile >  Retrofit response.body is empty if minified is enabled
Retrofit response.body is empty if minified is enabled

Time:08-22

I am trying to create a release version of my app using below configuration.

buildTypes {
    release {
        debuggable false
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
    debug {
        debuggable true
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

Its working fine in debug but retrofit response.body is empty in release mode.

I tried Proguard rules as mentioned here: https://github.com/square/retrofit/blob/master/retrofit/src/main/resources/META-INF/proguard/retrofit2.pro

I also tried adding -dontobfuscate to proguard-rule.pro, tried removing proguardFiles parameter from the release type but nothing is working.

I am using following version of dependncies at app level:

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation "com.squareup.okhttp3:okhttp"

At Project Level:

id 'com.android.application' version '7.2.0' apply false
id 'com.android.library' version '7.2.0' apply false
id 'org.jetbrains.kotlin.android' version "$kotlin_version" apply false
id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' version '2.0.1' apply false

CodePudding user response:

You will have to add the below code to proguard-rules.pro

-keepclassmembers,allowobfuscation class * {
   @com.google.gson.annotations.SerializedName <fields>;
 }

Also add below line in gradle.properties file.

android.enableR8=false

CodePudding user response:

Finally, solved after trying several proguard-rule.pro.

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

# GSON Annotations
-keepclassmembers class * {
  @com.google.gson.annotations.SerializedName <fields>;
}

-keepclassmembers class <package>.dto.** { *; }
-keepclassmembers class <package>.mapper.** { *; }
-keepclassmembers class <package>.model.** { *; }
-keepclassmembers class <package>.ApiResponse { *; }

Basically, if you are using APIResponse data class. Add the proguard rule

  • Related