/android-apt

UnOfficial Clone of Hugo Visser's The android-apt plugin

Primary LanguageGroovyThe UnlicenseUnlicense

What is this?

The android-apt plugin assists in working with annotation processors in combination with Android Studio. It has two purposes:

  • Allow to configure a compile time only annotation processor as a dependency, not including the artifact in the final APK or library
  • Set up the source paths so that code that is generated from the annotation processor is correctly picked up by Android Studio.

This plugin requires the android or android-library plugin (version 0.9.x or up) to be configured on your project.

Including and using the plugin in your build script

Add the following to your build script to use the plugin:

#!groovy
buildscript {
    repositories {
      mavenCentral()
    }
    dependencies {
        // replace with the current version of the Android plugin
        classpath 'com.android.tools.build:gradle:0.14.4'
        // the latest version of the android-apt plugin
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

Passing processor arguments

Some annotation processor may require to pass custom arguments, you can use apt.arguments for that purpose. For instance AndroidAnnotations needs the following configuration:

#!groovy
apt {
    arguments {
            resourcePackageName android.defaultConfig.applicationId
            androidManifestFile variant.outputs[0].processResources.manifestFile
    }
}

Make sure to configure packageName in the android defaultConfig block for this purpose. The arguments are processed for each variant when the compiler is configured. From this closure you can reference android, project and variant for the current variant.

Configurating a compiler style dependency

Annotation processors generally have a API and a processor that generates code that is used by the API. Depending on the project the processor and the API might be split up in separate dependencies. For example, Dagger uses two artifacts called dagger-compiler and dagger. The compiler artifact is only used during compilation, while the main dagger artifact is required at runtime.

To aid in configuring this dependency, the plugin adds a Gradle configuration named apt that can be used like this:

#!groovy
dependencies {
 apt 'com.squareup.dagger:dagger-compiler:1.1.0'
 compile 'com.squareup.dagger:dagger:1.1.0'
}

Note that in most cases you should probably use the provided configuration that was introduced in version 0.8.0 of the android plugin.

If your test code requires generated code to be visible in Android Studio, you can use the androidTestApt configuration:

#!groovy
dependencies {
 androidTestApt 'com.github.frankiesardo:android-auto-value-processor:0.1'
 androidTestCompile 'com.github.frankiesardo:android-auto-value:0.1'
}

Configuration of other annotation processors

For annotation processors that include the API and processor in one artifact, there's no special setup. You just add the artifact to the compile configuration like usual and everything will work like normal. Additionally, if code that is generated by the processor is to be referenced in your own code, Android Studio will now correctly reference that code and resolve references to it.

History & Credits

This plugin is based on a script that I've been using for some time which is the result of this post on Google+ and this post on StackOverflow.com. Variations of the SO post and my gists have been floating around for a while on the interwebs. That, and the fact that including scripts is a bit inconvenient pushed me to create this plugin.

License

This plugin is created by Hugo Visser and released in the public domain. Feel free to use and adapt as you like. To get in touch, hit me up on Twitter or Google Plus.