I have an Android library module where a public class uses some annotations from an external library. The annotations are purely used internally, so I don't want to expose it as an API
Gradle dependency, leaking that dependency to clients, but keep it as an implementation
one.
However, the presence of the annotation causes warnings for applications using my library, since the annotation class can not be resolved. This is actually quite understandable - a public class contains a symbol which is not on the compile path of the client - but is there a way to have annotations kept internal when packaging a library - or somehow ignored by the calling application? The annotations are RUNTIME
retention ones, so they cannot be completely stripped out in a build step or similar.
Setup for illustrative purposes:
my-library:
build.gradle
hasimplementation com.example.foobar
which contains@Example
annotationFoo.java
is a public class annotated with@Example
:
@Example(foo = "bar")
public class Foo {
...
}
- Other classes in
my-library
itself require the annotation to be present in runtime
some-client:
build.gradle
hasimplementation com.example.mylibrary
which contains the classFoo
- This application uses
Foo
objects, but doesn't need to know about the annotation, however when building it will get:
classes.jar(com/example/Foo.class): warning: Cannot find annotation method 'foo()' in type 'Example': class file for com.example.Example not found
If I change implementation
to API
for com.example.foobar
, the warning is eliminated, but the application will now get the @Example
annotation on its build path, which is an internal implementation detail. Is there another way?