Strip out unused code and libraries with Gradle

Sometimes, we want to write code just for debugging purpose, and we don’t want this code to live in our production. Furthermore, we don’t want to include any libraries that we are using just for debug and testing purposes, in our production apk.

A very good example will be Stetho. Stetho is a great library when we want to browse our database very easily, but it also includes a bunch of code and classes (almost 2K methods) that we have no reason to include in our apk file. Of course, we think that proguard supposed to do this, but if our code includes a method from Stetho library, proguard won’t strip the library out from the apk file, and when we want to shrink our apk as much as possible, all this code not helps us.

So we can do this Gradle. By default, in Android Studio, all the code of our application lives under app/src/main/java folder (and the code for tests in androidTest and test folder), But we can have a different code for different flavours of our app. By default, our app will have 2 flavours: release and debug. You can switch this flavours in the left pane of android studio:

 

 

 

By adding folders to your app directory, you can have 2 different classes with the same signature for each flavour: for release flavour, you need app/src/release/java folder, and for debug flavour you need app/src/debug/java folder.

 

 

 

 

 

Now we can put in this folders duplicate classes, and the build system will take the class belongs to the current flavour. Let’s demonstrate it: We want to use Stetho just in debug flavour, so in our Application we can put this line inside the onCreate():

DebugInitials.init(this);

And in the app/src/debug/java folder we will create DebugInitials.class like this:

public class DebugInitials {
    public static void init(Application application){
        //Exists just in debug build
        Stetho.initializeWithDefaults(application);
    }
}

In the app/src/release/java folder we will create DebugInitials.class like this:

public class DebugInitials {
    public static void init(Application application){

    }
}

This way, Stetho code will live just in debug flavour. Now we want to tell gradle to include Stetho library just in debug. This done very easily:

debugCompile 'com.facebook.stetho:stetho:1.4.2'

And that’s it! Stetho won’t be included any more in our production apk.

Leave a Reply

Your email address will not be published. Required fields are marked *