Monday, May 16, 2016

ProGuard exceptions for Retrofit in an Android project

If you are enabling ProGuard to minify an Android APK, then it will be necessary to tell ProGuard to leave the Retrofit and related JSON data classes as they are. Otherwise, you will get class not found errors when running the Android app.

In Android Studio, to add the exceptions to ProGuard, open up the file proguard-rules.pro in the editor. Add in the following lines.


# for Retrofit
-keepattributes Signature
-keepattributes Exceptions

-keep class com.squareup.** { *; }
-keep interface com.squareup.** { *; }
-keep interface retrofit.** { *;}
-keep class retrofit.** { *; }

-keepclasseswithmembers class * {
    @retrofit.http.* <methods>;
}

-dontwarn com.squareup.okhttp.**
-dontwarn rx.**
-dontwarn retrofit.**

# add your JSON classes here
-keep class com.dom.mypackage.myclass.** { *; }
Note: for the last line above, replace it with the path to your classes that correspond to the JSON classes you are using in Retrofit.

No comments:

Post a Comment