Andriod Proguard rules

Sanjana Human In Tech
2 min readMay 23, 2024

--

ProGuard is a tool used for code shrinking, optimization, and obfuscation in Android applications. It helps to reduce the size of the APK file and make the code more difficult to reverse-engineer. When enabling ProGuard in your Android project, it’s essential to configure ProGuard rules to ensure that it doesn’t remove or obfuscate classes, methods, or resources that your application requires.

Here’s a basic example of ProGuard rules commonly used in Android applications:

# Keep classes that are used for entry points (e.g., Activities, Services, Receivers)
-keep class com.example.app.MainActivity
-keep class com.example.app.MyService
-keep class com.example.app.MyReceiver

# Keep the application class
-keep class com.example.app.MyApplication

# Keep classes that are referenced in the AndroidManifest.xml file
-keep class com.example.app.model.** { *; }
-keep class com.example.app.view.** { *; }

# Keep all classes in a specific package and its subpackages
-keep class com.example.app.network.**

# Keep all public and protected methods in classes
-keepclassmembers class * {
public protected *;
}

# Keep all classes that implement Parcelable or Serializable interfaces
-keepclassmembers class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}

# Keep all enums
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}

# Keep all annotations
-keepattributes *Annotation*

# Keep all native method names and arguments
-keepclasseswithmembernames class * {
native <methods>;
}

# Keep all dynamic method invocations
-keepclassmembers,allowshrinking class * {
*** onCreate(...);
*** onStart(...);
*** onResume(...);
*** onPause(...);
*** onStop(...);
*** onDestroy(...);
}

These rules cover common scenarios, such as keeping specific classes, keeping methods and fields, preserving classes used for Android entry points (like activities, services, and receivers), and retaining classes referenced in the AndroidManifest.xml file.

You might need to customize these rules based on your specific application dependencies and requirements. It’s important to test your application thoroughly after applying ProGuard rules to ensure that everything works as expected. Additionally, you may need to periodically review and update your ProGuard rules as your application evolves.

--

--

Sanjana Human In Tech
Sanjana Human In Tech

Written by Sanjana Human In Tech

A React Native front-end enthusiast and dedicated development engineer, eager to expand knowledge on development techniques and collaborate with others.

No responses yet