Dagger2 and Kotlin launch bad reasons: app: compileDebugKotlinAfterJava

I am trying to implement Dagger 2 in a test application in order to learn clean architecture and dependency injection in Kotlin language.

EDIT:
I can compile thanks to @Logain, but I always have a static member problem with Dagger in my singleton (see below for my TaskWorker), so I'm looking for how I can fix this error.

But I have a problem, my DaggerComponent is well generated when I rebuild, but not when I want to run my application for testing, it fails and disappears. Error with this error:

Error: (21, 29) Unresolved reference: DaggerInjectorComponent

Error: Execution completed for task ': app: compileDebugKotlinAfterJava'.

> Compilation error. See log for details

At the time I am doing a rebuild, this task is passed correctly

:app:compileDebugKotlinAfterJava

      

So, I don't understand why it is failing.

Here is my InjectorComponent :

@Singleton
@Component(modules = arrayOf(ContextDaggerModule::class, LocalStoreDaggerModule::class))
interface InjectorComponent {

    fun inject(realmLocalStore: RealmLocalStore)

    fun inject(taskWorker: TaskWorker)

}

      

ContectDaggerModule :

@Module
class ContextDaggerModule (val app: Application) {

    @Provides
    @Singleton
    fun provideContext(): Context = app

    @Provides
    @Singleton
    fun provideApplication(): Application = app

    @Provides
    @Singleton
    fun provideResources(): Resources = app.resources

}

      

LocalStoreDaggerModule :

@Module
class LocalStoreDaggerModule {

    @Provides
    @Singleton
    fun provideLocalStore(context: Context): LocalStore {
        return RealmLocalStore(context)
    }

}

      

I think the problem is caused by the fact that I am injecting dependencies into Object-declarations , but all elements are static and dagger is not evaluating that. So I'm trying to hack it with a simple getter override and input, but nop.

Here's my "hack":

object TaskWorker {

    // @Inject lateinit var localStore: LocalStore
    // Not work cause it a static variable

    var localStore: LocalStore? = null
        @Inject
        get() = localStore

    // some cool function
}

      

I am following this code and this tutorial

I am using these dependencies:

//  Dagger2
    compile 'com.google.dagger:dagger:2.11'
    kapt 'com.google.dagger:dagger-compiler:2.11'
    provided 'org.glassfish:javax.annotation:10.0-b28'

      

+3


source to share


2 answers


Make sure you are using:

kapt {
    generateStubs = true
}

      

Due to restrictions on kapt



Or just try:

annotationProcessor 'com.google.dagger:dagger-compiler:2.11'

      

+1


source


You don't need this.

kapt {
    generateStubs = true
}

      

Just apply the plugin:

apply plugin: 'kotlin-kapt'

      



and add dependencies:

compile androidDependencies.dagger2
compile androidDependencies.dagger2Android
kapt androidDependencies.dagger2Kapt

      

sometimes tasks fail with errors like this. Try clean

it and use it as a last resort invalidate and restart

. Most of the time it works.

0


source







All Articles