Keep Rules, Mapping Files & Retrace
R8 only sees code that is referenced. Anything reached another way (by reflection, from native code, from a class name in a string or an XML file, by a serialization library) looks dead and gets removed or renamed. The app builds fine and crashes on the user's phone. Keep rules tell R8 about those hidden entry points.
The anatomy of a rule
proguard
-keep class com.example.hello.ShoutPlugin { <init>(); }
# ^ ^ ^
# | class specification members to keep (here: the no-arg constructor)
# keep option| Option | Keeps from removal | Keeps from renaming |
|---|---|---|
-keep |
the class and the listed members | yes |
-keepclassmembers |
listed members, if the class survives anyway | yes |
-keepclasseswithmembers |
classes that have all listed members | yes |
-keepnames (= ,allowshrinking) |
nothing (may be removed if unused) | yes |
-keepattributes Signature,*Annotation* |
generic signatures / annotations | (attributes) |
-keepattributes SourceFile,LineNumberTable |
file names and line numbers in stack traces | (attributes) |
Write them as narrowly as possible: -keep class com.example.** { *; } keeps your whole app un-shrunk and un-obfuscated, which defeats the point.
A reflection bug, then the fix
MainActivity loads a plugin by class name, the way plugin systems, dependency injection containers and serialization libraries do. Nothing references ShoutPlugin directly:
bash · try it yourself
mkdir -p hello/app/src/main/java/com/example/hello && cd hello
cat > settings.gradle.kts <<'KTS'
pluginManagement { repositories { google(); mavenCentral(); gradlePluginPortal() } }
dependencyResolutionManagement { repositories { google(); mavenCentral() } }
rootProject.name = "hello"
include(":app")
KTS
echo 'plugins { id("com.android.application") version "9.4.1" apply false }' > build.gradle.kts
cat > app/src/main/AndroidManifest.xml <<'XML'
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:label="Hello">
<activity android:name=".MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
XML
cat > app/src/main/java/com/example/hello/Plugin.java <<'JAVA'
package com.example.hello;
public interface Plugin { String name(); }
JAVA
cat > app/src/main/java/com/example/hello/ShoutPlugin.java <<'JAVA'
package com.example.hello;
public class ShoutPlugin implements Plugin {
public String name() { return "SHOUT"; }
}
JAVA
cat > app/src/main/java/com/example/hello/MainActivity.java <<'JAVA'
package com.example.hello;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
static Plugin load(String className) throws Exception {
return (Plugin) Class.forName(className).getDeclaredConstructor().newInstance();
}
@Override protected void onCreate(Bundle state) {
super.onCreate(state);
String label;
try { label = load("com.example.hello.ShoutPlugin").name(); }
catch (Exception e) { label = "no plugin: " + e; }
TextView view = new TextView(this);
view.setText(label);
setContentView(view);
}
}
JAVA
cat > app/build.gradle.kts <<'KTS'
plugins { id("com.android.application") }
android {
namespace = "com.example.hello"
compileSdk = 36
defaultConfig { applicationId = "com.example.hello"; minSdk = 24; targetSdk = 36; versionCode = 1; versionName = "1.0" }
buildTypes {
release {
isMinifyEnabled = true
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
}
KTS
touch app/proguard-rules.pro
gradle wrapper > /dev/null
./gradlew -q assembleRelease
echo "=== without a keep rule"
grep '^com.example' app/build/outputs/mapping/release/mapping.txt
echo "removed: $(grep -E '^com.example.hello.[A-Za-z]+