android - Dagger2 不生成 Dagger 类

标签 android kotlin dagger-2 kotlin-android-extensions

Dagger2 不创建类 Dagger*。我创建了接口(interface) MyContainerComponent,但没有创建类 DaggerMyContainerComponent!

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        classpath "com.android.databinding:dataBinder:1.0-rc4"
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath 'com.frogermcs.androiddevmetrics:androiddevmetrics-plugin:0.4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.6"
    }
}

allprojects {
    repositories {
        jcenter()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle

apply plugin: 'com.android.application'
apply plugin: 'android-apt'
apply plugin: 'com.android.databinding'
apply plugin: 'kotlin-android'
apply plugin: 'com.frogermcs.androiddevmetrics'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.tegra.module.profile"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:25.1.1'
    testCompile 'junit:junit:4.12'
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

    kapt 'com.android.databinding:compiler:1.0-rc4'

    //dagger
    compile 'com.google.dagger:dagger:2.8'
    apt 'com.google.dagger:dagger-compiler:2.8'
    provided 'javax.annotation:jsr250-api:1.0'

    //glide
    compile 'com.github.bumptech.glide:glide:3.7.0'

    //rxAndroid
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'io.reactivex:rxjava:1.1.6'

    //autoFactory
    compile group: 'com.google.auto.factory', name: 'auto-factory', version: '1.0-beta3'

    //retrofit
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile group: 'com.squareup.retrofit2', name: 'adapter-rxjava', version: '2.1.0'

    //OkHttp
    compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'

    //timber
    compile 'com.jakewharton.timber:timber:4.5.1'

    //stetho
    compile 'com.facebook.stetho:stetho:1.4.2'
    compile 'com.facebook.stetho:stetho-okhttp3:1.4.2'

    //constraint-layout
    compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
}

kapt {
    generateStubs = true
}

MyContainerComponent.kt

package com.tegra.module.profile.ioc

@Component(modules = arrayOf(ProfileModule::class))
@Singleton
public interface MyContainerComponent {
    fun inject(profileActivity: ProfileActivity)
}

ProfileModule.kt

package com.tegra.module.profile.bl.di

@Module
class ProfileModule(val application: Application) {

    @Provides
    @Singleton
    fun provideApplication(): Application {
        return application
    }

    @Provides
    @Singleton
    fun provideContext(): Context {
        return application
    }
}

有什么解决办法吗?已经尝试过:更改 gradle 版本、清理、重建、更改依赖版本,还删除 Gradle 缓存并重新安装 Android Studio。

最佳答案

为了在 Kotlin 文件中使用 Dagger 注释,您必须为 Dagger 编译器使用 kapt:

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

此外,删除所有 apt 依赖项,因为 kapt 也处理 Java 中的注释。

Official docs

关于android - Dagger2 不生成 Dagger 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42115498/

相关文章:

android - 列表弹出窗口 : getWindowVisibleDisplayFrame(Rect) null exception

java - Android MaterialCardView 切角样式

postgresql - 如何让 PostgreSQL DBLink 连接与 JDBC 一起工作?

unit-testing - 无法从仅 jvm 模块访问 commonMain 多平台类

android - 使用 .p12 签署 apk

android - 可以更改没有主题的对话框的窗口背景颜色吗?

android - Dagger 2 注入(inject)无效

java - Dagger 2 依赖注入(inject)不起作用

robolectric - Dagger2在Test中不生成组件

java - 改造 call.enqueue 代码 200,但 response.body() 为空