Android studio + Gradle + Android Annotations

Joh*_*ohn 8 android annotations gradle android-studio

我正在尝试将AndroidAnnotations添加到具有gradle构建系统的Android Studio项目中.有没有人这样做过?谁能帮我这个?我甚至不知道从哪里开始.我知道如何将库添加到gradle但AndroidAnnotations需要2个jar文件,我不知道该怎么办.

小智 13

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 :)
Run Code Online (Sandbox Code Playgroud)

这是你需要做的:

  • 您需要修改build.gradle文件(应用程序模块的构建文件)

首先添加匕首和注释版本.您还可以在依赖项中声明它们.当你有很多依赖项时,这会更方便.

ext.daggerVersion = '1.0.0';
ext.androidAnnotationsVersion = '2.7.1';

configurations {
    apt
}
Run Code Online (Sandbox Code Playgroud)

添加依赖项:

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}"

}
Run Code Online (Sandbox Code Playgroud)

Finnaly,加上这个.这为编译器添加了路径并为生成的文件创建了一个目录(这个目录将被称为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
    ]
}
}
Run Code Online (Sandbox Code Playgroud)

哦,是的,在您构建应用程序之后,您需要转到项目root/build/apt_generated,右键单击文件夹并设置"标记为源根"

  • 为什么需要Dagger?它与Android Annotations有某种关联? (2认同)
  • 编译"com.googlecode.androidannotations:androidannotations api:$ {androidAnnotationsVersion}"应该编译"com.googlecode.androidannotations:androidannotations-api:$ {androidAnnotationsVersion}"..注意你的遗失' - '. (2认同)