0

AndroidAnnotations

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. Android Annotation (AA) not only simplifies the code but also makes it easier to maintain and understand, it also helps developers shpe thier intent by letting AA generate the plumbing code at compile time. There are aslo some similar frameworks that archieves the same results and built for same purpose. Example of such includes Butter Knife, Dagger and so on. All of this have thier advantages and disadvantages based on their performance and features.

Features of AA

  1. Dependency injection
  2. Simplified threading model
  3. Event binding
  4. REST client
  5. No magic

Getting Started with AA First and foremost, importing the AA library is the first step. Below is an example of a configured graddle file.

buildscript {
    repositories {
      mavenCentral()
    }
    dependencies {
        // replace with the current version of the Android plugin
        classpath 'com.android.tools.build:gradle:2.3.0'
    }
}

repositories {
    mavenCentral()
    mavenLocal()
}

apply plugin: 'com.android.application'

def AAVersion = 'XXX'
dependencies {
    annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
}

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 25

        // If you have different applicationIds for buildTypes or productFlavors uncomment this block.
        //javaCompileOptions {
        //    annotationProcessorOptions {
        //        arguments = ["resourcePackageName": android.defaultConfig.applicationId]
        //    }
        //}
    }
}

To add additional AA plugins simply include in the dependencies of your graddle.

dependencies {
    annotationProcessor "org.androidannotations:{plugin-name}:$AAVersion"
    compile "org.androidannotations:{plugin-name}-api:$AAVersion"
}

NOTE: {plugin-name} should be replaced with the real name of the plugin to add.

Below are some example of how AA can help to simplify and tidy up your code.

  • Setting Content Layout In a scenario where a layout named R.layout.translate is to be set in an Activity, by default it is declared in the onCreate:
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.translate);
        }

However using AA one can simply do this: @EActivity(R.layout.translate) The @EActivity specifies that the layout R.layout.translate should be used as the Avtivity's layout. It is as simple as that.

  • Finding View

Also views can easily be declared as such:

    @ViewById(R.id.profile_image)
    ImageView profileImage;

This declaration applies to all sort of type views. From EditText to Layouts and more. Also one can simply handle onClicks by declaring:

@Click(R.id.profile_image) void onProfileImageClicked() { .... } It is also important to know that Classes declared with the @EActivity by default wll generate the following subclass, in the same package but in another source folder. This generated classes have the same name as the declared class but preceeded with an "". For instance, the following class:

package com.some.company;
@EActivity
public class MyActivity extends Activity {
  // ...
}

generates another:

package com.some.company;
public final class MyActivity_ extends MyActivity {
  // ...
}

This subclass adds behavior to your activity by overriding some methods (for instance onCreate()), yet delegating the calls to super. So therfore one needs to add _ to your activity names in AndroidManifest.xml: <activity android:name=".MyActivity" /> becomes <activity android:name=".MyActivity_" /> It is also important to know that this applies to every aspect of this class. Take for instance starting an Activity is declared : startActivity(this, MyListActivity.class); With AA it is now declares as: startActivity(this, MyListActivity_.class); Note the "_" ?

  • Passing Intents AA has its own custom Intent Builder. This can help to start an Activity or pass intent/bundles as easy as possible. Starting the activity MyActivity_.intent(context).start();

Building an intent from the activity Intent intent = MyActivity_.intent(context).get();

You can provide flags MyActivity_.intent(context).flags(FLAG_ACTIVITY_CLEAR_TOP).start();

You can even provide extras defined with @Extra in the activity MyActivity_.intent(context).myDateExtra(someDate).start();

To use startActivityForResult MyActivity_.intent(context).startForResult(REQUEST_CODE);

To get the result code and extra value from above intent

@OnActivityResult(REQUEST_CODE)
void onResult(int resultCode) {
}

For bundles MyActivity_.intent(context).withOptions(bundle).start();

Starting Services, adding flags to intents can all be archieved. For further info check the official page here


All Rights Reserved

Viblo
Let's register a Viblo Account to get more interesting posts.