Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Studio 配置 AndroidAnnotations 完整踩坑記錄

編輯:關於 Android 程式設計

AndroidAnnotations 是一個基於編譯期生成代碼的注解框架,能用 @EActivity、@ViewById、@Click 等注解省掉大量 findViewById 與 setOnClickListener 的樣板代碼。官方 GitHub Wiki 上有 Eclipse 與 IntelliJ 的配置說明,但 Android Studio 的環節最容易出問題,網上資料又版本各異。這裡把可跑通的配置過程完整記錄下來。

參考地址:https://github.com/excilys/androidannotations/wiki/Configuration

第一步:模塊級 build.gradle

核心是引入 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"
    }
}

第二步:項目級 build.gradle

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 的代碼生成鏈路就打通了。

第三步:修改 AndroidManifest.xml

這是新手最容易漏掉的一步。使用 @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>

第四步:在 Activity 中使用注解

@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("你好,上海!");
    }
}

常見問題排查

  • 找不到 MainActivity_ 這個類。基本是 apt 依賴沒寫對,或者沒有 Rebuild Project。可以查看 build/generated/source/apt/ 目錄確認生成文件是否存在。
  • 注解不生效、 findViewById 全部為 null。檢查清單文件是否已改為帶下劃線的類名。
  • AS 版本較高時 android-apt 報錯。新版本 Android Gradle Plugin 已內建 annotationProcessor 支持,可改用 annotationProcessor org.androidannotations:androidannotations:$AAVersion 並移除 apt 插件。
  • resourcePackageName 警告。當 applicationId 與 Java 包名不同時,需要在 apt 的 arguments 中顯式指定資源包名。
  • @AfterViews 裡控件為空。把初始化邏輯寫在 onCreate 而不是 @AfterViews/@AfterInject 中,就會遇到這個問題。

整體而言,AndroidAnnotations 的配置成本主要花在「版本匹配」上:Gradle 插件版本、buildToolsVersion、AA 版本三者要相互兼容。配置成功之後,代碼量的減少相當可觀。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved