-3

I'm trying to make an annotation processor which takes an integer. It's working fine and all if I use explicit integer. But when I use value from android databinding BR class:

@SomeAnnotation(BR.someField)

It says that the BR class not found.

I'd guess that the android databinding BR generation is not yet executed thus the error. Is there any way to flag my annotation processor to begin processing after databinding process finished?

Fadli
  • 976
  • 9
  • 24

1 Answers1

0

The question would also be, what you actually want to do with this value. If you actually need this value within your annotation processor, you might be out of luck, since class with the field itself is only created after the point in the compilation process where it is needed. If you need the value on runtime, you might have the same issue. But then you should consider changing your implementation, since handling annotations on runtime is very expensive.

Now coming to the point which might work. If you need the value to write it to sources you generate with the annotation processor, you could as well use this field as string value and write it to the newly created sources. You would only have to make sure to have the right imports set as well.

@SomeAnnotation("BR.someField")

When the generated code is compiled then, this field would behave exactly the same as if you would have put it in your self-written sources. Just make sure it does not end up in the definition of another annotation.

final int someField = BR.someField;
tynn
  • 38,113
  • 8
  • 108
  • 143
  • I'm wondering why the value of R.java files can be referenced in the annotation processor class (e.g. Butterknife) but the BR.java file can't. What I'm trying to do is to have a function annotated with `@SomeAnnotation(BR.someField)`. Then that function will be called whenever there's a PropertyChanged with `fieldId` equals to `BR.someField`. I might not need the value in the a.p. Inputting it as a String is a possible solution, not thinking about that before. But I'd lost the autocomplete and stuff. – Fadli May 11 '17 at 17:06
  • `R` is created by the Android tool chain, while `BR` is created by an annotation processor. – tynn May 12 '17 at 15:05
  • The folks that create `@Bindable` ap also use String as the solution for referencing value in `BR` class. I'll mark this as the answer then. – Fadli May 16 '17 at 05:41