Library projects - WonderCsabo/androidannotations GitHub Wiki
The id fields of the R inner classes of an Android project, such as R.id.someId
, are static final
constants. This allows us to use them in annotations parameter, which only accept compile time constant values.
To solve problems related to library projects, the Android team decided that the R inner classes fields of a library project would not be static final
anymore. Which means we can't use them in annotations in library projects any more.
Prior to AndroidAnnotations 2.7
, you can still use it in a library project but with a few limitations :
-
View injection and event binding can be done through "convention naming" instead of specifying the R id in the annotation (eg, if you have a
R.id.my_view
id, name your@ViewById
annotated fieldmyView
or your@Click
annotated method "myViewClicked"). -
Specifying the layout will have to be done using setContentView in onCreate. View binding & injection still work even if you inject the layout manually.
See this thread, this ACRA issue and this AA issue for more information.
Referencing ids by name
Since AndroidAnnotations 2.7
To enable full AndroidAnnotations support in library projects, we added a new resName
attribute on all annotations that accept an Android resource id.
This enables you to specify the resource id name as a String. Please note that AndroidAnnotations checks that the name exists at compile time.
A simple example:
@EActivity(resName="myLayout")
public class MyActivity extends Activity {
@Click(resName={"myButton1", "myButton2"})
public void someButtonClicked() {
}
}
Recognising library projects
Since AndroidAnnotations 4.0.0
AndroidAnnotations tries to recognise library projects automatically by parsing the project.properties file. However this file is no longer present in modern Gradle or Maven projects. You can explicitely tell AA that the project is a library project by setting the library
annotation processing option to true
.
Using R2 class
Since AndroidAnnotations 4.4.0
To workaround the problems explained above, the ButterKnife Gradle plugin generates an R2
class,
which contains final fields. You can use this class with AndroidAnnotations, by setting the useR2
annotation processing option to true
.