[译] Architecture Components 之 Adding Components to your Project

1,913 阅读1分钟

【目录】

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"

有关更多信息,请参阅添加构建依赖