29

I would like to do some annotation processing based on the example in the following link: http://www.zdnetasia.com/writing-and-processing-custom-annotations-part-3-39362483.htm.

However, I would like to implement this in my Android project, and it seems I cannot use the package with the android platform. Do I need to add an external jar or is there something I'm missing?

Thanks.

srowley
  • 807
  • 1
  • 12
  • 24

8 Answers8

23

The javax.annotation.processing package is not included in Android. You don't need to include this inside your compiled app module. You just need this dependency for your compiler/processor module during build time. Therefore, inside your app's build.gradle you should add the following after including your annotation processor:

provided 'org.glassfish:javax.annotation:10.0-b28'
Brian Attwell
  • 9,039
  • 2
  • 30
  • 26
18

There is a much easier way if you use Android Studio / Gradle.

I found the following looking through the answers of the question How do I add javax.annotation.Generated to Java SE 5?:

For a Gradle build the dependency should be

compile 'org.glassfish:javax.annotation:10.0-b28'

Community
  • 1
  • 1
Dirk
  • 1,951
  • 1
  • 19
  • 25
11

The javax.annotation.processing package is not included in Android, but the Android VM team describes how to include extra javax packages here - it might be applicable to your question.

approxiblue
  • 6,822
  • 16
  • 49
  • 58
Jens
  • 16,629
  • 4
  • 53
  • 50
  • Thanks, this looks like it is what I need! – srowley Mar 10 '12 at 23:09
  • 12
    Cannot see the linked solution. That's why we should quote/copy/refer instead of plain linking – denispyr Nov 01 '15 at 10:58
  • Updated the link for reference. For the record, the linked guide uses an old version of Android (with Ant build scripts); you'd want the gradle solutions in the other answers. – approxiblue Nov 18 '16 at 15:38
6
implementation 'org.glassfish:javax.annotation:10.0-b28'
compileOnly 'com.github.pengrad:jdk9-deps:1.0'

Add these two dependencies in your build.gradle file, this will work.

Aniruddh Parihar
  • 2,674
  • 2
  • 19
  • 34
Vineet Rathee
  • 61
  • 1
  • 1
3

compileOnly 'org.glassfish:javax.annotation:10.0-b28'

user2212515
  • 1,200
  • 1
  • 11
  • 9
2

Simply adding compile 'javax.annotation:javax.annotation-api:1.2' should do the trick.

see here for all available versions: https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api

TmTron
  • 12,918
  • 5
  • 78
  • 119
ToBe
  • 87
  • 1
  • 8
2

for kotlin DSL you need to use compileOnly("org.glassfish:javax.annotation:10.0-b28")

for groovy DSL you need to use compileOnly 'org.glassfish:javax.annotation:10.0-b28'

foroogh Varmazyar
  • 777
  • 1
  • 9
  • 14
-4

I find a method in butterknife. Add this in build.gradle:

compile files(org.gradle.internal.jvm.Jvm.current().getJre().homeDir.getAbsolutePath()+'/lib/rt.jar')
Peter Tretyakov
  • 3,284
  • 6
  • 36
  • 52
turbofan
  • 318
  • 3
  • 7