Kapt无法在Instant App功能模块中生成类 [英] Kapt not generating classes in Instant app feature module

查看:172
本文介绍了Kapt无法在Instant App功能模块中生成类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Android应用程序中使用dagger2.即使没有错误,它也不会生成匕首组件类.

I am using dagger2 in my android application. It is not generating dagger component classes even though there is no errors.

我已在设置中启用注释处理器并重新启动android studio,但这对我不起作用.我也读了这个线程 Dagger2不生成Daggercomponent类,并在一个线程上读到了已弃用,因此我正在使用annotationProcessor

I have enabled the annotation processors in the setttings and restart my android studio but that didn't work for me. I read this thread too Dagger2 not generating Daggercomponent classes and read on one thread that apt is deprecated so I am using annotationProcessor

基本模块build.gradle

Base Module 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

tv feature 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'
}

项目build.gradle

Project build.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

NetComponent.kt

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

apt在apt目录中生成dagger类,但是即使我在整个项目目录中进行搜索,现在也没有此类dagger生成的类.

apt generates dagger classes inside apt directory but there is no such dagger generated classes now even though I searched in entire project directory.

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

I see that its not generating DaggerNetComponent class, as there is no errors too on compilation. Does anyone know what could be the issue ?

推荐答案

您的模块是即时应用功能模块.看来kapt尚不支持这些功能.

Your module is an instant app feature module. And it looks like kapt doesn't support those yet.

我无法提供源支持,但这应该可行:

I cannot back this with a source but this should work:

  • 功能模块
    • 库模块+匕首+ kapt
    • feature module
      • library module + dagger + kapt

      将所有功能从功能模块移动到库模块.然后使功能模块依赖于库模块.

      Move everything from your feature module to a library module. Then make the feature module depend on the library module.

      库模块确实支持kapt.

      Library modules do support kapt.

      此示例项目在Instant App Feature模块中使用了Dagger和Kapt,并且可以开箱即用.您的项目设置中肯定还有其他问题,与Dagger或批注处理无关.

      This sample project uses Dagger and Kapt in Instant App Feature modules and it works out-of-the-box. There must be some other issue with your project setup, which is not related to Dagger or annotation processing.

      浏览即时应用Codelab 并进行确保您没有误会任何东西.

      Go through the Instant App Codelab and make sure you didn't misunderstand anything.

      原始答案应会起作用.

      使用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'
      

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

      If you're using databinding add this as well:

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

      当您使用其他构建插件版本时,请不要忘记更新版本.

      Don't forget to update the version when you use different build plugin version.

      Kotlin插件不会获取annotationProcessor依赖关系,我们必须改用kapt依赖关系.

      Kotlin plugin doesn't pick up annotationProcessor dependencies, we have to use kapt dependencies instead.

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

      Finally, to use latest version of Kotlin annotation processor put this at the top of your module's build.gradle:

      apply plugin: 'kotlin-kapt'
      

      com.android.feature模块的Kotlin支持是在Kotlin 1.1.4中添加的,确保至少使用它.

      Kotlin support for com.android.feature modules was added in Kotlin 1.1.4, make sure you use at least that.

      这篇关于Kapt无法在Instant App功能模块中生成类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆