[Hilt] Bazel HiltViewModel inject java.lang.RuntimeException: Cannot create an instance of class MyViewModel
See original GitHub issueAfter following instruction on integrating with Bazel, I’m getting the follwing exception
/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.app, PID: 10134
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MyActivity}: java.lang.RuntimeException: Cannot create an instance of class com.example.app.MyViewModel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.example.app.MyViewModel
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278)
at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:112)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:54)
at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41)
at com.example.app.MyActivity.getViewModel(MyActivity.kt:51)
at com.example.app.MyActivity.onCreate(MyActivity.kt:55)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.InstantiationException: java.lang.Class<com.example.app.MyViewModel> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create
MyViewModel.kt
@HiltViewModel
class MyViewModel @Inject constructor(private val repository: MyRepository) {
...
}
MyActivity.kt
@AndroidEntryPoint
class MyActivity : AppCompatActivity() {
private val viewModel by viewModels<MyViewModel>()
...
}
BUILD.bazel
kt_android_library(
name = "mylib",
...
plugins = [
"//:androidx_hilt_compiler",
],
deps = [
"//:hilt-android",
"@maven//:androidx_hilt_hilt_lifecycle_viewmodel",
]
)
Plugin is declared as:
kt_compiler_plugin(
name = "androidx_hilt_compiler_plugin",
id = "androidx.hilt.compiler",
visibility = ["//visibility:public"],
deps = [
"@maven//:androidx_hilt_hilt_compiler",
],
)
Previously opened issue #2147 is related to Gradle, but didn’t found a solution for Bazel.
Dagger/Hilt version 2.37
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
HiltViewModel: Cannot create an instance of class
To scope a dependency to a ViewModel use the @ViewModelScoped annotation. A @ViewModelScoped type will make it so that a single instance of...
Read more >Hilt View Models - Dagger
A Hilt View Model is a Jetpack ViewModel that is constructor injected by Hilt. To enable injection of a ViewModel by Hilt use...
Read more >I have an error with hilt i dont know why I keep getting the
I have an error with hilt i dont know why I keep getting the error java lang RuntimeException Cannot create an instance of...
Read more >[Solved]-Cannot create an instance of class ViewModel with ...
Coding example for the question Cannot create an instance of class ViewModel with constructor-kotlin.
Read more >Dagger Hilt — ViewModelClass has no zero argument ...
If you are using Dagger Hilt on your Android project, you've probably seen ... is to annotate the class with @HiltViewModel and add...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think this might also be due to something else…
If
androidx_hilt_compiler_plugin
is forandroidx.hilt:hilt-compiler
then that is the wrong processor to apply, theViewModel
functionality was moved to Hilt’s main compiler during the alpha cycles. You should be applying thecom.google.dagger:hilt-compiler
instead.Release migration steps in the release notes here: https://github.com/google/dagger/releases/tag/dagger-2.34
Ok, thanks! I’m closing the issue.