April 23, 2016

The idea behind making more downloads of your app is of samller size of the apk. There are many ways to make the apk smaller. One of the best way is to use Proguard. Proguard not only make the apk smaller but it makes the code obfuscate too. But the question is how does the Proguard work. I’ll explain that with a simple example.

Suppose you are making an app of Live Wallpaper, generally you will use AndEngine to do this. We will add the library in the gradle as

compile project(':AndEngine')
compile project(':AndEngineLiveWallpaperExtension')

In the AndEngine and the AndEngineLiveWallpaperExtension library there are many methods which we are not even using while making the LiveWallpaper app. But still those methods are occupying the space in the apk, which results in higher size of the apk. Here comes the use of Proguard. Proguard will detect and remove unused classes, fields, methods, and attributes from your packaged app, including those from included code libraries. ProGuard also optimizes the bytecode, removes unused code instructions, and obfuscates the remaining classes, fields, and methods with short names. The obfuscated code makes your APK difficult to reverse engineer.

Now comes the next part, how to use the Proguard.In order to use the Proguard we just have to make minifyEnabled as true, like this.


release {
            minifyEnabled true
           proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
 }

We can find out the declaration of the ‘proguard-android-optimize.txt’ under the sdk/tools/proguard. In this we can also define our own set of Proguard rules in the ‘proguard-rules-pro’.

Suppose in your application you are implementing the ad network, mainly of Mopub, AppBrain, AdColony, and the AppLovin then in the ‘proguard-rules-pro’ we have to define our custom rules of Proguard. Like below :


# AppBrain SDK:
-keep public class com.appbrain.KeepClass
-keep public class * implements com.appbrain.KeepClass
-keepclassmembers class * implements com.appbrain.KeepClass {
    ;
}
-keepattributes *Annotation*
-keep class android.webkit.JavascriptInterface
-dontwarn android.webkit.JavascriptInterface

# AdColony SDK:
-dontwarn com.jirbo.adcolony.**
-keep class com.jirbo.adcolony.**{*;}

# MoPub SDK:
-keepclassmembers class com.mopub.** { public *; }
-keep public class com.mopub.**
-keep public class android.webkit.JavascriptInterface {}

-keep class * extends com.mopub.mobileads.CustomEventBanner {}
-keep class * extends com.mopub.mobileads.CustomEventInterstitial {}
-keep class * extends com.mopub.mobileads.CustomEventNative {}

# App Lovin SDK:
-dontwarn com.applovin.**
-keep public class com.applovin.**.*
-keep class com.google.android.gms.ads.identifier.** { *; }

Few things to remember abou the Proguard :

1. While enabling the Proguard it will slow down the build time.
2. Suppose we have a class named Test, and we don’t want to implement proguard in it, then we will just use -keep public class Test, in this we are not allowing the proguard to this class.

Below is the sample of enabling the Proguard:


 buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }

        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

In the next tutorial I’ll show you how to decode your obfuscated code.