question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Hilt] Bazel HiltViewModel inject java.lang.RuntimeException: Cannot create an instance of class MyViewModel

See original GitHub issue

After 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:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
danysantiagocommented, Jun 17, 2021

I think this might also be due to something else…

If androidx_hilt_compiler_plugin is for androidx.hilt:hilt-compiler then that is the wrong processor to apply, the ViewModel functionality was moved to Hilt’s main compiler during the alpha cycles. You should be applying the com.google.dagger:hilt-compiler instead.

Release migration steps in the release notes here: https://github.com/google/dagger/releases/tag/dagger-2.34

0reactions
geadencommented, Jun 24, 2021

I see, thanks. I see you already found the issue filed on the Kotlin rules side bazelbuild/rules_kotlin#514 so I’m not sure there is much we can do on the Hilt side.

Ok, thanks! I’m closing the issue.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found