简体   繁体   中英

Annotation processor with android library (realm)

I'm trying to create an annotation processor that works on top of realm.io Sadly to use realm you need an android project, while to create an annotation processor you need a java one (in order to import javax.annotation.processing.*)

Anyone know a way to import AbstractProcessor and all the other needed stuff on an android library? I can't find I way to do this (already switched targetCompatibility and sourceCompatibility to 1.7)

a way to import AbstractProcessor and all the other needed stuff on an android library

The better question is: why do you want that?

If you are under impression that your processor would depend on Realm classes (or any other Android classes), — that's not the case. When you write code, that uses APT Mirror API, you don't have to reference those classes directly, only by names. The resulting code will look like this :

DeclaredType theBinder = lookup("android.os.Binder");
DeclaredType theIInterface = lookup("android.os.IInterface");
DeclaredType theCreator = lookup("android.os.Parcelable.Creator");
...
private DeclaredType lookup(CharSequence name) {
    return types.getDeclaredType(elements.getTypeElement(name));
}

You will then proceed to manipulate created TypeMirrors by using methods of Types and Elements utility classes. You can also convert those mirrors to alternative formats, such as Square's JavaPoet TypeName, but you really don't have to, because Mirror API provides most facilities you may ever need.

You definitely don't want to load Realm's classes inside your annotation processor. Firstly, there is simply not need for that. But more importantly, as you pointed out in the question, it is often not possible to share the same setup between annotation processor and it's runtime appliances. This issue isn't unique to Android, — for example, nobody expects to set up full-blown application server to compile a server program, that uses JAXB-aware annotation processor.

If you really want some piece of code from Realm in your processor, and it is not available as separate Java library, the simplest way is to just copy that code to your processor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM