Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Support Annotations @StringDef not working when exported in a .aar file

I'm having problems with the @StringDef support annotations. They work perfectly fine when the library they are in is part of the project (not an .aar file), but when I compile the library as .aar files and try to use them in another project, the annotations aren't enforced.

http://tools.android.com/tech-docs/support-annotations

Under the section "Using annotations in your own libraries" it says

If you annotate your own libraries with these annotations, and use Gradle to build an AAR artifact, at build time the Android Gradle plugin will extract annotation information and ship it inside your AAR file for use by clients of your library. You can take a look at the annotations.zip file inside the AAR where the information is recorded; this is using IntelliJ's external annotations XML format. This is necessary because .class files cannot contain enough information about annotations to handle the @IntDef information above; note that we need to record a reference to the constant itself, not its value. The Android Gradle plugin will run the extract annotations task as part of the build if (and only if) your project depends on the annotations support library. (Note that only the source-retention annotations are placed in the .aar file; the class level retentions are kept in classes.jar.)

I opened up the .aar file and inside there is the annotations.zip file, and if I extract that, there's an XML file that has all the correct annotations.

I'm using the @Retention(RetentionPolicy.SOURCE) as it says I need to

The app that I'm trying to use these in as .aar files does have a dependency on the support library compile 'com.android.support:support-annotations:23.1.1

What am I missing to make these work?

Edit: I double checked that the project using these as .aar files also has the annotation processor turned on.

like image 219
Ben987654 Avatar asked Feb 23 '16 00:02

Ben987654


2 Answers

The issue was the interfaces were private.

For some reason a private interface works properly when a library project is compiled by it's host project as a sub module.

When the library project is used as an .aar file, the private interface no longer works.

like image 99
Ben987654 Avatar answered Oct 27 '22 10:10

Ben987654


As mentioned here, the information about constants used as @StringDef/@IntDef values will be lost once it is compiled into aar file. What remains is just values of the constants.

@IntDef annotations cannot be class retention because the thing they need to record (which constants are valid) cannot be expressed in the .class file -- the only data stored there for an annotation is the value of the constant, not some sort of pointer to the constant itself.

like image 23
tcrondo Avatar answered Oct 27 '22 08:10

tcrondo