Skip to content

Crash in release build after updating to AGP 8.9.2 + Hilt 2.56.2: @HiltViewModel constructor stripped #4739

Open
@javadjafari1

Description

@javadjafari1

After updating both AGP to 8.9.2 and Hilt to 2.56.2, my app crashes in release builds due to a NoSuchMethodException when trying to create a @hiltviewmodel instance. The constructor appears to be stripped by R8.

Interestingly, this crash does not occur if I update only Hilt to 2.56.2, I found that this commit removed the keep rule:

-keepnames @dagger.hilt.android.lifecycle.HiltViewModel class * extends androidx.lifecycle.ViewModel

When I re-added this rule manually to my proguard-rules.pro, the crash was resolved.

Caused by: java.lang.NoSuchMethodException: <init> []
Caused by: java.lang.RuntimeException: Cannot create an instance of class x.x.x.MyViewModel
	at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.create(SourceFile:18)

Steps to Reproduce:

  1. Use Hilt 2.56.1 + AGP 8.9.2
  2. Create a simple @hiltviewmodel class with an injected constructor
  3. Inject it in an @androidentrypoint activity using by viewModels()
  4. Enable minification (minifyEnabled true) in release build
  5. Build and run → crash on launch

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions