Main page

How to setup Proguard in Cordova application?


proguard optimization performance configuration cordova mobile github english

Intro

Some time ago I was involved into design of architecture one mobile app which was based on AngularJS 1.6 framework. One of the main concerns of the app were performance. So perf improvements were made on different steps of the developing cycles. What actually were done:

The first option I've tried to cover in my recent posts Is your npm install still too slow?, How to migrate your SPA from JSPM/SystemsJS to Webpack? and How to separate your Webpack configs?

In this post I'd like to talk about 3rd step. In particular, how to improve your .apk package for Android. The great tool for it is Proguard.

Proguard

ProGuard is the most popular optimizer for Java bytecode. It makes your Java and Android applications up to 90% smaller and up to 20% faster. ProGuard also provides minimal protection against reverse engineering by obfuscating the names of classes, fields and methods.

More about this tool you can find on the offical website.

The description of the proguard sounds really great! But how we can use it with our existing app based on Cordova. After quick investigation I didn't find any cordova plugins for proguard at that moment. And I was super wondered that this plugin still didn't exist. So let's do it by yourself! ;)

cordova-plugin-proguard

So I've created my cordova-plugin-proguard and published it under MIT license on github.

Using of the plugin is pretty straightforward. What you can need is just fork the repo and add rules which specific for your application.

For example if you developing plugin for Google Maps, so likely you should add these rows into proguard-custom.txt:

-keep public class * extends plugin.google.maps.MyPlugin
-keepclassmembers public class plugin.google.maps.GoogleMaps {*; }
-keepclassmembers public class * extends plugin.google.maps.MyPlugin { *; }

How to install plugin in cordova app

  1. First of all you need to be sure that I've installed cordova globally
npm install -g cordova
  1. In a separate directory, create a new Cordova project:
cordova create cordova-plugin-test
  1. Add the platforms you need(in our case we need just Android):
cd cordova-plugin-test
cordova platform add android
  1. install cordova-plugin-proguard
cordova plugin add cordova-plugin-proguard
  1. Build the Cordova app:
cordova build android

Note: if you already have an app where you want to install cordova-plugin-proguard so you have to skip 1, 2, 3 steps and start from 4th step.

Please feel free to contribute in this plugin on github and ask me anything regarding it in comments.

Links

Happy obfuscation! ✌🏼