Android Dagger 2 Generated annotation JSR250
When you’re using Dagger 2.0 in your Android projects, you’ll quickly realize you need to include
an additional dependency to provide the @Generated
annotation (from JSR-250). [**]
These are the common options typically seen in online Dagger examples:
- javax.annotation:jsr250-api:1.0
- org.glassfish:javax.annotation:10.0-b28
- javax.annotation:javax.annotation-api:1.2
So which one should you use?
Let’s do a quick comparison of the 3 jars:
-
javax.annotation:jsr250-api:1.0
- Jar file size: 5.7kb
- Date of javax directory: Apr 27, 2006
- License: https://glassfish.java.net/public/CDDLv1.0.html
- Created-By: 1.5.0_05-b05 (Sun Microsystems Inc.)
-
org.glassfish:javax.annotation:10.0-b28
- Jar file size: 20kb
- Date of javax directory: Oct 8, 2008
- License: https://glassfish.dev.java.net/nonav/public/CDDL+GPL.html
- Created-By: Apache Maven Bundle Plugin
-
javax.annotation:javax.annotation-api:1.2
- Jar file size: 26kb
- Date of javax directory: Apr 26, 2013
- Created-By: Created-By: 1.7.0_09 (Oracle Corporation)
- License: https://glassfish.dev.java.net/nonav/public/CDDL+GPL.html
So the second contains slightly more meta-data files and a different license (includes GPL) than the first option, but otherwise they contain essentially the same content - e.g. the same .class files.
The third one contains a few extra annotations, including: @ManagedBean
, @Priority
, but is pretty much the same as the second one.
Verdict: use the first/oldest one: javax.annotation:jsr250-api:1.0
Final note: when you include the dependency in your android project, use the provided
scope instead of compile
, to prevent the JSR-250 annotations from being distributed with your app, in other words:
dependencies {
compile 'com.google.dagger:dagger:2.0.1'
provided 'javax.annotation:jsr250-api:1.0'
apt 'com.google.dagger:dagger-compiler:2.0.1'
}
** PS. if you don’t include one of these JSR-250 jars in your project, you’ll get this compile error in one of the files generated by dagger-compiler:
Error:(17, 24) error: package javax.annotation does not exist
Error:(20, 2) error: cannot find symbol class Generated
Error:(21, 14) error: dagger.internal.codegen.ComponentProcessor was unable to process this class because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.