Question

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) ?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Why not use androidx.annotation from Android standard sdk plartform

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top