https://github.com/omaraflak/annotation-processor-sample
Simple example to illustrate the use of android annotation and annotation processor.
https://github.com/omaraflak/annotation-processor-sample
android annotation annotation-processor annotationprocessor generated-code processor tutorial
Last synced: 10 months ago
JSON representation
Simple example to illustrate the use of android annotation and annotation processor.
- Host: GitHub
- URL: https://github.com/omaraflak/annotation-processor-sample
- Owner: omaraflak
- Created: 2017-08-17T14:37:21.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-08-15T13:30:26.000Z (over 1 year ago)
- Last Synced: 2025-04-12T14:24:09.916Z (10 months ago)
- Topics: android, annotation, annotation-processor, annotationprocessor, generated-code, processor, tutorial
- Language: Java
- Size: 176 KB
- Stars: 4
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Annotation Processor Example
Simple example to illustrate the use of android annotation processor.
This code will create an annotation named `@Activity` which is meant to target activities. The processor will generate a `Navigator` class with `static` methods to start the annotated activities.
# Creation Steps (Android Studio)
#### 1) File > New > New Module... > Java Library -> Name it "annotation"
#### 2) File > New > New Module... > Java Library -> Name it "processor"
#### 3) In processor build.gradle :
```
dependencies {
/*...*/
implementation project(':annotation')
implementation 'com.squareup:javapoet:1.9.0'
implementation 'com.google.auto.service:auto-service:1.0-rc3'
}
```
#### 4) In app build.gradle :
```
dependencies {
/*...*/
implementation project(':annotation')
annotationProcessor project(':processor')
}
```
#### 5) Create new file called `Activity.java` in annotation module :
```java
@Target(ElementType.TYPE) // To target classes
@Retention(RetentionPolicy.SOURCE)
public @interface Activity {
}
```
#### 6) Create new file called `ActivityProcessor.java` in processor module :
```java
@AutoService(Processor.class) // DON'T FORGET THIS
public class ActivityProcessor extends AbstractProcessor{
@Override
public synchronized void init(ProcessingEnvironment processingEnvironment) {
/* initialize variables */
}
@Override
public boolean process(Set extends TypeElement> set, RoundEnvironment roundEnvironment) {
/* code generation happens here (using JavaPoet) */
return false;
}
@Override
public Set getSupportedAnnotationTypes() {
return Collections.singleton(Activity.class.getCanonicalName());
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
}
```
#### 7) Annotate any class and Rebuild your project to generate the code
# Sample
* [Annotation](https://github.com/OmarAflak/Annotation-Processor-Sample/blob/master/annotation/src/main/java/me/aflak/annotation/Activity.java)
* [Processor](https://github.com/OmarAflak/Annotation-Processor-Sample/blob/master/processor/src/main/java/me/aflak/processor/ActivityProcessor.java)