简体   繁体   中英

Annotation processing third party libraries

I'm writing an annotation processor for an Android project and have run into a situation. I would like to process annotated classes from a library module, ie app module depends on library module and needs to process annotated classes from the library module.

However, the annotation processor isn't able to "see" the annotations from the dependency, presumably because the code is already compiled. See here for an issue on another library. A demonstration of the problem with my project is on the branch here . The annotations from sample module are processed but the ones from the lib module are not.

This is all well and good and I'd resigned myself to living without this feature, but it turns out the Android data-binding library can process annotations from modules and even third party libraries. This library, for example, provides @BindingAdapter methods that are processed fine by the app.

I played around with setting retention types on the annotations I use to no avail. The only significant difference I can see is that the data-binding processors target methods while my methods target classes (types), but I don't think that should make a difference from the processor. So I'm thinking it might have something to do with the data-binding processor being part of the Android plugin but I'm not sure how that helps here.

Anyone know how this is done?

Android Data Binding can't process annotations on dependencies, either. Instead, it processes the libraries at the compile time of the library and saves the information to an intermediate file as part of its archive (eg jar file). It then loads that intermediate file information from the dependency instead of reading the annotations.

If you save the intermediate information as a resource of the jar file dependency, you can pull it from the jar file pretty easily. If I recall correctly, it should be in your class path and you can use the ClassLoader's getResource() method. My memory may be a little stale on this as it doesn't use the jar file to store the intermediate file information any more.

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