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)
6
  • Do you have imports in your class? What version of jdk do you have? Commented Mar 16, 2016 at 1:09
  • Im on java 6. I put in the import but its not recognized Commented Mar 16, 2016 at 1:23
  • Share the whole class? Share the exact error message? Commented Mar 16, 2016 at 1:23
  • i sent a photo of the issue. Commented Mar 16, 2016 at 1: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. Commented Mar 16, 2016 at 17:41

2 Answers 2

13

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 :)

Sign up to request clarification or add additional context in comments.

1 Comment

So is it possible to generate a file that contains Android classes?
-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.