Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio Annotation AbstractProcessor Not Found

I'm trying to add an annotation processor to my Android project, but I get an error saying that AbstractProcessor can't be found. I've created a separate java project in the meantime, but I'd rather keep everything in one Android project. What's the best way to include AbstractProcessor in my project? I'm using AndroidStudio and Gradle.

UPDATE:

I repackaged javax.annotation, javax.lang.model, and javax.tools to sit under my project's namespace (as per these instructions). I include a resources/META-INF/services/javax.annotation.processing.Processor that contains the full classname of my AbstractProcessor subclass, and everything compiles fine. However, the processor never gets run.

EDIT: To clarify in short, I have an Android library project that provides an annotation for a method. I want to provide a processor that causes a compile time error if the annotation is used incorrectly. If I try to put the processor in the Android library project, Gradle won't build it because javax.* classes aren't found. The only way I could get it to work is to create a java module in the library project, and put the processor there. However, that means I have to have 2 dependencies in any projects that I want to use this annotation; one to get the annotation, and one to get the processor.

like image 994
Eliezer Avatar asked Nov 07 '14 16:11

Eliezer


1 Answers

OK, i think there are some huge misunderstandings here. First you do not have to package anything from javax.* into your application. The annotation processor runs at compile time and finishes all its job in that phase. After that, it can generate some files or configure the project etc, and those files will be packaged into the application. The annotation processor (and its javax.* dependency) will be not, since the application does not need the processor at runtime. Only the developer needs it while building the project.

The ant-based building is also deprecated. You should use Android gradle (official) or maven (unofficial) plugins.

You could ask that the processor would still need the dependency at compile time. That is true, but the javax.* classes are provided by the compiler which is building the project. In case of command-line build and IntelliJ-based IDE JDK APT provides it, in-case of Eclipse the Eclipse JDT APT plugin provides the classes.

If you want to add your annotation processor to an Android gradle project, check out our example in AndroidAnnotations. You should note the use of the android-apt plugin, which correctly adds the compile-time dependency (the processor), and also sets up the source paths so IntellJ can pick them out without manual config.

Reflections to the edit:

Unfortunately you should move the processor to a separate project. This is the only way it can work, and by the way it is the natural way for annotation processors. For example dagger and AndroidAnnotations also have this setup (but for a slightly different reason). Yeah, that means you have to add two depedendencies, but you have to deal with that. Or you can add your library as a required dep to your processor. In that case, the user only has to add the processor as a dependency, since the lib will also be added as a transitive dep. But that would be quiet weird, since the lib is the main dep here, the processor is only a tool to verify its correct usage (if i understood your edit well).

like image 99
WonderCsabo Avatar answered Sep 21 '22 13:09

WonderCsabo