Transfer from http://blog.csdn.net/zjbpku/article/details/22976291
What's this?
The ANDROID-APT plugin assists in working with annotation processors in combination with Android Studio. It has the purposes:
- Allow to configure a compile time only annotation processor as a dependency, not including the artifact in the final APK O R Library
- Set up the source paths so, code that's generated from the annotation processor are correctly picked up by Android Stu Dio.
This plugin requires the Android or Android-library plugin, version 0.7.x or up, to being configured on your project.
including and using the plugin in your build script
Add the following to your build script to use the plugin:
Buildscript {repositories {mavencentral ()}
dependencies {
Replace with the current version of the Android plugin
Classpath ' com.android.tools.build:gradle:0.7.3 '
The latest version of the ANDROID-APT plugin
Classpath ' com.neenbedankt.gradle.plugins:android-apt:1.2 '}
}
Apply plugin: ' Android ' Apply plugin: ' Android-apt '
Passing processor arguments
Some annotation processor may require to pass the custom arguments, you can use the apt.argumentsfor that purpose. For instance Android Annotation Library needs the following configuration:
Apt {arguments {resourcepackagename android.defaultConfig.packageName androidmanifestfile Variant.processResources.manifestFile}}
Make sure to configure PackageName in the Android Defaultconfig block for this purpose. The arguments is processed for each variant when the compiler is configured. From this closure can reference Android, project and variant for the current variant.
configurating a compiler style dependency
Annotation processors generally has a API and a processor that generates code, which is used by the API. Depending on the project the processor and the API might is split up in separate dependencies. For Example,dagger uses, artifacts called Dagger-compiler and Dagger. The compiler artifact is only used during compilation and while the main dagger artifact are required at runtime.
To aid in configuring this dependency, the plugin adds a Gradle configuration named apt, can is used like this:
dependencies {apt ' com.squareup.dagger:dagger-compiler:1.1.0 ' compile ' com.squareup.dagger:dagger:1.1.0 '}
Configuration of other annotation processors
For annotation processors This include the API and processor in one artifact, there ' no special setup. You just add the artifact to the compile configuration like usual and everything would work like normal. Additionally, if code that's generated by the processor are to being referenced in your own code, Android Studio would now Cor rectly reference that code and resolve references to it.
History & Credits
This plugin was based on a script that I ' ve been using for some time which was the result of this post on Google + and this p OST on stackoverflow.com. Variations of the so post and my gists has been floating around for a while on the interwebs. That, and the fact, including scripts are a bit inconvenient pushed me to create this plugin.
License
This plugin was created by Hugo Visser and released on the public domain. Feel free to use and adapt as. To get on touch, hit me up Ontwitter orgoogle Plus. @
Late delivery of translations (slag-flipping)
Ext.: android-apt