AndroidAnnotation配置与使用

背景

随着安卓手机在市场上的普及,安卓开发的需求也越来越大。但是在安卓开发过程中,其实很多操作都是较为繁琐,且没有必要的,虽然一天下来看看写的代码很多,但是可能也就一个界面,几个功能点。所以对于安卓开发中代码的简化和重用是十分有必要的。
昨天偶然在网易云课堂上看到了AndroidAnnotation这个第三方框架,据说能够大幅度简化安卓编程,提高编程效率。怀着一颗好东西都要尝试一下的心态,今天将之配置并尝试了一下。

配置

AndroidAnnotation的官网为http://androidannotations.org/。但是事实上,在这个网站上只有一个例子代码展示它能够如何如何简化代码,实际的内容主要还是在github上。
通过连接打开git,进入它的git wiki。在这里可以下载jar包,并由一些如何使用的guide。包括如何在Eclipse和IntelliJ上配置。不过在这里它讲的比较简单,如果按照它的方法配置,可能会遇到很多问题。另外bz用的时AndroidStudio,虽然是基于IntelliJ,但是不知为何在细节上还是有些差距的,比如AndroidStudio上就没有Annotation Processing这个设置项。所以我们需要找寻一种适用于AndroidStudio且更为简洁的方式。
得益于Gradle,我们可以直接使用build文件引入AndroidAnnotation。这里需要五步。

1.引入对android-apt的依赖。在app module的build文件中添加以下代码。

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2+'
    }
}

apply plugin: 'android-apt'  //添加android-apt插件

2.设置android-apt参数 。注意把包名换成你的应用的。另外outputs[0]是在新的android-studio的版本中才需要加的。

apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        resourcePackageName "你的包名"
    }
}

3.使用apt引入对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.最后的build文件应该是这样的。

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.重新build工程,系统会自动下载依赖的第三方库。然后就可以使用AndroidAnnotation了。

使用

AndroidAnnotation的使用其实很简单,主要需要注意两个点。
一是标签的使用。AA中有很多有用的标签,具体可以参考以下网页:https://github.com/excilys/androidannotations/wiki/AvailableAnnotations
二是在编译工程的时候,AA会将原来的Activity换成Activity_的形式,所以需要在AndroidMainfest中也做相应地修改。
这里尝试一个最简单地例子。这里使用了三个标签@EActivity设置布局文件。@ViewById引入一个控件。@Click设置空间点击事件。

@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);
    }

}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tanglikang.annotationtest" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity_"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

总结

经过实践,AndroidAnnotation确实能够大幅度增加android编程效率,值得学习和使用。

参考文献:
http://www.jayway.com/2014/02/21/androidannotations-setup-in-android-studio/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值