編輯:關於 Android 程式設計
AndroidAnnotations 是一個基於編譯期生成代碼的注解框架,能用 @EActivity、@ViewById、@Click 等注解省掉大量 findViewById 與 setOnClickListener 的樣板代碼。官方 GitHub Wiki 上有 Eclipse 與 IntelliJ 的配置說明,但 Android Studio 的環節最容易出問題,網上資料又版本各異。這裡把可跑通的配置過程完整記錄下來。
參考地址:https://github.com/excilys/androidannotations/wiki/Configuration
核心是引入 android-apt 插件,並把 androidannotations 分為「注解處理器(apt)」和「運行時庫(compile)」兩個依賴。
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
def AAVersion = '3.3.2'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "lzl.edu.com.firstannitation"
minSdkVersion 22
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
apt org.androidannotations:androidannotations:$AAVersion
compile org.androidannotations:androidannotations-api:$AAVersion
}
apt {
arguments {
androidManifestFile variant.outputs[0]
.processResources.manifestFile
// 若使用了 splits 產生多個產物,需要把索引 0 換成對應的序號
// 若 applicationId 與包名不一致,需在此處指定:
// resourcePackageName "lzl.edu.com.firstannitation"
}
}
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
// 注意:模塊依賴不要寫在這裡
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
同步之後,AndroidAnnotations 的代碼生成鏈路就打通了。
這是新手最容易漏掉的一步。使用 @EActivity 之後,框架實際生成的 Activity 類名帶有下劃線後綴,因此清單文件裡必須指向生成後的類:
<!-- 修改前 --> android:name="lzl.edu.com.firstannitation.MainActivity" <!-- 修改後 --> android:name="lzl.edu.com.firstannitation.MainActivity_"
完整清單示例:
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="lzl.edu.com.firstannitation">
<application>
<activity android:name=".MainActivity_">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {
@ViewById(R.id.textview1)
TextView textView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
// 所有 View 注入完成後的回調,此時控件已可安全使用
@AfterViews
void afterViews() {
textView1.setText("你好,上海!");
}
}
build/generated/source/apt/ 目錄確認生成文件是否存在。annotationProcessor 支持,可改用 annotationProcessor org.androidannotations:androidannotations:$AAVersion 並移除 apt 插件。onCreate 而不是 @AfterViews/@AfterInject 中,就會遇到這個問題。整體而言,AndroidAnnotations 的配置成本主要花在「版本匹配」上:Gradle 插件版本、buildToolsVersion、AA 版本三者要相互兼容。配置成功之後,代碼量的減少相當可觀。
Android 沉浸式體驗實戰:全屏加透明狀態欄的實現
前言從 Android 4.4 開始,系統提供了沉浸式全屏能力:狀態欄與虛擬按鍵可以動態隱藏,應用得以佔用完整的屏幕空間,按 Google 的說法,這能給用戶一種「身臨其
Android 記憶體洩漏的簡單檢查與分析方法
導語 記憶體洩漏大概是 Android 開發者最頭疼的問題之一。本文整理一下檢查與分析記憶體洩漏常用的一些工具與方法。 一、什麼是記憶體洩漏? Java 有垃圾回收機制,
SlidingTabLayout 實作教學:為 ViewPager 加上可滑動的頁卡標題
SlidingTabLayout 是一個非常經典的開源元件,能讓 ViewPager 上方出現一排可跟隨滾動、帶有指示線的頁卡標題。它的原始碼出自 Android Ope
開發隨筆:時間控件、資料庫查詢與字串處理的實戰心得
開發隨筆,小結專案開發中的得與失,重點如下: 進展——推薦功能及相關頁面: 1. 相關控件的使用:TimePicker、RadioGroup、RadioButton、B