6

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

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

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'

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

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

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

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

enter image description here

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

Ajay S
  • 48,003
  • 27
  • 91
  • 111
N Sharma
  • 33,489
  • 95
  • 256
  • 444
  • Is this module an instant app module? Post the whole build.gradle, please. – Eugen Pechanec Sep 06 '17 at 17:45
  • @EugenPechanec Please see. I have added it – N Sharma Sep 06 '17 at 17:49
  • I've created a bug report here https://youtrack.jetbrains.com/issue/KT-20244 Let's see what happens. – Eugen Pechanec Sep 14 '17 at 06:13
  • Ok, there's been progress. Turns out after you apply my Original answer the classes are generated, but they don't show in the Project window. After you right-click and select Synchronize, they should appear (after you build the project). Can you confirm this? – Eugen Pechanec Sep 19 '17 at 22:07
  • I Synchronized it but no luck – N Sharma Sep 20 '17 at 04:39
  • Have you tried with Kotlin 1.1.4-3? I just noticed in your question you have an older version. – Eugen Pechanec Sep 20 '17 at 07:22
  • I am using `ext.kotlin_version = '1.1.4-3'`, I updated after posting this question – N Sharma Sep 20 '17 at 07:24
  • Ok, go through the project referenced in the Kotlin issue and look for anything different/suspicious. For example in your dependencies you have `application project(...)` and `feature project(...)`. Base feature does NOT depend on any other features and certainly not application modules, that's the point of a base module. Dependencies are specified using `implementation` or `api` configuration. `application` and `feature` have no meaning here as far as I know. Visit this walkthrough https://codelabs.developers.google.com/codelabs/android-instant-apps/#0, then get back to your app. – Eugen Pechanec Sep 20 '17 at 07:39
  • Where is your NetComponent in your project? Inside base module or inside tv module? – Mustafa Berkay Mutlu Sep 20 '17 at 10:44
  • @EugenPechanec I think I found something :) wait lemme try – N Sharma Sep 20 '17 at 15:02
  • @EugenPechanec I have to add this `api 'com.google.dagger:dagger:2.11' kapt 'com.google.dagger:dagger-compiler:2.11'` in each feature module where I am using dagger2 even though I have same in base module, basically there should not be need to do but anyways thank you so much :) – N Sharma Sep 20 '17 at 15:10

3 Answers3

8

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:

  • 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.

Library modules do support kapt.

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.

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

The original answer should then work.

Original answer

Use kapt instead of annotationProcessor configuration. As in:

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 plugin doesn't pick up annotationProcessor dependencies, we have to use kapt dependencies instead.

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

apply plugin: 'kotlin-kapt'

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

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
  • Ah ok. Where it generates classes ? any path ? – N Sharma Sep 06 '17 at 16:48
  • Output of an annotation processor is responsibility of that annotation processor. Root for kapt output is `/build/generated/source/kapt/`. So for Dagger it's `/build/generated/source/kapt//`. – Eugen Pechanec Sep 06 '17 at 16:51
  • Nope. It is not gerating kapt folder – N Sharma Sep 06 '17 at 16:58
  • Hey @EugenPechanec, do we still need "generateStubs"? – G. Blake Meike Sep 06 '17 at 17:09
  • 1
    @G.BlakeMeike Good point, as long as you `apply plugin: 'kotlin-kapt'` you don't need `kapt.generateStubs = true`. – Eugen Pechanec Sep 06 '17 at 17:13
  • @EugenPechanec It is still not generating. I did clean and build project multiple times :/ – N Sharma Sep 06 '17 at 17:20
  • @Williams: Perhaps you could update the listing for your gradle dependencies clause? That is where the problem is, most likely (presuming you are applying the "kotlin-kapt" plugin) – G. Blake Meike Sep 06 '17 at 17:41
  • @G.BlakeMeike It's my updated build.gradle https://pastebin.com/ANyjE72j all are updated – N Sharma Sep 06 '17 at 17:46
  • @Williams Please read the updated answer. It's a workaround but it's the best that's available now. – Eugen Pechanec Sep 06 '17 at 17:53
  • @EugenPechanec I am working on instant app so I need feature module. That's why I can't make it library module – N Sharma Sep 06 '17 at 17:54
  • I added `apply plugin: 'kotlin-kapt'` in feature module too, that created kapt directory in `generated/source` but it is empty directory :/. I did clean and build – N Sharma Sep 06 '17 at 17:56
  • @Williams Feature modules can depend on library modules and library modules support kotlin annotation processor. I don't see a problem. – Eugen Pechanec Sep 06 '17 at 18:08
  • what do you mean by library module here ? Is it base module ? – N Sharma Sep 06 '17 at 18:09
  • What I have add in library ? dagger components and module ? Is feature module activity is accessible inside library module ? – N Sharma Sep 06 '17 at 18:52
  • For simplicity move all code from feature to library (code, XML, manifest). Then make the feature module dependent on library and expose the code to its own consumers 'api project(":library-module-name")'. Now any module depending on the feature module can see the code in the library module. – Eugen Pechanec Sep 06 '17 at 23:04
  • My base build.gradle is correct, github example says same to do `feature project(':query') application project(':app')`. – N Sharma Sep 23 '17 at 15:19
  • I have to add this api 'com.google.dagger:dagger:2.11' kapt 'com.google.dagger:dagger-compiler:2.11' in each feature module where I am using dagger2 even though I have same in base module, basically there should not be need to do. Thank you so much :) – N Sharma Sep 23 '17 at 15:20
  • You're right! I completely missed that, thanks for pointing that out. – Eugen Pechanec Sep 23 '17 at 15:21
  • If you see my base build.gradle I have `application project(':app') feature project(":main") feature project(":tv")` which is correct – N Sharma Sep 23 '17 at 15:22
  • Thank you so much +1 I will award bounty to your answer – N Sharma Sep 23 '17 at 15:23
  • @Nancy It should work if you have Dagger API only in the base project, it will be transitive. But the compiler apparently needs to be in every module. It's OK if you post this finding as an answer and accept it. – Eugen Pechanec Sep 23 '17 at 15:24
2

In your base module build.gradle you have

dependencies {
    // some other stuff        

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

While com.google.dagger:dagger:2.11 is transitively available to your feature module due to api keyword, kapt does not make com.google.dagger:dagger-compiler:2.11 transitively available to your feature module.

Therefore you need to add com.google.dagger:dagger-compiler:2.11 to all your feature module for dagger to generate classes

In feature module build.gradle

dependencies {
    // some other stuff        

    // add kapt 
    kapt 'com.google.dagger:dagger-compiler:2.11'
}
Weizhi
  • 1,027
  • 8
  • 22
1

I literally just had this issue.

Try removing 'jre7' from the Kotlin stdlib dependency.

HSFlik
  • 11
  • 1