From ff038707fde994148e4bcc769ee4c1d32b9e0dea Mon Sep 17 00:00:00 2001 From: Senthil Raja R Date: Tue, 16 Jun 2026 11:07:16 +0530 Subject: [PATCH] Add R8 keep rule for Hilt_* base classes to fix ClassCastException in release builds In R8 full mode (android.enableR8.fullMode=true), R8's aggressive vertical class merging optimization merges @AndroidEntryPoint classes with their generated Hilt_* base classes. This breaks the bytecode-transformed class hierarchy set up by the Hilt Gradle plugin, causing a ClassCastException at runtime when the Android system tries to create the service/activity. The existing keep rules only preserve @EntryPoint-annotated classes, but not the generated Hilt_* base classes themselves. Adding -keep class **.Hilt_* prevents R8 from merging these generated classes, preserving the class hierarchy required for Hilt's bytecode transformation to work correctly. Fixes #4668 --- .../META-INF/com.android.tools/proguard/hilt-android.pro | 8 +++++++- .../META-INF/com.android.tools/r8/hilt-android.pro | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/hilt-android/main/resources/META-INF/com.android.tools/proguard/hilt-android.pro b/hilt-android/main/resources/META-INF/com.android.tools/proguard/hilt-android.pro index 4e8b7f3c40b..8647f45d146 100644 --- a/hilt-android/main/resources/META-INF/com.android.tools/proguard/hilt-android.pro +++ b/hilt-android/main/resources/META-INF/com.android.tools/proguard/hilt-android.pro @@ -2,4 +2,10 @@ # See b/183070411#comment4 for more info. -keep,allowobfuscation,allowshrinking @dagger.hilt.internal.ComponentEntryPoint class * -keep,allowobfuscation,allowshrinking @dagger.hilt.internal.GeneratedEntryPoint class * --keep,allowobfuscation,allowshrinking @dagger.hilt.android.EarlyEntryPoint class * \ No newline at end of file +-keep,allowobfuscation,allowshrinking @dagger.hilt.android.EarlyEntryPoint class * + +# Keep Hilt generated base classes to prevent R8 full mode from merging them +# with @AndroidEntryPoint classes via vertical class merging. This merging +# breaks the bytecode-transformed class hierarchy set up by the Hilt Gradle +# plugin and causes ClassCastException at runtime (b/4668). +-keep class **.Hilt_* \ No newline at end of file diff --git a/hilt-android/main/resources/META-INF/com.android.tools/r8/hilt-android.pro b/hilt-android/main/resources/META-INF/com.android.tools/r8/hilt-android.pro index 4e8b7f3c40b..8647f45d146 100644 --- a/hilt-android/main/resources/META-INF/com.android.tools/r8/hilt-android.pro +++ b/hilt-android/main/resources/META-INF/com.android.tools/r8/hilt-android.pro @@ -2,4 +2,10 @@ # See b/183070411#comment4 for more info. -keep,allowobfuscation,allowshrinking @dagger.hilt.internal.ComponentEntryPoint class * -keep,allowobfuscation,allowshrinking @dagger.hilt.internal.GeneratedEntryPoint class * --keep,allowobfuscation,allowshrinking @dagger.hilt.android.EarlyEntryPoint class * \ No newline at end of file +-keep,allowobfuscation,allowshrinking @dagger.hilt.android.EarlyEntryPoint class * + +# Keep Hilt generated base classes to prevent R8 full mode from merging them +# with @AndroidEntryPoint classes via vertical class merging. This merging +# breaks the bytecode-transformed class hierarchy set up by the Hilt Gradle +# plugin and causes ClassCastException at runtime (b/4668). +-keep class **.Hilt_* \ No newline at end of file