Android @StringDef support annotations do not work when exporting to .aar file

I'm having problems with @StringDef support annotations. They work great when the library in which they are located is part of the project (and not the .aar file), but when I compile the library as .arar files and try to use them in another project, annotations are not applied.

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

The section "Using annotations in your own libraries" says

If you comment on your own libraries with these annotations and use Gradle to create the AAR artifact, during the build, the Android Gradle plugin will extract the annotation information and send it to your AAR file for use by your library clients. You can see the annotations.zip file inside the AAR where the information is written; it uses the IntelliJ external annotation XML format. This is necessary because .class files cannot contain enough annotation information to process the @IntDef information above; note that we need to write a link to a constant, not its value. The Android Gradle plugin will run the extraction annotation task as part of the if build (and only if) your project depends on the annotation support library. (Note that only source preservation annotations are placed in the .aar file; stored class-level values ​​are stored in .jar classes.)

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

I am using @Retention(RetentionPolicy.SOURCE) as it says what I need

The application I'm trying to use as .aar files has a dependency on the compile 'com.android.support:support-annotations:23.1.1 support library

What am I missing to make them work?

Edit: I double-checked that a project using them as .aar files also included an annotation handler.

+5
source share
2 answers

The problem was that the interfaces were private.

For some reason, the private interface works correctly when the library project is compiled as an add-on by this host project.

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

+1
source

As mentioned here , information about constants used as @ StringDef / @ IntDef values ​​will be lost after it is compiled into aar file. All that remains is the constant value.

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

+1
source

Source: https://habr.com/ru/post/1243622/


All Articles