Dagger 2 and android Studio: working but can't see generated classes

梦想的初衷 提交于 2019-11-30 10:57:55

Use the Android-Apt plugin by Hugo Visser:

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

buildscript {
  repositories {
    mavenCentral()
  }

  dependencies {
    // the latest version of the android-apt plugin
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
  }
}

android {
    compileSdkVersion 22
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.mateuyabar.android.dagger2test"
        minSdkVersion 22
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false // ignoring some references from dagger-compiler
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'javax.inject:javax.inject:1'
    compile 'javax.annotation:javax.annotation-api:1.2'
    compile 'com.google.dagger:dagger:2.0'
    apt 'com.google.dagger:dagger-compiler:2.0'
    provided 'org.glassfish:javax.annotation:10.0-b28'
}

Note the apt 'com.google.dagger:dagger-compiler:2.0' line as well. This should make the generated sources visible for AS.

I stumbled upon same problem, in my case I was testing new classes and tried to inject them separately into my main class, in that case Dagger won't generate any DaggerComponents from factory.

Simply, remember to put all injections that goes into one class in one Module & one Component. You cannot inject more than one DaggerComponent into one class. Besides that's the point of those components, to aggregate injected classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!