6

I've been working on a simple java annotation processor that extends AbstractProcessor.

I've been able to successfully test this using javac -Processor MyProcessor mySource.java

The problem is integrating this into a simple Hello World android application using Android Studio.

I started by making a new Android Project, and then adding a separate module where I place all my annotation processor code (the MyProcessor class as well as the custom annotation it uses).

I then added this new module as a dependency of the HelloWorld android project.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':MyProcessorModule')
}

How do I then use the processor to generate code based on all the source files in the HelloWorld application?

James
  • 2,445
  • 2
  • 25
  • 35

3 Answers3

3

there's a plugin to make it work. It works perfectly with modules that are on Maven, not sure about local .jar files, but you sure give it a try:

here the plugin page: https://bitbucket.org/hvisser/android-apt

and here my build.gradle with it:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        ... add this classpath to your buildscript dependencies
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt' << apply the plugin after the android plugin

dependencies {
    // in dependencies you call it
    apt 'com.company.myAnnotation:plugin:1.0-SNAPSHOT'
    compile 'com.company.myAnnotation:api:1.0-SNAPSHOT'

and it should work.

Budius
  • 39,391
  • 16
  • 102
  • 144
1

Use the android-apt plugin. Better yet, fork a sample project that already uses the android-apt plugin for developing local annotation processors.

Read about the sample project here.

Brian Attwell
  • 9,239
  • 2
  • 31
  • 26
0

what you are doing is perfectly right, here is an example of one of my projects that I use as a library:

dependencies {
    compile project(':boundservice')
}

of course what you should do also is to define the module you added your library as

android-library plagin and not application.

This should happen automatically if you do it using the New Module -> Android Library option, but maybe as this project, as I understand... has nothing to do with Android you should just chose the Java Library option.

When you do that and sync the build.gradle file (maybe a little clean build should help as well) you should be able to use your class files in the Android project.

Emil Adz
  • 40,709
  • 36
  • 140
  • 187