Dependency injection Kotlin和Dagger 2:使用提供程序类的问题

Dependency injection Kotlin和Dagger 2:使用提供程序类的问题,dependency-injection,kotlin,dagger-2,kotlin-android-extensions,dagger,Dependency Injection,Kotlin,Dagger 2,Kotlin Android Extensions,Dagger,在浏览了互联网之后,似乎没有类似的问题,它正在折磨着我。在使用Dagger 2学习依赖注入的过程中,我试图将一个示例从Java翻译成Kotlin。该项目在Java中编译得很好,但使用Kotlin,不喜欢javax.inject.Provider类,无法构建 少了什么?此处对Kotlin使用的Provider类是否不正确 以下是Gradle事件日志中的错误: repositorytrends\custom_implementations\RepoTrendsAppComponent.java:8:

在浏览了互联网之后,似乎没有类似的问题,它正在折磨着我。在使用Dagger 2学习依赖注入的过程中,我试图将一个示例从Java翻译成Kotlin。该项目在Java中编译得很好,但使用Kotlin,不喜欢javax.inject.Provider类,无法构建

少了什么?此处对Kotlin使用的Provider类是否不正确

以下是Gradle事件日志中的错误:

repositorytrends\custom_implementations\RepoTrendsAppComponent.java:8: error: java.util.Map<java.lang.Class<? extends android.app.Activity>,? extends javax.inject.Provider<dagger.android.AndroidInjector.Factory<? extends android.app.Activity>>> cannot be provided without an @Provides-annotated method.
自定义应用程序文件:

class RepoTrendsApp: Application(){

    @Inject lateinit var activityInjector: ActivityInjector

}
Build.gradle用于良好度量:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId 'com.inviscidlabs.repositorytrends'
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }

    kapt {
        generateStubs = true
    }
}

dependencies {
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation "com.android.support:appcompat-v7:$supportLibraryVersion"
    implementation "com.android.support:design:$supportLibraryVersion"

    implementation "com.google.dagger:dagger:$daggerVersion"
    implementation "com.google.dagger:dagger-android-support:$daggerVersion"
    kapt "com.google.dagger:dagger-android-processor:$daggerVersion"
    kapt "com.google.dagger:dagger-compiler:$daggerVersion"

    implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
    implementation "com.squareup.retrofit2:converter-moshi:$retrofitVersion"
    implementation "com.squareup.moshi:moshi:$moshiVersion"
    kapt "com.ryanharter.auto.value:auto-value-moshi:$autoValueMoshiVersion"
    compileOnly "com.ryanharter.auto.value:auto-value-moshi-annotations:$autoValueMoshiVersion"

    compileOnly "com.google.auto.value:auto-value:$autoValueVersion"
    annotationProcessor "com.google.auto.value:auto-value:$autoValueVersion"

    implementation "io.reactivex.rxjava2:rxjava:$rxJavaVersion"
    implementation "io.reactivex.rxjava2:rxandroid:$rxAndroidVersion"
    implementation "com.jakewharton.rxrelay2:rxrelay:$rxRelayVersion"

    //Drop in replacement for Fragments
    implementation "com.bluelinelabs:conductor:$conductorVersion"

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

}
根据要求,该模块:


听起来您的模块上缺少了
@提供的一些注释。以下是Kotlin/Android/Dagger 2模块定义示例:

@Module
class MyAndroidModule(private val application: Application) {
    @Provides
    @Singleton
    @CustomInjectionAnnotation
    fun provideApplicationContext(): Context = application

    @Provides
    @Singleton
    fun provideLocationManager(): LocationManager =
            application.getSystemService(Context.LOCATION_SERVICE) as LocationManager

    @Provides
    @Singleton
    @Named("version")
    fun provideVersionString(): String = "beta"    
}

您不需要
活动Injector
类。按如下方式修改应用程序类:

class RepoTrendsApp: Application(), HasActivityInjector {

    @Inject
    internal lateinit var dispatchingActivityInjector: DispatchingAndroidInjector<Activity>

    override fun onCreate() {
    super.onCreate()
        DaggerRepoTrendsAppComponent.builder()
            .repoTrendsAppModule(RepoTrendsAppModule(this))
            .build()
            .inject(this)
    }

    override fun activityInjector(): AndroidInjector<Activity>? {
        return dispatchingActivityInjector
    }
}
类RepoTrendsApp:Application(),HasActivityInjector{ @注入 内部lateinit var dispatchingActivityInjector:DispatchingAndroidInjector 重写fun onCreate(){ super.onCreate() DaggerRepoTrendsAppComponent.builder() .repoTrendsAppModule(repoTrendsAppModule(this)) .build() .注入(这个) } 覆盖有趣的活动Injector():AndroidInjector{ 返回dispatchingActivityInjector } }
以及您的组件:

@Singleton
@Component(modules = arrayOf(
        AndroidSupportInjectionModule::class,
        RepoTrendsAppModule::class
))
interface RepoTrendsAppComponent : AndroidInjector<RepoTrendsApp>
@Singleton
@组件(模块=阵列)(
AndroidSupportInjectionModule::类,
RepoModule::class
))
接口组件:AndroidJector

您的
reportrendsappmodule
是什么样子的?您似乎忘记了将
AndroidInjectionModule
添加到您的组件中,但请包括所有引用的代码(如上文所述),谢谢您的回复。在完成了初学者代码的其余部分并使用您的建议之后,项目成功构建。虽然它不能回答为什么Dagger对我的地图不满意,但这仍然有效。我假设这个AndroidSupportInjector处理了典型的样板代码。希望我能继续理解Android中的依赖注入。非常感谢您的帮助Dagger不高兴,因为您没有任何
@提供的
方法来提供您的
映射
依赖关系。实际上,
AndroidSupportInjector
为您处理样板文件。
@Module
class MyAndroidModule(private val application: Application) {
    @Provides
    @Singleton
    @CustomInjectionAnnotation
    fun provideApplicationContext(): Context = application

    @Provides
    @Singleton
    fun provideLocationManager(): LocationManager =
            application.getSystemService(Context.LOCATION_SERVICE) as LocationManager

    @Provides
    @Singleton
    @Named("version")
    fun provideVersionString(): String = "beta"    
}
class RepoTrendsApp: Application(), HasActivityInjector {

    @Inject
    internal lateinit var dispatchingActivityInjector: DispatchingAndroidInjector<Activity>

    override fun onCreate() {
    super.onCreate()
        DaggerRepoTrendsAppComponent.builder()
            .repoTrendsAppModule(RepoTrendsAppModule(this))
            .build()
            .inject(this)
    }

    override fun activityInjector(): AndroidInjector<Activity>? {
        return dispatchingActivityInjector
    }
}
@Singleton
@Component(modules = arrayOf(
        AndroidSupportInjectionModule::class,
        RepoTrendsAppModule::class
))
interface RepoTrendsAppComponent : AndroidInjector<RepoTrendsApp>