Search code examples
javaandroidstatic-analysisfindbugsjsr305

Can JSR 305 annotations be used with code that will run on Android?


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


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.