Hi, I did not find any up to date explanation of how ot use Proguad with Ionic 2, so I had to go through several different explanations figuring out what works with latest versions of android, proguard, cordova and ionic 2.
I’m sharing the solution I came up with, which seems to work well with my app, so others can use it and to get feedback if I missed something.
1. The information for proguard contained in the platform project seems to be outdated for latest android versions:
In platforms/android/project.properties you will find this hint:
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
However, I found out we should ignore it since it does not work anymore (has no effect) and the referenced proguard-project.txt seems to be missing anyway.
2. Enable Proguard
You will have to modify platforms/android/build.gradle instead and add some configuration to buildTypes.release section:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),
'proguard-rules.pro'
...
}
}
Hint: I have used proguard-android-optimize.txt as suggested in https://developer.android.com/studio/build/shrink-code.html#enabling-gradle which seems to enable more shrinking than proguard-android.txt. Did not find any problems with it.
The proguard-rules.pro file is a file with your custom configurations. In old cordova project this is referenced as “proguard-project.txt”, however I used here the name used by Android Studio normally and which is used in android documenations. The file has to be in the same folder as the build.gradle file, or you have to adjust the path to it.
3. Add required proguard rules
You will have to exclude some classes from being stripped away or from being renamed. I assembled several examples I found and came up with following solution (add them in proguard-rules.pro):
# ionic
-keep class org.apache.cordova.** { *; }
-keep public class * extends org.apache.cordova.CordovaPlugin
-keep class com.ionic.keyboard.IonicKeyboard.** { *; }
#admob
-keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents();
}
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
public static final *** NULL;
}
-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
@com.google.android.gms.common.annotation.KeepName *;
}
-keepnames class * implements android.os.Parcelable {
public static final ** CREATOR;
}
-keep public class com.google.cordova.admob.**
# Not sure if needed, found it in several documentations
-keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents();
}
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
public static final *** NULL;
}
-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
@com.google.android.gms.common.annotation.KeepName *;
}
-keepnames class * implements android.os.Parcelable {
public static final ** CREATOR;
}
The first block seems to be needed for Ionic & Cordova.
The second one is needed by AdMob plugin (if you use it)
I’m not so sure about the 3rd block. I found it in several documentations and examples, however I’m not sure if you really need it. I just added it to be sure. Feel free to play around with it and give some feedback.
Some other cordova plugins require also special rules, so keep an eye open on their documentations for hints regarding ProGuard.
4. Add your owns project proguard rules
You might need more rules if you have added platform specific code in some cases, however, I don’t need anything else than above since I don’t have this.
5. IMPORTANT: Make sure to re-enable and configure proguard after reinitializing/re-adding android platform!
Probably your config will be lost when re-adding/initializing android platform so you will have to do it again. In my case I checked in build.gradle + proguard-rules.pro into my projects VCS to make sure I see if anything changed there. Maybe adding a hook which ensures that the config is there or even adds it would make sense. (Does somebody want to share some hook here, maybe?)
That’s it! Recompile your project using ionic build android --release --prod
and pray hard while opening the app ;). If it crashes see logcat output to find out what classes its complaining about. You should test the app well before releasing it, especially any interaction with native plugins.
Hint:
This not only obfuscates the apps code but also reduces the size of your app and according to androids documentation it will also speed it up. In my case:
APK without proguard: 7,33 MB
APK with proguard: 7,25 MB
Installed app without proguard: 16,18 MB
Installed app with proguard: 12,98 MB
Any Feedback? Did you come up with more rules needed?
Greetings
Ralf