Can a JSR 305 annotation like javax.annotation.Nonnull
be used in code that will run on Android ? It does not look like the Dalvik VM is supporting the package and those annotations have a runtime retention policy. Has anyone got it working using the javax package (or alternatives like using eclipse JDT) ?

- 16,299
- 4
- 85
- 85
3 Answers
The javax.* package is not fully supported by dalvik or ADT. You can use normal annotations however. The full package list in the official docs.
That said, anything that targets .class files will not work. There's a library called dexmaker that helps with this problem.

- 2,563
- 1
- 21
- 24
-
Some javax packages are supported according to the docs but not the javax.annotation one. Have you tried using those annotations with android ? – Christophe Roussy Feb 28 '14 at 14:16
-
Yea they support the EGL, XML and crypto stuff – meredrica Feb 28 '14 at 14:22
Afaik, annotations can be missing at runtime (even with runtime retention), which will result in the annotation being silently dropped. So if your code compiles, then you're OK even if the VM that is later running your code does not know of the annotation.
In your case, you are using javax.annotation.Nonnull
, which is used by static analysis tools, but usually has no effect at runtime. So yes, you can use it.
In contrast, a hypothetical other annotation whose presence is relevant to your app at runtime, must be present on the classpath.
-
I have seen crashes when using reflection to iterate over a class that uses annotations which are not available at run-time. This was with javax.annotation.Nullable, on Android. Be careful with reflection. – Dave Cameron Oct 28 '14 at 18:47
Why not use androidx.annotation from Android standard sdk plartform

- 621
- 5
- 8
-
1This post doesn't look like an attempt to answer this question. Every post here is expected to be an explicit attempt to *answer* this question; if you have a critique or need a clarification of the question or another answer, you can [post a comment](//stackoverflow.com/help/privileges/comment) (like this one) directly below it. Please remove this answer and create either a comment or a new question. See: [Ask questions, get answers, no distractions](//stackoverflow.com/tour). – Dharman Aug 28 '19 at 07:57