Add R8 keep rule for Hilt_* base classes to fix ClassCastException in release builds#5195
Open
Senthil455 wants to merge 1 commit into
Open
Add R8 keep rule for Hilt_* base classes to fix ClassCastException in release builds#5195Senthil455 wants to merge 1 commit into
Senthil455 wants to merge 1 commit into
Conversation
… 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.ClassCastExceptionThis 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
Fixes #4668