How to use custom Java annotation processor in Gradle?

I worked on a simple java annotation processor that extends AbstractProcessor .

I was 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 creating a new Android project, and then added a separate module into which I put all my annotation processor code (the MyProcessor class, as well as the user annotation that it uses).

Then I added this new module depending on the HelloWorld android project.

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

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

+5
source share
3 answers

There is a plugin to make it work. It works fine with modules that are on Maven, not sure about local .jar files, but you are sure to try:

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

and here is 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.

+2
source

Use the android-apt plugin. Even better, run a sample project that already uses the android-apt plugin to develop local annotation processors.

Read about the sample project here .

+1
source

what you are doing is absolutely correct, here is an example of one of my projects, which I use as a library:

 dependencies { compile project(':boundservice') } 

of course, what you have to do is define the module that you added to the library as

android-library plugin, not an application.

This should happen automatically if you do this using the New Module โ†’ Android Library option, but maybe as this project, as I understand it ... has nothing to do with Android, you just have to select the Java Library option.

When you do this and synchronize the build.gradle file (maybe a small clean build should also help), you should be able to use your class files in an Android project.

0
source

Source: https://habr.com/ru/post/1201753/


All Articles