After 4 months I am 4 months older and a little smarter :) If you want to use
Annotations in Android use http://jakewharton.github.io/butterknife/.
It is way better and it is easy to set up :)
以下是您需要执行的操作:
- 您需要修改build.gradle文件(应用程序模块的构建文件)
首先添加Dagger和注释版本。您也可以在依赖项中声明它们。当您有很多依赖项时,这只是更方便的方法。
ext.daggerVersion = '1.0.0';
ext.androidAnnotationsVersion = '2.7.1';
configurations {
apt
}
添加依赖:
dependencies {
apt "com.googlecode.androidannotations:androidannotations:${androidAnnotationsVersion}"
compile "com.googlecode.androidannotations:androidannotations-api:${androidAnnotationsVersion}"
apt "com.squareup.dagger:dagger-compiler:${daggerVersion}"
compile "com.squareup.dagger:dagger:${daggerVersion}"
}
最后,添加以下内容。这将为编译器添加路径并创建一个生成文件的目录(该目录将被称为apt_generated):
android.applicationVariants.each { variant ->
aptOutput = file("${project.buildDir}/source/apt_generated/${variant.dirName}")
println "****************************"
println "variant: ${variant.name}"
println "manifest: ${variant.processResources.manifestFile}"
println "aptOutput: ${aptOutput}"
println "****************************"
variant.javaCompile.doFirst {
println "*** compile doFirst ${variant.name}"
aptOutput.mkdirs()
variant.javaCompile.options.compilerArgs += [
'-processorpath', configurations.apt.getAsPath(),
'-AandroidManifestFile=' + variant.processResources.manifestFile,
'-s', aptOutput
]
}
}
噢,是的,在您构建应用程序之后,您需要转到项目根目录/build/apt_generated,在文件夹上右键单击并设置“标记为源根目录”