google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.42k stars 2.01k forks source link

java.lang.RuntimeException: android.app.Application does not implement dagger.android.HasActivityInjector #1709

Closed ckdrb7017 closed 4 years ago

ckdrb7017 commented 4 years ago

I am studying Dagger2 now. I am studying by looking at several examples. But there are some parts that aren't working properly, looking at the examples. I know that the structure of Dagger2 should be defined and implemented in the order of application -> active -> user component. However, an error occurred when following this. the message is java.lang.RuntimeException: android.app.Application does not implement dagger.android.HasActivityInjector The code is long, but I wish someone would show up to help me. I am very desperate.

AppComponent @Singleton @Component(modules = [AndroidSupportInjectionModule::class, ActivityBindingModule::class, ApplicationModule::class]) interface AppComponent : AndroidInjector {

@Component.Builder
interface Builder {
    @BindsInstance
    fun application(application: Application): Builder
    fun build(): AppComponent
}

}

BaseApplication class BaseApplication : DaggerApplication() { override fun applicationInjector(): AndroidInjector { return DaggerAppComponent.builder().application(this).build() } }

ActivityScope @Scope annotation class ActivityScope

ActivityBindingModule @Module abstract class ActivityBindingModule {

@ContributesAndroidInjector(modules = [MainModule::class])
abstract fun mainActivity(): MainActivity

}

MainModule @Module class MainModule { @ActivityScope @Provides fun util() : Utils = Utils()

}

ApplicationModule @Module class ApplicationModule { @Provides fun providesContext(application: Application): Context { return application } }

Utils class Utils { @Inject constructor(){ } fun add(a :Int, b:Int):Int= a+b fun sub(a :Int, b:Int):Int= a-b fun div(a :Int, b:Int):Int= a/b fun mul(a :Int, b:Int):Int= a*b

}

MainActivity class MainActivity : DaggerAppCompatActivity(), HasActivityInjector {

lateinit var utils : Utils

@Inject
lateinit var activityDispatchingAndroidInjector: DispatchingAndroidInjector<Activity>

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    var aa = utils.add(10,20)

    Log.d("TAG",""+aa)

}
override fun activityInjector(): AndroidInjector<Activity> {
    return activityDispatchingAndroidInjector
}

}

BaloghTamas commented 4 years ago

I think you are using an old version of Dagger Android, as HasActivityInjector had been removed in 2.24, try with the latest one as the docs, that you may follow are updated as well

jhbiggs commented 4 years ago

Yes, HasActivityInjector is part of dagger-android, which is a different dependency injection method than Dagger2 and is deprecated. I have jumped back to Dagger 2, which does not use the : AndroidInjector class. Which packages are you importing? You have to watch out for "dagger-android" and remove the imports wherever they pop up.

JakeWharton commented 4 years ago

Two things:

Chang-Eric commented 4 years ago

Closing this since it sounds like this issue is fixed by changing to the new HasAndroidInjector. Please let me know if this isn't the case though, thanks.