【问题标题】:Kapt not generating classes in Instant app feature moduleKapt 不在 Instant 应用功能模块中生成类
【发布时间】:2018-02-15 05:28:20
【问题描述】:

我在我的 android 应用程序中使用 dagger2。即使没有错误,它也不会生成 dagger 组件类。

我已在设置中启用注释处理器并重新启动我的 android studio,但这对我不起作用。我也读了这个帖子Dagger2 not generating Daggercomponent classes,并在一个帖子上读到apt已被弃用,所以我正在使用annotationProcessor

基础模块 build.gradle

apply plugin: 'com.android.feature'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    baseFeature true
    defaultConfig {
        minSdkVersion 23
        targetSdkVersion 26
        versionCode 1
        versionName "0.0.1"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    application project(':app')
    feature project(":main")
    feature project(":tv")  

    api 'com.android.support:appcompat-v7:26.0.2'
    api 'com.android.support.constraint:constraint-layout:1.0.2'
    api 'com.android.support:design:26.0.2'

    api "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    api "org.jetbrains.anko:anko-commons:$anko_version"

    api "android.arch.lifecycle:runtime:1.0.0-alpha9"
    api "android.arch.lifecycle:extensions:1.0.0-alpha9"
    kapt "android.arch.lifecycle:compiler:1.0.0-alpha9"

    api 'com.squareup.retrofit2:retrofit:2.3.0'
    api "com.squareup.retrofit2:converter-moshi:2.0.0"

    api 'com.google.dagger:dagger:2.11'
    kapt 'com.google.dagger:dagger-compiler:2.11'

    api 'com.github.bumptech.glide:glide:4.0.0'
    kapt 'com.github.bumptech.glide:compiler:4.0.0'

    // new version 1.5.2 has some multi dex issue
    debugApi 'com.squareup.leakcanary:leakcanary-android:1.5.1'
    releaseApi 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
}

repositories {
    mavenCentral()
}

apply plugin: 'com.google.gms.google-services'

电视功能 build.gradle

apply plugin: 'com.android.feature'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"


    defaultConfig {
        minSdkVersion 23
        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'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation project(':base')
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.0'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.0'
}

项目构建.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.1.3-2'
    ext.anko_version = '0.10.1'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-beta4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:3.1.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

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

NetComponent.kt

@Component(modules = arrayOf(AppModule::class, NetModule::class))
interface NetComponent {
    fun inject(activity: MainActivity)
}

apt 在 apt 目录中生成 dagger 类,但即使我在整个项目目录中搜索,现在也没有这样的 dagger 生成类。

我看到它没有生成DaggerNetComponent 类,因为编译时也没有错误。有谁知道可能是什么问题?

【问题讨论】:

  • 这个模块是即时应用模块吗?请发布整个 build.gradle。
  • @EugenPechanec 请看。我已经添加了
  • 我在这里创建了一个错误报告youtrack.jetbrains.com/issue/KT-20244 让我们看看会发生什么。
  • 好的,有进展了。在您应用我的原始答案后,会生成类,但它们不会显示在“项目”窗口中。在您右键单击并选择同步后,它们应该会出现(在您构建项目之后)。你能确认一下吗?
  • 我同步了但没有成功

标签: android dagger-2 android-instant-apps kapt


【解决方案1】:

您的模块是一个免安装应用功能模块。而且看起来 kapt 还不支持这些。

我无法通过来源支持这一点,但这应该可以:

  • 功能模块
    • 库模块 + 匕首 + kapt

将所有内容从您的功能模块移至库模块。然后使特性模块依赖于库模块。

库模块确实支持 kapt。

This sample project 在 Instant App 功能模块中使用 Dagger 和 Kapt,它开箱即用。您的项目设置肯定存在其他问题,与 Dagger 或注释处理无关。

查看Instant App Codelab,确保您没有误解任何内容。

原来的答案应该可以工作。

原答案

使用kapt 而不是annotationProcessor 配置。如:

kapt "android.arch.lifecycle:compiler:1.0.0-alpha9"
kapt 'com.google.dagger:dagger-compiler:2.11'
kapt 'com.github.bumptech.glide:compiler:4.0.0'

如果您正在使用数据绑定,请添加以下内容:

kapt "com.android.databinding:compiler:3.0.0-beta4"

当您使用不同的构建插件版本时,不要忘记更新版本。

Kotlin 插件不使用 annotationProcessor 依赖项,我们必须使用 kapt 依赖项。

最后,要使用最新版本的 Kotlin 注释处理器,请将其放在模块 build.gradle 的顶部:

apply plugin: 'kotlin-kapt'

Kotlin 对 com.android.feature 模块的支持是 added in Kotlin 1.1.4,请确保您至少使用它。

【讨论】:

  • 嗯,好的。它在哪里生成类?任何路径?
  • 注释处理器的输出是该注释处理器的责任。 kapt 输出的根是/build/generated/source/kapt/。所以对于 Dagger,它是 /build/generated/source/kapt/<flavor>/<whatever-package-your-original-class-was-in>
  • 不。它没有生成 kapt 文件夹
  • 嘿@EugenPechanec,我们还需要“generateStubs”吗?
  • @G.BlakeMeike 好点,只要你apply plugin: 'kotlin-kapt',你就不需要kapt.generateStubs = true
【解决方案2】:

在你的base模块build.gradle你有

dependencies {
    // some other stuff        

    api 'com.google.dagger:dagger:2.11'
    kapt 'com.google.dagger:dagger-compiler:2.11'
}

虽然由于api 关键字,com.google.dagger:dagger:2.11 对您的功能 模块可传递,但kapt 使com.google.dagger:dagger-compiler:2.11 可传递到您的功能模块。

因此,您需要将 com.google.dagger:dagger-compiler:2.11 添加到您的所有 功能 模块中,以便 dagger 生成类

功能模块build.gradle

dependencies {
    // some other stuff        

    // add kapt 
    kapt 'com.google.dagger:dagger-compiler:2.11'
}

【讨论】:

  • 没用
  • @KenZira 你想提供一些额外的信息吗?
【解决方案3】:

我确实遇到了这个问题。

尝试从 Kotlin 标准库依赖项中删除“jre7”。

【讨论】:

  • 不。我重新构建了我的项目,并且 kapt 内部的 debug/debugFeature 为空
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-26
  • 2019-05-07
  • 1970-01-01
  • 2016-07-29
  • 2018-08-17
  • 1970-01-01
相关资源
最近更新 更多