Search code examples
javaandroidkotlindependency-injectiondagger

Android - Dagger not generating DaggerModelComponent.create()


I'm following these tutorials here trying to learn dagger. https://www.youtube.com/watch?v=wJkHYBf8VkA

In my activity, I'm trying to call DaggerModelComponent.create() after rebuilding my project and it is unresolved.

Heres my model class:

class Car @Inject constructor(engine: Engine, wheels: Wheels) {

fun drive(){
    Log.d("Car", "driving...")
}}

My component interface:

@Component interface CarComponent {

fun getCar() : Car }

And my activity:

private lateinit var car: Car

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

    val component = DaggerCarComponent.create()
    car.drive()
}

I also have models for engine and wheels :

class Engine @Inject constructor()

and

class Wheels @Inject constructor()

This is what is not working in my activity:

val component = DaggerCarComponent.create()

Apparently, Dagger is supposed to generate this class but I don't think it is.

These are the dependency imports in my app build.grale

implementation 'com.google.dagger:dagger:2.25.2'
annotationProcessor 'com.google.dagger:dagger-compiler:2.25.2'

Am I missing something? Perhaps I'm doing something wrong in Kotlin, I'm following a tutorial in Java. Thanks in advance.


Solution

  • Found the solution! For Kotlin, apparently it's better to use kapt over annotationProcessor. So adding:

    apply plugin: 'kotlin-kapt'
    

    and

    kapt 'com.google.dagger:dagger-compiler:2.25.2'
    

    to app build.gradle fixed the issue.