Android Studio: Annotation Processor "depends on" android.jar?

992 views
Skip to first unread message

Kiran Rao

unread,
Nov 13, 2014, 8:07:03 AM11/13/14
to adt...@googlegroups.com
I am developing an annotation processor using Android Studio. My project has three modules:

anno-api- This contains that API - i.e, just defines the annotations and some constants
anno-processor - This has a compile dependency on anno-api and it contains the class extending AbstractProcessor
sample - This demonstrates how to use the annotations.

The setup is working just fine. Now, I want to do better validation in my Annotation Processor. For this, both anno-api and anno-processor need to have access to the Android API.
For example, consider this annotation. It has a method of type Class but I want to restrict this to just sub-classes of Activity.

@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface AwesomeActivity {
   
Class<? extends android.app.Activity> base();
}

How do I go about doing this? How do I tell anno-api and anno-processor to depend on Android classes?

Here are the things I tried:

1) I tried making both anno-api and anno-processor as Android Library projects.
2) I tried adding android.jar in the lib/ folder of the anno-api project and adding a dependency to the JAR file in build.gradle.

In both cases the I end up with compile errors in my Annotation Processor. Basically it refuses to recognize the javax.annotation.* and other such classes. I suspect that at this point, only the classes present in android.jar are recognized.

Kiran Rao

unread,
Nov 17, 2014, 1:25:25 AM11/17/14
to adt...@googlegroups.com
Bump! Any ideas on this? Or perhaps this is not the right forum for this question?

Jake Wharton

unread,
Nov 17, 2014, 5:35:25 PM11/17/14
to adt...@googlegroups.com

I'm also very interested in this. At one point Xav said "We should support this" but I'm not sure if a bug was ever filed to track it.

When I get back to a computer I'll look it up and send with more info. I also have a workaround for this that you can use temporarily.

--
You received this message because you are subscribed to the Google Groups "adt-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adt-dev+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Xavier Ducrohet

unread,
Nov 18, 2014, 1:38:51 AM11/18/14
to adt...@googlegroups.com
Would you might making a very very simple project that shows the issue? That'll help me make sure we can make something that works.

--
You received this message because you are subscribed to the Google Groups "adt-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adt-dev+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Xavier Ducrohet
Android SDK Tech Lead
Google Inc.
http://developer.android.com | http://tools.android.com

Please do not send me questions directly. Thanks!

Kiran Rao

unread,
Nov 18, 2014, 2:41:25 AM11/18/14
to adt...@googlegroups.com
@Xavier, here is a skeleton project I quickly put together. Do let me know if you need more details:

https://github.com/curioustechizen/studio-apt-android-dep-issue

Jake Wharton

unread,
Nov 18, 2014, 5:30:43 PM11/18/14
to adt...@googlegroups.com
A couple months ago I played with adding the support annotations jar as a dependency to an annotation processor API similar to your 'api' module: https://gist.github.com/JakeWharton/e8a3685feb6a94b23393

I half-started expanding this into a Gradle plugin which included aar unpacking for provided deps and a provided android.jar for the actual processor module. Never got around to finishing it.

To unsubscribe from this group and stop receiving emails from it, send an email to adt-dev+unsubscribe@googlegroups.com.

Kiran Rao

unread,
Nov 19, 2014, 5:22:34 AM11/19/14
to adt...@googlegroups.com
Thanks a ton Jake! Using your work-around I was able to get it to work. I have updated the github repo with the work-around. What do those additional lines in the build.gradle actually do?

@Xavier, is there a bug filed for this issue that I can follow?

Jake Wharton

unread,
Nov 20, 2014, 1:16:35 AM11/20/14
to adt...@googlegroups.com
They initialize the internal components of the Android plugin (thus subject to break at any time) which know how to resolve the SDK location for the project. After doing this they add the Maven m2 repository from inside the SDK so that artifacts inside the SDK can be resolved.

This also relies on the fact that the support-annotations artifact is a jar, rather than the normal aar, and thus don't require additional processing in order to add them as dependencies.

Roman Mazur

unread,
Nov 22, 2014, 2:11:22 PM11/22/14
to adt...@googlegroups.com
@Xavier Here is another example. A bit different yet. Processor depends not on Android classes but on Android library. https://github.com/stanfy/enroscar/tree/master/async
It is an Android library. Hence, it's packaged as AAR, not JAR. There is also an annotation processor that handles annotations of that library, which is packaged as JAR but depends on annotations that are part of the Android library. I was trying to raise such a question some time ago. But the community was silent. My current solution is to declare a new configuration for Android library and add dependency on this configuration in the processor project. I also remove such a dependency from the processor's POM. Another variant would be to extract annotations to a separate artifact. But I chose a different way. It has its consequences that you can find in a related discussion.

deviant

unread,
Sep 17, 2015, 8:52:53 PM9/17/15
to adt-dev
I just realized that all popular annotation processing libs like ButterKnife, AndroidAnnotations, Dart, Dagger etc are powered by maven. Is this means gradle doesnt support accessing android classes from java library?
Reply all
Reply to author
Forward
0 new messages