5

I am trying to begin creating a javax annotation processor, im doing it from android studio for now. I just need the gradle dependency i think for it. Right now in gradle i have the following which i have tried:

provided 'javax.annotation:jsr250-api:1.0'

but when i try to build the following class it says it cant find AbstractProcessor class:

class MyAnnotationProcessor extends AbstractProcessor{
}

How do i get it to recognize this class ?

here is the exact error:

enter image description here

and my imports look like this:

enter image description here

and here is my java version:

 $java -version 
Picked up JAVA_TOOL_OPTIONS: -Xmx512m -Xms64m
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • Do you have imports in your class? What version of jdk do you have? – jbunting Mar 16 '16 at 01:09
  • Im on java 6. I put in the import but its not recognized – j2emanue Mar 16 '16 at 01:23
  • Share the whole class? Share the exact error message? – jbunting Mar 16 '16 at 01:23
  • i sent a photo of the issue. – j2emanue Mar 16 '16 at 01:27
  • It looks like something does not believe that you are using java 8. Your IDE might be pointing at an older version of the JDK? You don't need any dependency to use AbstractProcessor -- it ships with Java. – jbunting Mar 16 '16 at 17:41
  • here is the exact version of the JDK my IDE says its using. I got it from the IDE settings itself let me know if this is correct: /Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home – j2emanue Mar 16 '16 at 19:57

2 Answers2

12

AbstractProcessor is not accessible in android library module. To make it work, you can create a separate "java library" module and add its dependency in your main module. Below is the step by step description of the solution what worked for me:

  1. Create a new module in your android project. Right click on your project -> New -> Module -> select Java Library
  2. Follow the instructions to add module name, package name etc and finish
  3. Now create a new class CustomAnnotationProcessor in this library and extend the AbstractProcessor

  4. Following is the snapshot of my Custom annotation processor:

    import java.util.Set;
    import javax.annotation.processing.AbstractProcessor;
    import javax.annotation.processing.RoundEnvironment;
    import javax.lang.model.element.TypeElement;
    
    public class CustomAnnotationProcessor extends AbstractProcessor{
    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    return false;
    }
    }
    
  5. Add compile options in your app/build.gradle to java 1.7. see following snapshot: enter image description here

  6. Add dependency of this newly created module in your main module

  7. Compile and Run..!!

Don't forget to upvote if you liked my answer :)

Vinay
  • 1,859
  • 22
  • 26
-1

I got the same issue and I found the solution. See the link below:

  • Completely remove your Java module from project layout.
  • Make sure that only your code survives, delete all (or maybe just .iml file would suffice?) other stuff in module directory.
  • Add your sources again as module Java Library. Everything should work now.

https://stackoverflow.com/a/32933935/3899061

Community
  • 1
  • 1
xojan
  • 65
  • 1
  • 5