Proguard and Ionic

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

1 Like

where to find proguard-rules. pro or should i create it and is there a cli command for proguard like the jarsigner ,keytool etc…

@Rasioc Hey … i am new in android development ( i’m iOS Developer since 5 years) have struggled a lot for same thing for the cordova application to minified it and i did as well but due to not loading facing Error message Failed to Create webView as its was converting all the files but tries your set of rules which really awesome it worked for me in first occurrence . i want to say thanks a lot for providing such a really much information thing. Thanks

Hi
Can anyone help me…
I am able to do use proguard in Android but how to do in IOS ? Please reply unable to understand what are the steps for doing proguard in IOS

Proguard is an Android thing, and not available on iOS.

Hi Sigmund,

Thanks for the reply. Do you know the good alternative for iOS obfuscation. I am stuck.

1 Like

I’m searching an alternative too

Did anybody manage to get this running on Cordova-Android 7 yet?
edit: Ok got it, will update original post

OK seems like I cannot update my original post anymore :frowning:

Here are changes needed for Cordova-android 7:

In Step 2)
The gradle file you should modify is now a different one, its platforms/android/app/build.gradle (notice the /app subfolder)
The proguard-rules.pro file must be stored in the same folder as this file (or you adjust the path within the build.gradle).

cordova-plugin-camera plugin image capture not work but choose gallery image works fine.
but I have solved this problem.
I am use this proguard rules:

File Name: proguard-rules.pro

# ionic
-keep class org.apache.cordova.** { *; }
-keep class org.apache.cordova.camera.** { *; }
-keep class org.apache.cordova.** { *; }
-keep public class * extends org.apache.cordova.CordovaPlugin
-keep class com.ionic.keyboard.IonicKeyboard.** { *; }

# Your App id like:  'com.abc.xyz'
-keep class com.abc.xyz.BuildConfig { *; }

#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;
}

ProGuard File - https://github.com/Riyaz0001/cordova-plugin-proguard

Hi Rasioc, this above process is not working for My IONIC 3 project.

In my project.properties, i don’t found " proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt" line, but i has wrote that line manually. then crate ’ proguard-rules.pro’ file manually and do all steps but my apk file is not secure?
I am unable to impliment progaurd in my ionic project.
Can you help me?

I searched high and low on the internet and couldn’t find the answer, but I got lucky and figured it out! I’m still on Ionic v3, but this will probably work with v4 as well.

  1. Create a file in the src/assets/ folder called proguard-custom.txt and add your custom attributes here.
    • In my case cordova-plugin-proguard was having issues with cordova-plugin-ionic-webview so I added these lines to resolve that
      -keep class com.ionicframework.cordova.webview.** { *; } 
      -keep class com.ionicframework.cordova.webview.*
      
  2. If you already have cordova-plugin-proguard installed on your project, remove it and re-add it. Adding the plugin grabs your configs from your custom .txt file.
    ionic cordova plugin remove cordova-plugin-proguard
    ionic cordova plugin add cordova-plugin-proguard
    
  3. Add / remove the Android platform
    ionic cordova platform rm android
    ionic cordova platform add android
    
  4. You can verify that your custom settings have been added by looking at the /platforms/android/app/src/main/assets/www/proguard-custom.txt file. You will see all the default options that the cordova-plugin-proguard plugin adds, but at the bottom of the file you should see everything that you added to your proguard-custom.txt file!

Just remember that anytime you change the src/assets/proguard-custom.txt file, you have to remove and add the cordova-plugin-proguard plugin.

1 Like

Hello As you mention above steps I followed the same in ionic 3 but I can not find any progress in APK Decompilere online tool ‘APK Decompiler’ after ‘ionic cordova build android --minifycss --optimizejs --minifyjs --release --prod’ my plugins and resources easly decompiled.