Skip to content Skip to sidebar Skip to footer

Android - Proguard And Retrofit 2?

I am using from Proguard for my project and bellow code is in my proguard-rules.pro : # Retrofit -keep class com.google.gson.** { *; } -keep class com.google.inject.** { *; } -keep

Solution 1:

YOU need to keep your models, don't obfuscate them. I saw errors with your models, sqlite cannot work on your models. It should be like this:

-keep classpackage.to.yourmodels.** { *; }

Solution 2:

2021 solution

Use @keep annotation before your data classes so they're retained. This removes the need of having separate Proguard rules.

Denotes that the annotated element should not be removed when the code is minified at build time. This is typically used on methods and classes that are accessed only via reflection so a compiler may think that the code is unused.

@KeepdataclassListing(
    val id: String = "",
    val name: String = ""
)

Source: https://developer.android.com/reference/kotlin/androidx/annotation/Keep

Solution 3:

As per rerofit 3.8.1

-dontwarn okio.**-dontwarn javax.annotation.**-dontwarn retrofit2.Platform$Java8

Solution 4:

Current Proguard rules (as of 12/18) are here.

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

# 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

Solution 5:

I've noticed that adding the annotation of @Keep for each of the classes, indeed it fixes everything, but also doesn't change the names of everything I've set it to.

So, my solution is a bit different:

Put all requests, responses and the classes they need (to hold data) into some folder (or folders if you want to be more organized), and create the Proguard rule inside "proguard-rules.pro" file:

-keep,allowobfuscation classcom.your_library.rest_objects.** { *; }

Alternatively, if this doesn't work for some reason, you can use this (got from here) :

-keep publicclass * {
    publicprotected *;
}

In addition, if you build an obfuscated library, you should put this (the second one, not the one with "allowobfuscation") in "consumer-rules.pro", and make sure you have this inside its gradle file:

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
//        https://developer.android.com/studio/projects/android-library
            consumerProguardFiles 'consumer-rules.pro'
        }
    }

Post a Comment for "Android - Proguard And Retrofit 2?"