本文介绍了Android Studio + Gradle + Android 注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to add AndroidAnnotations to Android Studio project that has a gradle build system. Has anyone done this? Can anyone help me with this? I do not even know where to start. I know how to add libraries to gradle but AndroidAnnotations requires 2 jar files and I do not know what should I do.

推荐答案

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 文件(你的应用模块的构建文件)

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

First add dagger and annotations version. You can also declare them in dependencies. This is just more convenientwhen you have a lot of dependencies.

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):

Finnaly, add this. This adds path for compiler and creates a dir for generated files (this dir will be called 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,右键单击文件夹并设置标记为源根目录"

Oh, yes, and after you build your aplication, you need to go to project root/build/apt_generated, right click on folder and set "Mark as source root"

这篇关于Android Studio + Gradle + Android 注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 14:50