Configure and use AndroidAnnotation

Source: Internet
Author: User

Configure and use AndroidAnnotation
Background

With the popularity of Android mobile phones in the market, the demand for Android development is also growing. However, in the process of Android development, many operations are complicated and unnecessary. Although there are a lot of code to be written in one day, it may also be an interface with several features. Therefore, it is necessary to simplify and reuse the code in Android development.
Yesterday, I accidentally saw the third-party framework AndroidAnnotation In the Netease cloud class. It is said that it can greatly simplify Android programming and improve programming efficiency. With the mentality of trying a good thing, I configured and tried it today.

Configuration

The official website of AndroidAnnotation is http://androidannotations.org /. But in fact, there is only one example code on this website to show how it can simplify the code. The actual content is mainly on github.
Open git through a connection and go to its git wiki. Here, you can download the jar package and use the guide on how to use it. Including how to configure on Eclipse and IntelliJ. However, it is relatively simple here. If you configure it according to its method, you may encounter many problems. In addition, when bz uses AndroidStudio, although it is based on IntelliJ, I do not know why there are still some gaps in details. For example, there is no Annotation Processing configuration item in AndroidStudio. Therefore, we need to find a more concise method for AndroidStudio.
Thanks to Gradle, we can directly use the build File to introduce AndroidAnnotation. Five steps are required.

1. Introduce the dependency on android-apt. Add the following code to the build file of the app module.

Buildscript {repositories {mavenCentral ()} dependencies {classpath 'com. neenbedankt. gradle. plugins: android-apt: 1.2 + '} apply plugin: 'android-apt' // Add the android-apt plug-in.

2. Set the android-apt parameter. Replace the package name with your application name. In addition, outputs [0] must be added in the new android-studio version.

Apt {arguments {androidManifestFile variant. outputs [0]. processResources. manifestFile resourcePackageName your package name }}

3. Use apt to introduce dependencies on androidannotation.

dependencies {    apt org.androidannotations:androidannotations:3.0+              compile org.androidannotations:androidannotations-api:3.0+    compile fileTree(dir: 'libs', include: ['*.jar'])    compile 'com.android.support:appcompat-v7:21.0.3'}

5. The final build file should be like this.

buildscript {    repositories {        mavenCentral()    }    dependencies {        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2+'    }}apply plugin: 'com.android.application'apply plugin: 'android-apt'android {    compileSdkVersion 21    buildToolsVersion 21.1.2    defaultConfig {        applicationId com.tanglikang.annotationtest        minSdkVersion 9        targetSdkVersion 21        versionCode 1        versionName 1.0    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }}apt {    arguments {        androidManifestFile variant.outputs[0].processResources.manifestFile        resourcePackageName com.tanglikang.annotationtest    }}dependencies {    apt org.androidannotations:androidannotations:3.0+          // add these    compile org.androidannotations:androidannotations-api:3.0+  // two lines    compile fileTree(dir: 'libs', include: ['*.jar'])    compile 'com.android.support:appcompat-v7:21.0.3'}

6. re-build the project. The system automatically downloads the dependent third-party libraries. Then you can use AndroidAnnotation.

Use

The use of AndroidAnnotation is actually very simple. You need to pay attention to two points.
First, use tags. There are many useful tags in AA. For details, refer to the following webpage: https://github.com/excilys/androidannotations/wiki/availableannotations.
Second, during project compilation, AA will replace the original Activity with Activity _. Therefore, you need to modify it in AndroidMainfest.
Here is a simple example. Three labels @ EActivity are used to set the layout file. @ ViewById introduces a control. @ Click: Set the space Click event.

@EActivity(R.layout.activity_main)public class MainActivity extends ActionBarActivity {    @ViewById(R.id.tv_1)    TextView hello;    @Click(R.id.tv_1)    public void showToast(){        Toast.makeText(MainActivity.this, ok, Toast.LENGTH_LONG).show();    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);    }}

  
                          
                                   
                
    
               
  
Summary

After practice, AndroidAnnotation can greatly increase the android programming efficiency and is worth learning and using.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.