Moving to AndroidAnnotations Tips and Tricks


AndroidAnnotations (AA) is a very useful library, which allows you to save a lot of time and to simplify the code. Today I would like to share some good advices about AA that I have learned from experience. The article is targeted to both: people who are already using AA and also for beginners, therefore there will be some links to documentation.

What is AndroidAnnotations?

From the official site: “AndroidAnnotations is an Open Source framework that speeds up Android development. It takes care of the plumbing, and lets you concentrate on what’s really important. By simplifying your code, it facilitates its maintenance.”

More technically AA is using Java Annotations to generate code at compile time. It is useful because it reduces boilerplate, simplifies the codebase and encourages good practices. AA has no negative impact on app performance, which is important in mobile world.

Setting up the environment

AA authors did a great job with the documentation. ConfigurationAndroid Studio with Gradle and FirstActivity you have got a lengthy explanation on how to setup AA depending on your IDE/build system. These links should be enough to get up & running, so we will jump straight to the code. In case of trouble – leave me a comment below


Here’s an android code using AA. It enables Toast usage from any thread without worring about calling Toast from main thread. It uses injection (@Bean and @RootContext) and @UiThread (with REUSE, I’ll talk about it later) from Android Annotations library.

Moving to AA

Starting a new project with AA is quite simple, but when you want to add AA support to an existing codebase, and the project isn’t small, then you probably won’t be able to refactor everything at once.

Note that if you want to inject ToastManager with @Bean annotation in any standard Android class then you should enable AA on these classes, but remember to search through whole project for any new instance creations and add _ at the end of the class name. The underscore postfix indicates that this class was altered by AA.

With other classes (those which you will want to annotate with @EBean) it might not be that simple because they will have to have a constructor with no parameters or with Context type parameter.

If you want to use something like ToastManager (which I like to write) in classes that do not support AA yet, you can access it by calling ToastManager_.getInstance_(MyApplication_.getInstance()).

Something about @UiThread

When using @UiThread, by default it’s done with a Handler and new Runnable. In the code above I used a useful addon – UiThread.Propagation.REUSE. It will use a Handler only if called off main thread – otherwise it will simply call your method.

If you’re calling method annotated with @UiThead and view is destroyed after the call, you might get a NullPointerException when accessing activity or view items. Propagation.REUSE can save you if the calling method is on the main thread (because annotated method will simply be called instead of using Handler). Keep in mind though – if you’re calling annotated method off main thread, NullPointerException might still be thrown.


If want to make your life easier and your code more friendly/readible, then go for AndroidAnnotions. It will take some time to move everything/most of the stuff, but the bright side is that you don’t have to do it immediately. With a few tricks you can easily adopt AndroidAnnotations to an existing project and then refactor it on the go. I strongly recommend to read AA wiki page since it has a lot of useful information and it’s well written.

I hope you enjoyed this. If you have any questions or suggestions please leave comments and I will reply.

Do you like this post? Want to stay updated? Follow us on Twitter or subscribe to our Feed.


Piotr Zientarski

Latest Blogposts

25.01.2021 / By Daria Karasek

The Top Scalac’s Posts of 2020

We’ve been writing more than ever this year with 36 articles about tech, business, and backstage at Scalac.

20.01.2021 / By Daria Karasek

Why Developers Should Pay Attention to ZIO in 2021

We already know the cases of companies big, and small adopting ZIO in their production apps. So, how is this change coming? Let’s explore it in more detail.

20.01.2021 / By Mateusz Sokół

Build your own Kafka in ZIO – Queues & Fibers

Queuing and messaging platforms have been gaining in popularity in recent years. They solve numerous problems based on asynchronous message passing or consumer and producer patterns. In this blog post, we're going to build a basic message broker functionality with ZIO for our internal clinic messaging system, specifically with ZIO Queues and ZIO Fibers.

Need a successful project?

Estimate project