Skip to content
tezvyn:
Menu

ProGuard Keep Rules: A "Do Not Remove" List for Your Code

Source: developer.android.comMediumHow cards are made

ProGuard Keep Rules: A "Do Not Remove" List for Your Code

ProGuard keep rules are a "do not remove" list for Android's code shrinker, telling it what to spare when it can't detect usage. This is crucial for code accessed via reflection or JNI.

Why it exists

To reduce your app's size, Android's build tools use a shrinker (like ProGuard or R8) to remove unused code. However, this automated process can't always detect code that is used indirectly, such as through reflection. Removing this "invisibly" used code causes your app to crash at runtime. Keep rules exist to prevent this.

The mental model

Think of keep rules as a "do not remove" list you give to an aggressive decluttering service. The service (R8) automatically throws away anything it thinks is junk. Your list tells it, "Even though you don't see me using this item, it's an heirloom. Do not throw it away." You are manually overriding the automated logic to prevent mistakes.

How it works

You define these rules in a proguard-rules.pro file using a specific syntax. A rule like -keep class com.example.MyModel { *; } instructs the shrinker to preserve the MyModel class and all of its members (fields and methods) from being removed or renamed (obfuscated). Many third-party libraries that use reflection already include their own keep rules, which are automatically applied to your build.

When to use it

You need keep rules primarily in three situations. First, when your app uses reflection to access code by its string name, which is common with serialization libraries like Gson or Moshi. Second, when interacting with native code through the Java Native Interface (JNI), as the build tools can't see calls from C/C++ into your Java/Kotlin code. Third, for any classes that are only referenced in Android Manifest files or XML layouts, like custom Views.

When not to use it

You don't need keep rules for code that is directly called from other parts of your app's source. The biggest anti-pattern is writing overly broad rules, like -keep class com.example.* { ; }, which effectively disables shrinking for an entire package. Be as specific as possible, as broad rules defeat the purpose of using a shrinker.

One canonical example

A data class used for JSON parsing with a library like Gson. Gson uses reflection to create objects and set their fields from a JSON string. Without a keep rule, R8 might remove the class's empty constructor or fields, causing a runtime crash. A rule like -keep class com.myapp.data.User { <init>; public *; } ensures the User class, its constructor, and all its public members are preserved.

Interview question

Which situation most critically necessitates defining a ProGuard keep rule to avoid runtime crashes?

  • a.A class whose methods are frequently invoked directly by other classes within the application.
  • b.A data class that is only referenced in the Android Manifest file or an XML layout.Correct
  • c.A utility class that is very large and contributes significantly to the app's overall size.
  • d.A class whose name needs to remain unchanged for easier debugging in production.
Why?

A class referenced only in the Android Manifest or XML layout is not directly called from code, making its usage invisible to the shrinker, thus requiring a keep rule to prevent its removal and subsequent crashes. While keep rules do prevent obfuscation, the primary and most critical reason for their necessity to prevent runtime crashes is the removal of code that is used indirectly and thus undetectable by the shrinker, not merely for debugging convenience.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles