Thread
#dagger
    Hovhannes

    Hovhannes

    1 year ago
    Hello everybody, I want to convert Dagger into Hilt, but I'm new at Hilt. I get these non  understandable errors. What mistakes did I make?  Thanks in advance.
    error: [Dagger/MissingBinding] com.example.feed.feedApp cannot be provided without an @Inject constructor or an @Provides-annotated method.
      public abstract static class SingletonC implements HiltWrapper_ActivityRetainedComponentManager_ActivityRetainedComponentBuilderEntryPoint,
                             ^
          com.example.feed.feedApp is injected at
              com.example.feed.dagger.viewmodel.FeedViewModelModule.providePauseAbleObservable(feedApp)
          com.example.feed.util.PauseAbleObservable<?> is injected at
              com.example.feed.viewmodel.FeedViewModel(�, PauseAbleObservable)
          com.example.feed.viewmodel.FeedViewModel is injected at
              com.example.feed.viewmodel.FeedViewModel_HiltModules.BindsModule.binds(vm)
          @dagger.hilt.android.internal.lifecycle.HiltViewModelMap java.util.Map<java.lang.String,javax.inject.Provider<androidx.lifecycle.ViewModel>> is requested at
              dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap() [com.example.feed.feedApp_HiltComponents.SingletonC ? com.example.feed.feedApp_HiltComponents.ActivityRetainedC ? com.example.feed
    FeedViewModelModule.kt
    @Module
    @InstallIn(ActivityComponent::class)
    class FeedViewModelModule {
    
        @Provides
        fun providePauseAbleObservable(feedApp: FeedApp):  PauseAbleObservable<*> {
            return PauseAbleInterval(
                { System.currentTimeMillis() },
                sportsFeedApp.resources.getInteger(R.integer.pollingPeriodSeconds).toLong(),
                TimeUnit.SECONDS
            )
        }
    }
    PauseAbleObservable.kt
    interface PauseAbleObservable<T> {
         val observable: Observable<T>
    
        fun pause()
    
        fun resume()
    }
    FeedViewModel .kt
    @HiltViewModel
    class FeedViewModel @Inject constructor(
        private val feedRepository: FeedRepository,
        var pauseAbleObservable: PauseAbleObservable<*>
    ) : ViewModel() {....}
    a

    Arun

    1 year ago
    Could you also show your FeedApp? Is it marked with @HiltAndroidApp
    Hovhannes

    Hovhannes

    1 year ago
    @Arun, thanks for your reply. Yes, it's marked @HiltAndroidApp FeedApp.kt
    @HiltAndroidApp
    class FeedApp : Application()
    a

    Arun

    1 year ago
    Could you try changing to
    @Provides
        fun providePauseAbleObservable(feedApp: Application):  PauseAbleObservable<*> {
    I think Hilt binds to Application instead of FeedApp.
    Hovhannes

    Hovhannes

    1 year ago
    @Arun I I've tried it, I get the same errors again. Results must show on a Fragment. Are the errors depend on @InstallIn? But I used ActivityRetainedComponent and FragmentComponent, it did'nt help.
    @Module
    @InstallIn(ActivityComponent::class)
    class FeedViewModelModule {
    
        @Provides
        fun providePauseAbleObservable(feedApp: FeedApp):  PauseAbleObservable<*> {
            return PauseAbleInterval(
                { System.currentTimeMillis() },
                sportsFeedApp.resources.getInteger(R.integer.pollingPeriodSeconds).toLong(),
                TimeUnit.SECONDS
            )
        }
    }
    FunkyMuse

    FunkyMuse

    1 year ago
    Have you tried installing into
    ViewModelComponent
    if that fails then you need to make sure your
    PauseAbleObservable<@JvmSuppressWildcards*>
    if that fails then you need the type set correctly
    if that fails then you need to annotate your activity with @AndroidEntryPoint as well
    Hovhannes

    Hovhannes

    1 year ago
    I think the error is fixed, but now I get this error. @AndroidEntryPoint is marked public final class SportsFeedApp extends android.app.Application { ^ Expected @HiltAndroidApp to have a value. Did you forget to apply the Gradle
    a

    Arun

    1 year ago
    Are you using Kotlin 1.5.20?
    Hovhannes

    Hovhannes

    1 year ago
    @Arun Yes
    a

    Arun

    1 year ago
    It is a known bug https://github.com/google/dagger/issues/2684#issuecomment-860250964 <- please follow this in the mean time
    Hovhannes

    Hovhannes

    1 year ago
    @Arun the previous issue has fixed, but now I get this error 🤦‍♂️ I marked @AndroidEntryPoint in Fragments/Activities Unable to start activity ComponentInfo{com.example.feed/com.example.feed.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.example.feed.viewmodel.FeedViewModel 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.feed.viewmodel.FeedViewModel 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 com.example.feed.MainActivity.onCreate(MainActivity.kt:36) 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.feed.viewmodel.FeedViewModel> has no zero argument constructor MainActivity.kt
    class MainActivity : AppCompatActivity() {
    
        private lateinit var model: FeedViewModel
    
        private lateinit var navController: NavController
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    FeedViewModel.kt
    @HiltViewModel
    class FeedViewModel @Inject constructor(
        private val feedRepository: FeedRepository,
        private val pauseAbleObservable: PauseAbleObservable<*>
    ) : ViewModel() {