• 7

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I want to know about Android Annotations, is it better way to use in all android projects?.

If correct, how to implement it. Is there any good tutorial for it?

If it is a wrong way. what are the drawbacks of Android Annotations?

Thanks in advance for the help.

Android Annotations is an annotation-driven framework that allows you to simplify the code in your applications and reduces the boilerplate of common patterns, such as setting click listeners, enforcing ui/background thread executions, etc.

You could go from having something like this:

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

        final TextView descriptionTextView = (TextView) findViewById(R.id.tv_description);
        final Button hideButton = (Button) findViewById(R.id.btn_hide);
        hideButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

To something like this:

public class MainActivity extends AppCompatActivity {

    TextView mDescriptionTextView;

    protected void onHideButtonClick() {

How it works

You annotate your activities and components, the annotations processor then generates classes (at compilation time) that extend your activities and components (i.e. your activities cannot be final) with an underscore suffix by default, so if you have MainActivity, now you will also have a MainActivity_ class.

This new class contains a well-written boilerplate code that does whatever the annotation specifies.

How to implement

I wrote this tutorial about how to integrate Android Annotations and even include an example on how integration tests get updated, check here.

That tutorial is valid as of today, using Android Studio ~1.5.1, and it tries to explain a little bit on how the internal works.

Should you use it?

I would say that if you have a small-medium project its fine. It will make your code easier to read. But if your application is bigger and contains a lot of navigation flows with complex activity/component life cycles, it can get a little bit hard to implement or difficult to debug and understand errors if something is not appropriately annotated.

Because of how Android Annotations operate, they embed themselves in the life cycle and doing so, you are now dependent of their lifecycle (e.g. if you annotate your views with @ViewById, then you cannot reference them in onCreate(), you need to make a method and annotate it with @AfterViews and when this method gets executed then your views are ready to be used). This is not necessarily a problem, you just need to have a good understanding of Android's behaviors and well, Android Annotations behaviors as well.

In summary, as in any library, if you depend on it, well you depend on it, so you might as well understand very thoroughly how it works. Your project now depends on someone else's.

  • 37
Reply Report

I don't use Android Annotations, not anymore. When I used this library, it was buggy and made debugging a nightmare. Another downside is that it lowers the portability of your code. If you're working alone on the project, then it's okay, you don't have this issue, but when you work in a team, you have to give this a second thought.

If you want to use it, there are plenty of tutorials right on their site.

An alternative: If you want to decrease the amount of code while making it really easy to use and understand, I suggest you the Butter Knife library. I use is a lot and didn't encounter any bugs so far. Very easy to use and read.

  • 7
Reply Report

Android Annotations is 3rd party library that was made to be all in one tool for Android. Its capable of dependency injection, thread handling, and more. I don't recommend using it; it's buggy, and unstable. In my current job I am working on a project and my task is to remove Android Annotations. I would suggest using Dagger 2, Butterknife, and RxJava

  • 4
Reply Report

Android Annotations is a library that "autogenerates" code for us by using some attributes or anotations like @EActivity, @ViewById, @OnClick. It's intended to facilitate and decrease coding time.

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

(Documentation here: https://github.com/excilys/androidannotations/wiki)

But... We don't use it, I completely agree with DDsix answer. Use SOLID principles and code what should be coded when and where it should be...

  • 4
Reply Report

Here is the example with Android Studio.

  1. Create an Android project with the "Empty Activity" template.
  2. Edit the project_root/build.gradle

    buildscript {
        repositories {
        dependencies {
            classpath 'com.android.tools.build:gradle:1.5.0'
    ==>     classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    allprojects {
        repositories {
    ==>     maven {
    ==>         url = 'https://oss.sonatype.org/content/repositories/snapshots'
    ==>     }
  3. Edit the app/build.gradle

    apply plugin: 'com.android.application'
    apply plugin: 'android-apt'  <============
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.2"
        defaultConfig {
            applicationId "com.just.myapplication"
            minSdkVersion 19
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:23.1.1'
    ==> apt "org.androidannotations:androidannotations:4.0-SNAPSHOT"
    ==> compile 'org.androidannotations:androidannotations-api:4.0-SNAPSHOT'
  4. Add a TextView and a Button in the main layout, activity_main.xml

        android:text="Not Click Yet"
        android:layout_below="@+id/textView" />

  5. Change the activity name to "MainActivity_" in AndroidManifest.xml

    ==> <activity android:name=".MainActivity_">
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>

  6. Now, your MainActivity.java could be simplified as below

    package com.just.myapplication;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.TextView;
    import org.androidannotations.annotations.Click;
    import org.androidannotations.annotations.EActivity;
    import org.androidannotations.annotations.ViewById;
    @EActivity (R.layout.activity_main)
    public class MainActivity extends AppCompatActivity {
        TextView mText;
        void button() {
            mText.setText("Button Clicked!");
  7. Try to run it with a device or a emulator to see how simple it works.

  • 2
Reply Report
      • 2
    • Thanks its really helpful but we will have to keep in mind 2 things 1- For calling MainActivity using anywhere in java files we will have to use MainActivity_ name instead of MainActivity 2- Sync project with gradle Files after cleaning the project if there is any issue