I migrated my app from dagger2 to hilt manually. i didn't face any compilation or build errors. but when I try to run my app, it crashes at the splash screen with this stack:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: pfe.usdb.dictili, PID: 7815
java.lang.RuntimeException: Unable to start activity ComponentInfo{pfe.usdb.dictili/pfe.usdb.dictili.view.auth.SplashScreen}: java.lang.RuntimeException: Cannot create an instance of class pfe.usdb.dictili.viewmodel.auth.AuthVModel
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 pfe.usdb.dictili.viewmodel.auth.AuthVModel
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:106)
at androidx.hilt.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:74)
at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:69)
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 pfe.usdb.dictili.view.auth.SplashScreen.getViewModel(Unknown Source:2)
at pfe.usdb.dictili.view.auth.SplashScreen.onCreate(SplashScreen.kt:46)
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<pfe.usdb.dictili.viewmodel.auth.AuthVModel> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278)
at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:106)
at androidx.hilt.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:74)
at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:69)
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 pfe.usdb.dictili.view.auth.SplashScreen.getViewModel(Unknown Source:2)
at pfe.usdb.dictili.view.auth.SplashScreen.onCreate(SplashScreen.kt:46)
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)
it says it cannot create an instance of that viewmodel because it doesn't have a zero argument constructor, but: 1- my viewmodel class is injected with hilt (BaseViewModel is abstract):
class AuthVModel @ViewModelInject constructor(
private val sharedPreferences: PreferencesRepository,
private val authRepository: AuthRepository
) : BaseViewModel()
2- my activity is annotated with AndroidEntryPoint (BaseActivity is abstract):
@AndroidEntryPoint
class SplashScreen : BaseActivity() {
private val viewModel: AuthVModel by viewModels()
3- the repositories the viewmodel is depending on are injected properly and android studio 4.1.1 is showing the injection icon on the left:
class AuthRepository @Inject constructor(
@NonAuthenticatedAuthenticationService private val unauthorizedService: AuthService,
@AuthenticatedAuthenticationService private val authorizedService: AuthService
)
class PreferencesRepository @Inject constructor(
private val sharedPreferences: SharedPreferences
)
I don't know what's happening here, this is not my first time working with this, since I built some apps recently just like this (some code is 100% copied from these projects).
Did you use the required dependency for jetpack components (ViewModel in your case) in your module's gradle.build
file?
Namely:
kapt "androidx.hilt:hilt-compiler:$hiltJetpackVer"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:$hiltJetpackVer"
Note that the version is different than the one used for the main hilt dependency.