Search code examples
androidkotlindagger-2

Dagger Component is not getting generated using kotlin in android


ApiModule.kt

@Module
class ApiModule {

    private val BASE_URL = "https://raw.githubusercontent.com"

    @Provides
    fun providesCountriesApi() : CountriesApi{

        return Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
            .create(CountriesApi::class.java)

    }

    @Provides
    fun provideCountriesService(): CountriesService {
        return CountriesService()
    }
}

ApiComponent.kt

@Component(modules = [ApiModule::class])
interface ApiComponent {
    fun inject(service: CountriesService)

    fun inject(viewModel: ListViewModel)
}

CountriesService.kt

class CountriesService {


    @Inject
    lateinit var api: CountriesApi

    init {
        DaggerApiComponent.create().inject(this)
    }

    fun getCountries(): Single<List<Country>> {
        return api.getCountries()
    }
}

CountriesApi.kt

interface CountriesApi {
    @GET("DevTides/countries/master/countriesV2.json")
    fun getCountries(): Single<List<Country>>
}

gradle

apply plugin: 'com.android.application'

    apply plugin: 'kotlin-android'

    apply plugin: 'kotlin-android-extensions'

    android {
        compileSdkVersion 29
        buildToolsVersion "29.0.0"
        defaultConfig {
            applicationId "com.demo.kotlinandroidmaster"
            minSdkVersion 15
            targetSdkVersion 29
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
    }

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        implementation 'androidx.core:core-ktx:1.0.2'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        implementation 'androidx.appcompat:appcompat:1.0.2'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
        implementation 'com.squareup.retrofit2:retrofit:2.4.0'
        implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
        implementation "androidx.recyclerview:recyclerview:1.0.0"
        implementation "androidx.cardview:cardview:1.0.0"
        implementation 'com.google.android.material:material:1.0.0-rc01'
        implementation 'com.github.bumptech.glide:glide:3.7.0'
        implementation 'info.androidramp:loading-gear:1.0.4'
        implementation 'androidx.appcompat:appcompat:1.0.0'
        implementation 'com.google.dagger:dagger:2.21'
        implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
        implementation 'io.reactivex.rxjava2:rxjava:2.1.0'
        implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
        implementation "androidx.room:room-runtime:2.0.0-rc01"
        implementation "androidx.room:room-rxjava2:2.0.0-rc01"
        implementation "androidx.lifecycle:lifecycle-extensions:2.0.0-rc01"
        annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.0.0-rc01"
        annotationProcessor "androidx.room:room-compiler:2.0.0-rc01"
        annotationProcessor 'com.google.dagger:dagger-compiler:2.21'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test:runner:1.2.0'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    }

Dagger component is not getting generated

enter image description here


Solution

  • You are missing the annotation processor and the dagger compiler dependency.

    In your build.gradle add after the other plugins:

    apply plugin: 'kotlin-kapt'
    

    and then, as dependency:

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

    Side note: you are not using the latest version of Dagger.