For example, want to run something in a background thread? Annotate the method with `@Background`. Want to run something in the main/UI thread? (rub some bacon on it) Annotate the method with `@UiThread`.
In this article, I provide a quick walkthrough for creating a very simple Chuck Norris facts app. I'll assume that you already have basic Android knowledge (and using Android Studio + Gradle). Then, at the end, I'll explain in more details some issues/gotchas I ran into while using AndroidAnnotations for the first time, and mention other goodies.
How to add AndroidAnnotations to my projectUsing Android Studio + Gradle, the only file you need to edit is your app's `build.gradle`. We will add a dependency for android-apt and some related configurations. The changes to a new project are highlighted:
Now, your project should be ready for adding the annotations and processing them.
- Note: I chose to put the buildscript dependency in the app's build.gradle file so that it only affects the one module. You could also put the android-apt buildscript dependency in your project's root build.gradle so that all sub-projects/modules would be affected.
How to create a very simple Chuck Norris facts app using AndroidAnnotationsThis very simple app's feature-set includes: Ability to press a button to download and show a random fact.
For simplicity, Strings have not been extracted or made `static final`. And, the code is well-commented to explain each AndroidAnnotations-specific feature introduced.
Here's the following files we are going to edit/add:
- MainActivity.java + activity_main.xml
- MainFragment.java + fragment_main.xml
- HttpUtils.java (has helper method for retrieving text)
In /res/layout/activity_main.xmlNothing special here.
In /res/layout/fragment_main.xmlNothing special here, except for the ids that will match those in MainFragment.java.
In HttpUtils.javaNothing special here related to AndroidAnnotations.
If running this code, then you may want to add a check for Internet availability. And, you can check out the full source code, which has that check and a few more tweaks: https://github.com/danialgoodwin/android-learn/tree/master/sample-apps/framework--android-annotations--chuck-norris
When you build the project, all of the generated code can be found in `/app/build/generated/source/apt/debug`. You'll be able to see exactly what code will be executing during runtime and where any performance issues may be.
There is a bunch more annotations and features available. But, hopefully, this was enough to get you started and slightly interested in learning more. ;)
Gotchas (Things to look out for)Here's a few issues I ran into while setting up AndroidAnnotations for the first time. Hopefully, it will save others some time.
- When navigating to an Activity or Fragment annotated, make sure to navigate to the version with the underscore after it because that's the file with all the auto-generated code.
- I was not able to use an annotated Fragment that was an inner class of an Activity.
~ Danial Goodwin ~
ps - By the way, have you heard of LTE Discovery or Simply Advanced Unit Converter?