Android butterknife 實現原理

來源:互聯網
上載者:User

標籤:

簡介

ButterKnife 是一個 Android 系統的 View 注入架構,能夠通過『註解』的方式來綁定 View 的屬性或方法。

比如使用它能夠減少 findViewById() 的書寫,使代碼更為簡潔明了,同時不消耗額外的效能。

當然這樣也有個缺點,就是可讀性會差一些,好在 ButterKnife 比較簡單,學習難度也不大。

添加依賴

這裡以 Android Studio Gradle 為例,為項目添加 ButterKnife,注意兩個步驟都要完成:

1. Project 的 build.gradle 添加:
dependencies {  classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8‘} 
2. App 的 build.gradle 添加:
applyplugin: ‘com.neenbedankt.android-apt‘ dependencies {  compile ‘com.jakewharton:butterknife:8.0.1‘  apt ‘com.jakewharton:butterknife-compiler:8.0.1‘} 

這裡順便提一個名為 Android Butterknife Zelezny 的外掛程式,可在 Settings – plugins 中下載,提供一鍵產生註解的功能,這裡不多贅述,可以看以下文章:

ButterKnife 註解架構的偷懶外掛程式

ButterKnife 的使用

ButterKnife 的使用是比較簡單的,具體的使用可以參考官方文檔或者搜尋其他博文資料,在這裡只簡單介紹一下它的用途:

  1. 通過使用 @BindView 來消除 findViewById
  2. 將多個 View 組織到一個列表中,一次性操作它們
  3. 通過使用 @onClick 為 View 綁定監聽,消除 listener 的匿名內部類
  4. 通過使用資源註解如 @BindColor,來消除資源的尋找

我們看到呢,它好像消除了一堆東西,其實就是簡化的意思啦。

使用方式整合

總的來說,ButterKnife 有以下使用:

  1. View 綁定
  2. Resource 綁定
  3. 非 Activity 綁定
  4. View List 綁定
  5. Listener 綁定

具體的使用,參照文檔吧!

Butter Knife

原理簡單分析Java Annotation Processing

註解處理器,在 Java5 中叫 APT,有沒有很眼熟:

1 dependencies {2   classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8‘3 }4  5 // 引入 ButterKnifeProcessor 等6 apt ‘com.jakewharton:butterknife-compiler:8.0.1‘7  

這是一個用於編譯時間掃描和解析 Java 註解的工具,通過它我們可以自己定義註解,並定義解析器來處理它們。它的原理是讀入 Java 原始碼,解析註解,然後產生新的 Java 代碼,新產生的程式碼最後被編譯成 Java 位元組碼。

當然這裡我們不是要深究 APT 的原理,而是要知道在 ButterKnife 中運用到了這個工具。

ButterKnife 流程

這裡以一個栗子來一步步觀察,ButterKnife 的工作流程。

在此之前看一眼 @BindView 註解的定義:

@Retention(CLASS) @Target(FIELD)public @interface BindView {  /** View ID to which the field will be bound. */  @IdResint value();}

重點在於 @Retention(CLASS),它表示該註解在編譯時間被保留,但在運行時 JVM 會忽略它。因此使用 ButterKnife 的註解,不會對運行時的效能造成消耗。

掃描 ButterKnife 註解

首先我們使用註解來聲明我們的一個 View:

@BindView(R.id.text1) TextViewtext1;

於是在我們編譯的時候,ButterKnifeProcessor 類的 process() 方法便會執行,搜尋到所有的 ButterKnife 註解(@BindView),然後產生一個 Java 類。

根據註解,產生 Java 類

我們在 app\build\generated\source\apt\ 中找到產生的 MainActivity$$ViewBinder 檔案,該類中包含了一個 bind 方法:

public Unbinderbind(final Finderfinder, final T target, Object source) {    InnerUnbinderunbinder = createUnbinder(target);    Viewview;    view = finder.findRequiredView(source, 2131492944, "field ‘text1‘");    target.text1 = finder.castView(view, 2131492944, "field ‘text1‘");    return unbinder;  } 

到這裡就越來越像我們手寫 findViewById() 了。

動態注入

最後當我們執行 ButterKnife.bind(this) 時,ButterKnife 會載入上面產生的類,然後調用其 bind 方法。

  • 這裡首先調用了 findRequiredView 去尋找 R.id.text1 所對應的控制項,其實相當於我們的 findViewById()
  • 其次調用 castView,相當於類型轉換,把找到的 View 轉化為 TextView 類型

至此,我們就完成了一次 ButterKnife 的工作流程。

總結

實際上我們發現,ButterKnife 的原理還是相對簡單的,因為其主要痛點在於 APT 的使用,那屬於其他方面的內容,在此我們並不深究。

在這個架構中我們看到了『反射』和『Annotation Processing』間的取捨,ButterKnife 使用了後者,避免了額外效能的消耗。

Android butterknife 實現原理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.