Wednesday, January 13, 2016

Guard your Android App with Proguard

This is how the module level build.gradle should look like 

buildTypes {
  release {
      minifyEnabled true  <----------- This is important   
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro' signingConfig signingConfigs.release } debug { // applicationIdSuffix '.debug' debuggable true } }

proguard-android.txt - [ SDK_PATH/tools/proguard ]
proguard-rules.pro -  Can be found in your Android Studio Project.

What are the typical issues that one can run into ?

  • If you have included third party libraries then please include their Proguard config as well in your project
Example : Lets say we have included firebase library then following rules are really good to have in
proguard-rules.pro
                  
-keep class com.firebase.** { *; }
-keep class org.apache.** { *; }
-keepnames class com.fasterxml.jackson.** { *; }
-keepnames class javax.servlet.** { *; }
-keepnames class org.ietf.jgss.** { *; }
-dontwarn org.w3c.dom.**
-dontwarn org.joda.time.**
-dontwarn org.shaded.apache.**
-dontwarn org.ietf.jgss.**


  • If your model classed implement Parcelables then we often see the error , Class not found while unmarshalling
 
-keepnames class * implements android.os.Parcelable { *; }
-keepclassmembers class * implements android.os.Parcelable { *; }


  • If you have used EventBus in the project , then you might run into "onEvent" method not found cause Proguard might be stripping it 
-keepclassmembers class ** {
    public void onEvent*(***);}

  • Json property not found and not marked ignorable , this is common error seen if you have used JsonSchemaToPojo jackson convertor and your model class doesnt contain all the properties in the schema cause you are interested only in few properties. Add a class level Annotation
@JsonIgnoreProperties(ignoreUnknown = true)
public class Match implements Parcelable {

    @JsonProperty("date")
    private String date;    @JsonProperty("matchid")
    private String matchid;    @JsonProperty("matchno")
    private String matchno;    @JsonProperty("result")
    private String result;}
  •  Error on some devices : couldn't find class 'com.google.android.gms.measurement.internal.zzz'  
if project level build.gradle contains
classpath 'com.google.gms:google-services:1.5.0-beta'
change that to 
classpath 'com.google.gms:google-services:1.5.0'

1 comment:

  1. Use this for Retrofit Gson Error:
    +-keepclassmembers enum * { *; }

    ReplyDelete