简体   繁体   English

kotlin 没有生成 Dagger 组件

[英]Dagger Component is not generated in kotlin

I am using Dagger2 for DI but the problem is its not generating component file for the interface.我将 Dagger2 用于 DI,但问题是它没有为接口生成组件文件。 Below are my codes for module,gradle file and component.下面是我的模块代码,gradle 文件和组件。 I have done build / rebuild / clean / restart project.我已经完成构建/重建/清理/重启项目。 What can be the cause for not generating Dagger Component class?没有生成 Dagger 组件 class 的原因是什么?

Module Class模块 Class

    package com.global.Utills.DI

import com.global.Utills.SharedPref
import dagger.Module
import dagger.Provides
import javax.inject.Singleton


@Singleton
@Module
class NetworkModule {

    @Singleton
    @Provides
    fun provideNetworkChecker() = SharedPref()

}

**SharedPref Class is Just a random class ** **SharedPref Class 只是一个随机的 class **

Component Class组件 Class

    package com.global.Utills.DI

import android.content.Context
import com.global.Backend.NetworkApi
import com.global.Utills.DI.Module.RetrofitModule
import com.global.Utills.NetworkChecker
import com.global.Utills.SharedPref
import dagger.Component
import retrofit2.Retrofit
import javax.inject.Singleton


@Singleton
//@Component(modules = [ContextModule::class,RetrofitModule::class,NetworkModule::class])
@Component(modules = [NetworkModule::class])
interface Component {

//     fun getNetworkApi():NetworkApi
//
//    // fun getNetworkChecker():NetworkChecker
//
//     fun getContext():Context
     fun getShred():SharedPref
}

Build.Gradle建筑.Gradle

    apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.1"

    defaultConfig {
        vectorDrawables.useSupportLibrary = true
        applicationId "com.global.cvcv"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        multiDexEnabled true

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
    kapt {
        generateStubs = true
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.1'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    //material design
    implementation 'com.google.android.material:material:1.2.0'

    // dimen.xml
    implementation 'com.intuit.sdp:sdp-android:1.0.6'

    //rxjava
    implementation 'io.reactivex.rxjava2:rxjava:2.2.5'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'

    // Lifecyles, LiveData and ViewModel
    implementation 'androidx.lifecycle:lifecycle-runtime:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    kapt 'androidx.lifecycle:lifecycle-compiler:2.2.0'

    // Room
    implementation "android.arch.persistence.room:runtime:1.1.1"
    implementation "android.arch.persistence.room:rxjava2:1.1.1"
    kapt "android.arch.persistence.room:compiler:1.1.1"

    //DAGGER
    implementation "com.google.dagger:dagger:2.24"
    implementation "com.google.dagger:dagger-android:2.22.1"
    implementation "com.google.dagger:dagger-android-support:2.22.1"
    kapt "com.google.dagger:dagger-compiler:2.22.1"
    kapt "com.google.dagger:dagger-android-processor:2.22.1"



    //Firebase messaging dependency  firestore

    implementation 'com.google.firebase:firebase-messaging:20.2.4'
    implementation 'com.google.firebase:firebase-firestore:21.5.0'

    //Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.6.1'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
    implementation 'com.squareup.retrofit2:converter-gson:2.6.1'
    implementation "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0"

    //Glide Library
    implementation 'com.github.bumptech.glide:glide:4.10.0'



}

@Modules cannot be scoped. @Modules不能限定范围。 Only methods can be scoped.只有方法可以限定范围。 Remove @Singleton from NetworkModule .NetworkModule中删除@Singleton

@Module
class NetworkModule {
    @Singleton
    @Provides
    fun provideNetworkChecker() = SharedPref()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM