Android Studio – 28 – Proguard and Code Obfuscation

Proguard is a code shrinking and obfuscation tool included with the Android Gradle plugin. It is designed to help reduce the size of your Android app and protect your code from reverse engineering. Code obfuscation is the process of making your code more difficult to understand by renaming classes, methods, and variables to shorter, less descriptive names. This makes it challenging for attackers to reverse engineer your app’s source code. In this guide, we’ll explore Proguard and code obfuscation in Android Studio, its significance, and the steps to follow, along with code examples and commands for illustration.

The Significance of Code Obfuscation

Code obfuscation serves several important purposes in Android app development:

  1. Code Protection: Obfuscation makes it more challenging for attackers to reverse engineer your app and extract sensitive information or vulnerabilities.
  2. Reduced APK Size: By renaming classes, methods, and variables to shorter names, Proguard can significantly reduce the size of your app’s APK, making it faster to download and install.
  3. Improved App Performance: Smaller APKs can lead to improved app performance, especially on devices with limited storage and slower internet connections.
  4. Enhanced Security: While code obfuscation is not a substitute for other security measures, it adds an additional layer of protection to your app.

Using Proguard for Code Obfuscation

Proguard is included with the Android Gradle plugin, making it easy to enable code obfuscation in your Android Studio project. Here are the steps to follow:

1. Enable Proguard:

Open your app’s build.gradle file and ensure that Proguard is enabled. By default, Proguard is enabled for release builds. You can find the Proguard configuration block under the buildTypes section:

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

The minifyEnabled property should be set to true to enable Proguard.

2. Proguard Configuration:

Proguard uses a configuration file (proguard-rules.pro) to define which classes, methods, and variables should be obfuscated and which should be kept intact. You can create and customize this file in your project. Here’s an example of a simple Proguard configuration:

-dontobfuscate
-dontoptimize
-keep class com.example.myapp.model.** { *; }
-keepclassmembers class com.example.myapp.model.** { *; }

In this example, we’re preserving the structure of the com.example.myapp.model package and keeping all classes and class members intact. You can customize this configuration based on your app’s needs.

3. Run Proguard:

Build your release APK, and Proguard will automatically run as part of the build process. You can do this from Android Studio or by running the following command in your project’s root directory:

./gradlew assembleRelease

Proguard will process your code according to the rules defined in the proguard-rules.pro file and generate an obfuscated APK.

4. View Proguard Output:

After building the release APK, you can inspect the Proguard output to see how your code has been obfuscated. The output is usually located in the build/outputs/mapping/release directory of your project.

Example: Code Obfuscation with Proguard

Let’s illustrate the process with a simple example. Suppose you have the following class in your app:

package com.example.myapp.model;

public class UserModel {
    private String username;
    private String password;

    public UserModel(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }
}

And here’s a Proguard configuration that preserves this class:

-keep class com.example.myapp.model.** { *; }

After running Proguard, the class may be obfuscated to something like this:

package com.example.myapp.model;

public class a {
    private String a;
    private String b;

    public a(String a, String b) {
        this.a = a;
        this.b = b;
    }

    public String a() {
        return a;
    }

    public String b() {
        return b;
    }
}

Proguard has renamed the class and its members to shorter, less descriptive names, making it more challenging to understand the original code.

Conclusion

Code obfuscation with Proguard is an important step in Android app development to protect your code from reverse engineering and reduce the size of your app’s APK. By enabling Proguard in your project and configuring it according to your app’s needs, you can enhance the security and performance of your Android app. While code obfuscation is not a silver bullet for security, it is a valuable tool in your overall security strategy.