proguard-rules.pro
Overview
The `proguard-rules.pro` file is a configuration file used by the [ProGuard](https://www.guardsquare.com/en/products/proguard) tool in Android projects to control code shrinking, optimization, obfuscation, and preverification during the build process. ProGuard helps reduce the size of the APK by removing unused code and renaming classes and members to short, meaningless names, thereby improving app performance and security against reverse engineering.
This file contains **project-specific ProGuard rules** that extend or override the default set of rules applied by the Android build system. It allows developers to specify which classes or members should be kept (excluded from removal or obfuscation), maintain debugging information, or configure specific behaviors for third-party libraries.
Detailed Explanation of Contents
Since `proguard-rules.pro` is a configuration file rather than a source code file with classes or functions, the documentation focuses on the key directives, their purpose, and usage examples.
Important Directives and Comments
1. Inclusion of Default ProGuard Rules
# By default, the flags in this file are appended to flags specified
# in /Users/stoyan/Library/Android/sdk/tools/proguard/proguard-android.txt
The project-specific rules in this file are applied after the default Android SDK ProGuard rules.
The default rules cover common Android framework classes and prevent breaking essential functionality.
The order of applying rules can be adjusted in the
build.gradlefile viaproguardFilesdirective.
2. Adding Project-Specific Keep Options
# Add any project specific keep options here:
Use
-keepdirectives here to specify classes or methods that ProGuard should not shrink or obfuscate.This is critical for classes accessed via reflection, native code, or external libraries.
3. Preserving JavaScript Interfaces in WebView
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
WebView JavaScript interfaces accessed via
addJavascriptInterfacerequire these classes/methods not to be obfuscated or removed.Replace
fqcn.of.javascript.interface.for.webviewwith the fully qualified class name (e.g.,com.example.MyJavaScriptInterface).This prevents runtime crashes caused by missing methods or classes.
4. Preserving Debugging Information
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
Keeps line number and source file information in the obfuscated bytecode.
Useful for debugging crashes, as stack traces will map correctly to source code lines.
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Hides the original source file name to add an additional layer of obfuscation.
Useful to prevent revealing source file names in stack traces.
5. Rules for Crashlytics
# For Crashlytics
-keep class com.crashlytics.** { *; }
-keep class com.crashlytics.android.**
These keep rules ensure that the Crashlytics SDK classes are not removed or obfuscated.
Crashlytics depends on certain classes and members being retained for proper crash reporting.
The double asterisk (
**) wildcard means "all sub-packages and classes."
Usage Examples
Example 1: Keeping a Class with Its Public Methods
-keep class com.example.MyClass {
public *;
}
Keeps the class
MyClassand all its public members from being removed or renamed.
Example 2: Keeping All Classes in a Package
-keep class com.example.mypackage.** { *; }
Keeps all classes and their members in the specified package and its subpackages.
Example 3: Keeping a WebView JS Interface
-keepclassmembers class com.example.MyJSInterface {
public *;
}
Prevents obfuscation/removal of the specified JavaScript interface class's public members.
Important Implementation Details
This file does not contain any executable code or classes, but instead provides rules that instruct ProGuard how to process the compiled bytecode.
The comments provide guidance on common scenarios requiring customization, such as WebView JavaScript interfaces and crash reporting tools.
The rules in this file are appended to the default Android SDK ProGuard rules, allowing selective overrides or additions.
Misconfiguration in this file (e.g., missing keep rules for reflection-based code) can cause runtime errors such as ClassNotFoundException or NoSuchMethodException.
Interaction with Other Parts of the System
This file is referenced in the Android app's
build.gradleunder theproguardFilesdirective:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
The build system uses this file during the release build process to optimize and obfuscate the APK.
It works alongside other ProGuard configuration files (default Android rules, third-party libraries' rules).
It affects the final APK size, security, and debuggability.
Specific rules ensure compatibility with third-party SDKs (e.g., Crashlytics) and Android platform features (e.g., WebView).
Visual Diagram: Flowchart of proguard-rules.pro Role and Workflow
flowchart TD
A[Source Code & Libraries] --> B[Compile to Bytecode]
B --> C[Default ProGuard Rules (proguard-android.txt)]
C --> D[Project-Specific Rules (proguard-rules.pro)]
D --> E[ProGuard Processing]
E --> F[Optimized & Obfuscated Bytecode]
F --> G[APK Packaging]
G --> H[Release APK]
subgraph ProGuard Configuration
C
D
end
The diagram illustrates how the
proguard-rules.profile fits into the Android build pipeline:After compiling source code and libraries to bytecode, ProGuard applies default rules.
Then, it applies the project-specific rules in
proguard-rules.pro.This combined configuration guides ProGuard in shrinking, optimizing, and obfuscating the code.
The resulting bytecode is packaged into the final APK for release.
Summary
The
proguard-rules.profile is essential for customizing ProGuard’s behavior in an Android project.It allows developers to protect critical classes/members from removal or obfuscation, especially those used via reflection or by third-party libraries.
Proper configuration ensures optimized APK size without breaking app functionality or crash reporting.
The file interacts closely with the Android build system and the default ProGuard rules.
Understanding and maintaining this file is vital for release builds and app security.
For more information on ProGuard and rule syntax, refer to the official guide: [Android ProGuard Guide](https://developer.android.com/studio/build/shrink-code) [ProGuard Manual](https://www.guardsquare.com/en/products/proguard/manual)