简体   繁体   中英

Dagger with Mvp on Kotlin - Android

I am an Android developer that is just getting into Kotlin, and I have been trying to setup an Mvp Dagger application in Kotlin, but I cannot figure out what is going on. I continue to get this error:

Error:dagger.internal.codegen.ComponentProcessor was unable to process this interface 
because not all of its dependencies could be resolved.
Check for compilation errors or a circular dependency with generated code.

Here is my code:

KotlinApplication:

class KotlinApplication: MultiDexApplication(), HasActivityInjector {

    @Inject lateinit var injector: DispatchingAndroidInjector<Activity>

    override fun onCreate() {
        super.onCreate()
        inject()
    }

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

    fun inject() {
        DaggerAppComponent.builder().application(this).build().inject(this);
    }


}

AppComponent:

@Singleton
@Component(modules = arrayOf(AppModule::class, InjectionModule::class))
interface AppComponent {

    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(app: KotlinApplication): Builder

        fun build(): AppComponent
    }

    fun inject(app: KotlinApplication)

}

AppModule:

@Module
class AppModule {
    @Singleton
    @Provides
    fun provideAppContext(app: KotlinApplication): Context {
        return app
    }

    @Singleton
    @Provides
    fun provideApplication(app: KotlinApplication): Application {
        return app
    }
}

InjectionModule:

@Module
abstract class InjectionModule {

    @ActivityScope
    @ContributesAndroidInjector(modules = arrayOf(MainModule::class))
    internal abstract fun bindMainActivity(): MainActivity

}

MainActivity:

class MainActivity : AppCompatActivity(), MainMvp.View {

    @Inject lateinit var presenter: MainMvp.Presenter

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

MainMvp:

interface MainMvp {
    interface View {

    }

    interface Presenter {

    }
}

MainPresenter:

class MainPresenter(view: MainMvp.View): MainMvp.Presenter {

    var view: MainMvp.View

    init {
        this.view = view
    }

}

MainBindModule:

@Module
interface MainBindModule {
    @ActivityScope
    @Binds
    fun bindView(activity: MainActivity): MainMvp.View
}

MainModule:

@Module(includes = arrayOf(MainBindModule::class))
class MainModule {

    @ActivityScope
    @Provides
    fun providePresenter(view: MainMvp.View): MainMvp.Presenter {
        return MainPresenter(view)
    }

}

And finally, ActivityScope:

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class ActivityScope

Does anyone know what might be wrong? I have used the same exact Architectural pattern in java several times before, and I have never ran into this error before.

Also, when I comment out everything inside of InjectionModule, everything works

I think you have to add AndroidSupportInjectionModule::class in your AppComponent :

@Singleton
@Component(modules = arrayOf(AppModule::class,
AndroidSupportInjectionModule::class, InjectionModule::class))
interface AppComponent {

    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(app: KotlinApplication): Builder

        fun build(): AppComponent
    }

    fun inject(app: KotlinApplication)

}

If it still does not help try referring to this github repo : Kotlin MVP Chat . Checkout the di folder which contains all dependency injection code in kotlin.

With Reference to Official Dagger2 issue page

It says:

This is a gradle issue, not a dagger one.

Try clean/re-build your project

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM