在Android中使用Kotlin无法生成Dagger组件

时间:2019-07-20 06:34:39

标签: android kotlin dagger-2

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>>
}

渐变

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组件

enter image description here

2 个答案:

答案 0 :(得分:1)

在运行应用程序之前尝试做项目的build

完成Build时会生成匕首组件,而Clean完成时会删除匕首组件。

修改

@Singleton
@Component(
    modules = [AppModule::class, AndroidInjectionModule::class,
        InjectionBinder::class,
        AndroidSupportInjectionModule::class, InjectionBinder::class]
)
interface AppComponent {

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

        fun build(): AppComponent
    }

    fun inject(app: YourApplication)
}

这是一个AppComponent的工作示例

答案 1 :(得分:0)

您缺少注释处理器和Dagger编译器依赖项。

在您的build.gradle中添加其他插件:

apply plugin: 'kotlin-kapt'

然后作为依赖项:

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

旁注:您没有使用最新版本的Dagger。