I'm trying to use Dagger 2 in an Android Studio Project. I've used the CoffeeMaker example. I've managed to make the app build and working however: - I don't success in seeing the generated code. - If I debug, I can't see it neither. - Moreover DaggerCoffeeApp_Coffee as marked as reed (Cannot resolve symbol)
我正在尝试在Android Studio项目中使用Dagger 2。我用过CoffeeMaker的例子。我设法使应用程序构建和工作: - 我没有成功看到生成的代码。 - 如果我调试,我也看不到它。 - 此外DaggerCoffeeApp_Coffee标记为芦苇(无法解析符号)
My gradle files are:
我的gradle文件是:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
and
apply plugin: 'com.android.application'
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'
provided 'com.google.dagger:dagger-compiler:2.0'
provided 'org.glassfish:javax.annotation:10.0-b28'
}
Thanks
2 个解决方案
#1
24
Use the Android-Apt plugin by Hugo Visser:
使用Hugo Visser的Android-Apt插件:
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.
请注意apt'com.google.dagger:dagger-compiler:2.0'行。这应该使生成的源对AS可见。
#2
6
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.
我偶然发现了同样的问题,在我的情况下,我正在测试新类并尝试将它们分别注入我的主类,在这种情况下,Dagger不会从工厂生成任何DaggerComponents。
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
简单地说,记住将所有注入放入一个模块和一个组件中的一个类中。您不能将多个DaggerComponent注入一个类。除了那些组件的重点,聚合注入的类
#1
24
Use the Android-Apt plugin by Hugo Visser:
使用Hugo Visser的Android-Apt插件:
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.
请注意apt'com.google.dagger:dagger-compiler:2.0'行。这应该使生成的源对AS可见。
#2
6
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.
我偶然发现了同样的问题,在我的情况下,我正在测试新类并尝试将它们分别注入我的主类,在这种情况下,Dagger不会从工厂生成任何DaggerComponents。
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
简单地说,记住将所有注入放入一个模块和一个组件中的一个类中。您不能将多个DaggerComponent注入一个类。除了那些组件的重点,聚合注入的类