Skip to content

Add R8 keep rule for Hilt_* base classes to fix ClassCastException in release builds#5195

Open
Senthil455 wants to merge 1 commit into
google:masterfrom
Senthil455:hilt-r8-fullmode-fix
Open

Add R8 keep rule for Hilt_* base classes to fix ClassCastException in release builds#5195
Senthil455 wants to merge 1 commit into
google:masterfrom
Senthil455:hilt-r8-fullmode-fix

Conversation

@Senthil455

Copy link
Copy Markdown

Problem

When using @androidentrypoint on a class (e.g., a Service, Activity, or BroadcastReceiver) with R8 full mode enabled (�ndroid.enableR8.fullMode=true), the app crashes at runtime with:

java.lang.RuntimeException: Unable to create service *.presentation.service.PushMessagingService: java.lang.ClassCastException

This works correctly in debug builds and when R8 full mode is disabled.

Root Cause

In R8 full mode, the aggressive vertical class merging optimization merges @androidentrypoint user classes with their generated Hilt_* base classes when the user class has no additional methods. For example, PushMessagingService (which is : FirebaseMessagingService()) would be merged with Hilt_PushMessagingService into a single class.

This merging breaks the bytecode-transformed class hierarchy that the Hilt Gradle plugin set up via AndroidEntryPointClassVisitor. The plugin rewrites @androidentrypoint classes to extend Hilt_* instead of their original superclass. When R8 undoes this by merging the classes, a ClassCastException occurs at runtime when the Android system tries to create the service.

Fix

Added -keep class .Hilt_ to the Hilt Android proguard rules. This prevents R8 from merging the generated Hilt_ base classes with user classes, preserving the class hierarchy required for Hilt's bytecode transformation to work correctly.

The existing keep rules only preserve @EntryPoint-annotated classes, but not the generated Hilt_* base classes themselves.

Changes

  • hilt-android/main/resources/META-INF/com.android.tools/r8/hilt-android.pro: Added keep rule
  • hilt-android/main/resources/META-INF/com.android.tools/proguard/hilt-android.pro: Added keep rule

Fixes #4668

… 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 google#4668
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ClassCastException (Unable to create service) in release builds

1 participant