Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to extend AbstractProcessor to create java annotation processor

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)
like image 627
j2emanue Avatar asked Dec 25 '22 08:12

j2emanue


1 Answers

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

like image 155
Vinay Avatar answered May 11 '23 12:05

Vinay