1

I'm learning how to use Dagger 2 and MVP architeture.

But I'm stuck in this error now:

Unresolved reference: DaggerHelloComponent

Look, this is my module:

@Module
class HelloModule {
    lateinit var activityDagger: HelloActivityDagger

    constructor(activityDagger: HelloActivityDagger) {
        this.activityDagger = activityDagger
    }

    @Provides
    fun providesHelloPresenter(): HelloActivityPresenterDagger = HelloActivityPresenterDagger(activityDagger)
}

and my component:

@Component(modules = [HelloModule::class])
interface HelloComponent {
    fun inject(activityDagger: HelloActivityDagger)
}

So when I try to builder this component like this DaggerHelloComponent.create().inject(this) in my HelloActivityDagger shows me the error above.

Anyone know what I am doing wrong?

Cause I can see whats wrong in this code.

Oh, I already have followed this kapt things from this question Unresolved reference DaggerApplicationComponent and nothing happens

EDIT

To be more readable I`ve uploaded my project to Git. https://github.com/luangs7/DaggerMvpExample

LMaker
  • 1,444
  • 3
  • 25
  • 38
  • Did you build your project after configuring `kapt`? Your first build will of course fail, but it should generate the Dagger class for you to import and use from there on. – zsmb13 Aug 07 '18 at 17:10
  • yes, I`ve rebuild the project several times after – LMaker Aug 07 '18 at 17:13
  • have you add `apply plugin: 'kotlin-kapt'`, because kapt is deprecated in default gradle. So you need to add mentioned plugin in order to kapt needs to work. – Moinkhan Aug 08 '18 at 04:17
  • Just to add a small note on this issue, remember to check everywhere you have `@Inject` in your app. Note that you cannot inject into private fields. If you have `@Inject private lateinit var ...` you'll have this error even if your components and modules have been properly setup. – O'Kamiye Nov 12 '18 at 13:33

1 Answers1

0

Putting this code in the MyApplication class works for me to create the module starting off from your GitHub project - after the first failed build, kapt generates the DaggerHelloComponent class, and I can import it.

import android.app.Application
import br.com.squarebits.mvpexample.DaggerMvp.componentes.DaggerHelloComponent
import br.com.squarebits.mvpexample.DaggerMvp.componentes.HelloComponent
import br.com.squarebits.mvpexample.DaggerMvp.modules.HelloModule

class MyApplication : Application() {

    val component: HelloComponent by lazy {
        DaggerHelloComponent.builder()
                .helloModule(HelloModule(HelloActivityDagger()))
                .build()
    }

}

If this is not happening at all, you should try the usual debugging steps of restarting Studio with File -> Invalidate Caches / Restart, cleaning and rebuilding your project, etc. Perhaps try pulling the GitHub repo to a new location and see if that builds for you - the code isn't the problem.

zsmb13
  • 85,752
  • 11
  • 221
  • 226