https://kotlinlang.org logo
#dagger
Title
# dagger
h

Hovhannes

07/02/2021, 7:02 AM
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.
Copy code
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
Copy code
@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
Copy code
interface PauseAbleObservable<T> {
     val observable: Observable<T>

    fun pause()

    fun resume()
}
FeedViewModel .kt
Copy code
@HiltViewModel
class FeedViewModel @Inject constructor(
    private val feedRepository: FeedRepository,
    var pauseAbleObservable: PauseAbleObservable<*>
) : ViewModel() {....}
a

Arun

07/02/2021, 7:05 AM
Could you also show your FeedApp? Is it marked with @HiltAndroidApp
h

Hovhannes

07/02/2021, 7:11 AM
@Arun, thanks for your reply. Yes, it's marked @HiltAndroidApp FeedApp.kt
Copy code
@HiltAndroidApp
class FeedApp : Application()
a

Arun

07/02/2021, 7:30 AM
Could you try changing to
Copy code
@Provides
    fun providePauseAbleObservable(feedApp: Application):  PauseAbleObservable<*> {
I think Hilt binds to Application instead of FeedApp.
h

Hovhannes

07/02/2021, 7:39 AM
@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.
Copy code
@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
        )
    }
}
f

FunkyMuse

07/02/2021, 7:45 AM
Have you tried installing into
Copy code
ViewModelComponent
1
if that fails then you need to make sure your
Copy code
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
h

Hovhannes

07/02/2021, 8:02 AM
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

07/02/2021, 8:03 AM
Are you using Kotlin 1.5.20?
h

Hovhannes

07/02/2021, 8:04 AM
@Arun Yes
a

Arun

07/02/2021, 8:04 AM
It is a known bug https://github.com/google/dagger/issues/2684#issuecomment-860250964 <- please follow this in the mean time
h

Hovhannes

07/02/2021, 11:08 AM
@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
Copy code
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
Copy code
@HiltViewModel
class FeedViewModel @Inject constructor(
    private val feedRepository: FeedRepository,
    private val pauseAbleObservable: PauseAbleObservable<*>
) : ViewModel() {
2 Views