Background
With the popularity of Android phones in the market, the need for Android development is growing. But in the Android development process, in fact, a lot of operations are more cumbersome, and there is no need, although a day down to see write code a lot, but may also be an interface, a few functional points. Therefore, it is necessary to simplify and reuse code in Android development.
Yesterday in the NetEase Cloud class saw androidannotation this third-party framework, is said to greatly simplify the Android programming, improve programming efficiency. With a good thing to try the mentality, today to configure and try it.
Configuration
Androidannotation's official website is http://androidannotations.org/. But in fact, there is only one example code on this site that shows how it can simplify the code, and the actual content is mostly on GitHub.
Open git via a connection and go to its git wiki. Here you can download the jar package, and by some how to use the guide. Includes how to configure on Eclipse and IntelliJ. However, it is easier to talk about here, and if you follow its approach, you may encounter many problems. In addition BZ with the time Androidstudio, although is based on IntelliJ, but I do not know why in detail there are some gaps, such as androidstudio on the annotation processing this setting. So we need to find a way to be androidstudio and more concise.
Thanks to Gradle, we can introduce androidannotation directly using the build file. It takes five steps.
1. The introduction of dependency on ANDROID-APT. Add the following code to the app module's build file.
buildscript { repositories { mavenCentral() } dependencies { ‘com.neenbedankt.gradle.plugins:android-apt:1.2+‘ ‘android-apt‘ //添加android-apt插件
2. Set the ANDROID-APT parameter. Pay attention to replacing the package name with your application. In addition Outputs[0] is in the new Android-studio version of the need to add.
apt { arguments { androidManifestFile variant.outputs[0].processResources.manifestFile "你的包名" }}
3. Use apt to introduce a dependency on androidannotation.
dependencies { "org.androidannotations:androidannotations:3.0+" "org.androidannotations:androidannotations-api:3.0+" compile fileTree(dir‘libs‘, include: [‘*.jar‘]) ‘com.android.support:appcompat-v7:21.0.3‘}
5. The final build file should look 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 +Buildtoolsversion"21.1.2"Defaultconfig {ApplicationID"Com.tanglikang.annotationtest"Minsdkversion9Targetsdkversion +Versioncode1Versionname"1.0"} buildtypes {release {minifyenabledfalseProguardfiles 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 theseCompile"org.androidannotations:androidannotations-api:3.0+" //LinesCompile Filetree (dir:' Libs ', include: [' *.jar ']) Compile' com.android.support:appcompat-v7:21.0.3 '}
6. Rebuild the project, the system will automatically download the dependent third-party library. Then you can use the androidannotation.
Use
The use of androidannotation is actually very simple, the main need to pay attention to two points.
One is the use of labels. AA has a lot of useful tags, specifically, you can refer to the following Web page: Https://github.com/excilys/androidannotations/wiki/AvailableAnnotations.
Second, when compiling the project, AA will change the original activity into activity_ form, so it needs to be modified in androidmainfest.
Try one of the simplest examples here. The layout file is set up using three tabs @eactivity. @ViewById introduce 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); }}
<?xml version= "1.0" encoding= "Utf-8"?><manifest xmlns:android="Http://schemas.android.com/apk/res/android" package ="Com.tanglikang.annotationtest" > <applicationandroid:allowbackup="true"android:icon="@mipmap/ Ic_launcher "android:label=" @string/app_name "android:theme=" @style/ Apptheme " > <activityandroid: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>
Summarize
In practice, androidannotation really can significantly increase the efficiency of Android programming, it is worth learning and use.
Reference documents:
http://www.jayway.com/2014/02/21/androidannotations-setup-in-android-studio/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Androidannotation Configuration and use