我們只介紹AndroidStudio的配置方法,至於依舊使用Eclipse的同學,請繞道。
我們打開模塊的(不是全工程的)build.gradle文件,其內容大致是這個樣子:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
}
buildTypes {
release {
}
}
}
dependencies {
}
我們配置Dagger2只需要在dependencies中添加兩行說明即可: dependencies {
......
compile 'com.google.dagger:dagger:2.10'
annotationProcessor 'com.google.dagger:dagger-compiler:2.10'
}
然後就完成了所有配置過程。如果你在其他教程裏看到,還配置了apt相關的內容,比如在工程的build.gradle中添加:
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
並且在module中添加: apply plugin: 'com.neenbedankt.android-apt'
這種配置方法說明,當時所在的gradle版本低於2.2,而在2.2及以後的版本中,gradle官方添加了對Annotation Processors的支持(https://bitbucket.org/hvisser/android-apt),使用annotationProcessor代替了apt,因此無需再配置apt相關的屬性。好了,配置的教程就介紹完了,接下來我們就從《Dagger2教程二之基礎使用(原)》開始介紹。