2015-01-17

Level Up - Android Dev: How to get started with AndroidAnnotations (easy) (and build Chuck Norris app)

AndroidAnnotations is an open source framework for Android. Basically, you provide Java annotations for things, and the framework will auto-generate the necessary boilerplate code so you don't have to write it yourself. Runs at compile-time! (Read: No reflection or startup time impact) And, there's a lot of code AndroidAnnotations saves you from writing manually.

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.

Getting started..


How to add AndroidAnnotations to my project

Using 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 AndroidAnnotations

This 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
  • AndroidManifest.xml
  • HttpUtils.java (has helper method for retrieving text)

In MainActivity.java


In /res/layout/activity_main.xml

Nothing special here.



In MainFragment.xml


In /res/layout/fragment_main.xml

Nothing special here, except for the ids that will match those in MainFragment.java.


In AndroidManifest.xml



In HttpUtils.java

Nothing 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.

Linked Resources:



~ Danial Goodwin ~

ps - By the way, have you heard of LTE Discovery or Simply Advanced Unit Converter?

No comments: