如何在 build.gradle 中添加 Dagger 2
自 Gradle 2.2 发布以来,不再使用 android-apt 插件。应使用以下设置 Dagger 2 的方法。对于旧版 Gradle,请使用下面显示的上一个方法。
对于 Gradle> = 2.2
dependencies {
// apt command comes from the android-apt plugin
annotationProcessor 'com.google.dagger:dagger-compiler:2.8'
compile 'com.google.dagger:dagger:2.8'
provided 'javax.annotation:jsr250-api:1.0'
}
对于 Gradle <2.2
要使用 Dagger 2,必须添加 android-apt
插件,将其添加到根 build.gradle:
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
然后应用程序模块的 build.gradle 应该包含:
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
android {
…
}
final DAGGER_VERSION = '2.0.2'
dependencies {
…
compile "com.google.dagger:dagger:${DAGGER_VERSION}"
apt "com.google.dagger:dagger-compiler:${DAGGER_VERSION}"
}
参考: https : //github.com/codepath/android_guides/wiki/Dependency-Injection-with-Dagger-2