This code works perfectly in Java, but has an undefined problem in Kotlin
Problem :
error: [Dagger/MissingBinding] ru.terrakok.cicerone.Router cannot be provided without an @Inject constructor or an @Provides-annotated method. public abstract void inject(@org.jetbrains.annotations.NotNull()
ru.terrakok.cicerone.Router is injected at com.setname.dagger2ciceronelearning.presentation.main.MainActivity.mRouter
com.setname.dagger2ciceronelearning.presentation.main.MainActivity is injected at com.setname.dagger2ciceronelearning.di.AppComponent.inject(com.setname.dagger2ciceronelearning.presentation.main.MainActivity)
AppComponent
@Singleton
@Component(modules = arrayOf(NavigationModule::class))
interface AppComponent {
fun inject(mainActivity: MainActivity)
fun inject(fragmentOne: FragmentOne)
fun inject(fragmentTwo: FragmentTwo)
}
NavigationModule
@Module
class NavigationModule {
private val firstLevelNavigation: Cicerone<Router> = Cicerone.create()
private val secondLevelNavigation: Cicerone<Router> = Cicerone.create()
@Provides
@Singleton
@Named(Navigation.ROUTER_FIRST_LEVEL)
fun provideFirstLevelRouter(): Router = firstLevelNavigation.router
@Provides
@Singleton
@Named(Navigation.ROUTER_SECOND_LEVEL)
fun provideSecondLevelRouter(): Router = secondLevelNavigation.router
@Provides
@Singleton
@Named(Navigation.NAV_HOLDER_FIRST_LEVEL)
fun provideFirstLevelNavigatorHolder(): NavigatorHolder = firstLevelNavigation.navigatorHolder
@Provides
@Singleton
@Named(Navigation.NAV_HOLDER_SECOND_LEVEL)
fun provideSecondLevelNavigationHolder(): NavigatorHolder = secondLevelNavigation.navigatorHolder
}
LearingApp
class LearningApp : Application() {
companion object {
lateinit var appComponent: AppComponent
}
override fun onCreate() {
super.onCreate()
appComponent = DaggerAppComponent
.builder()
.build()
}
}
MainActivity
class MainActivity : AppCompatActivity() {
@Inject
@Named(Navigation.ROUTER_FIRST_LEVEL)
lateinit var mRouter: Router
@Inject
@Named(Navigation.NAV_HOLDER_FIRST_LEVEL)
lateinit var mNavigatorHolder: NavigatorHolder
private lateinit var mNavigator:Navigator
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
LearningApp.appComponent.inject(this)
mNavigator = SupportAppNavigator(this, R.id.main_container)
mRouter.newRootScreen(Screens.FragmentOneScreen())
}
override fun onResume() {
super.onResume()
mNavigatorHolder.setNavigator(mNavigator)
}
override fun onPause() {
mNavigatorHolder.removeNavigator()
super.onPause()
}
}
就像一个非常聪明的人曾经说过的: You need to target the annotation at the field by using @field:Named.
You can try to use custom qualifiers:
@Qualifier
@Retention
annotation class RouterFirstLevel
then your module:
@Provides
@Singleton
@RouterFirstLevel
fun provideFirstLevelRouter(): Router = firstLevelNavigation.router
and activity:
@Inject
@RouterFirstLevel
lateinit var mRouter: Router
PS I agree with jbarat though, field in annotation should have helped, try to use string instead of string constants, maybe some type mismatch
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.