[譯] Architecture Components 之 Adding Components to your Project

zly394發表於2017-06-08

【目錄】

1. Architecture Components 之 Guide to App Architecture

2. Architecture Components 之 Adding Components to your Project

3. Architecture Components 之 Handling Lifecycles

4. Architecture Components 之 LiveData

5. Architecture Components 之 ViewModel

6. Architecture Components 之 Room Persistence Library

示例程式碼連結


新增元件到專案中

警告:Architecture Components 還是 alpha 版本,在 1.0 版本釋出之前可能有重大更改。

Architecture Components 可以從 Google 的 Maven 倉庫獲取。要使用它們,請按照下列步驟操作:

新增 Google Maven 倉庫

預設情況下 Android Studio 沒有配置訪問此庫。

要將該庫新增到專案中,請開啟專案中的 build.gradle 檔案(不是應用或模組下的)並且新增如下所示的高亮的行。

allprojects {
    repositories
{
        jcenter
()
       
maven { url 'https://maven.google.com' }
   
}
}

新增 Architecture Components

開啟應用或模組中的 build.gradle 檔案並將所需的元件新增為依賴:

  • 使用 LifecyclesLiveDataViewModel 需要新增:

    • compile "android.arch.lifecycle:runtime:1.0.0-alpha1"

    • compile "android.arch.lifecycle:extensions:1.0.0-alpha1"

    • annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha1"

  • 使用 Room 需要新增:

    • compile "android.arch.persistence.room:runtime:1.0.0-alpha1"

    • annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha1"

    • 使用測試 Room 遷移需要新增:

      • testCompile "android.arch.persistence.room:testing:1.0.0-alpha1"
    • 使用 Room 的 RxJava 支援需要新增:

      • compile "android.arch.persistence.room:rxjava2:1.0.0-alpha1"

有關更多資訊,請參閱新增構建依賴

相關文章