将Android组件库添加到项目Adding Components to your Project

本文介绍如何将Google的架构组件添加到Android Studio项目中,包括添加Maven仓库、依赖项配置等步骤,并提供了针对不同需求如Kotlin支持、Java 8支持、RxJava集成的配置建议。

Adding Components to your Project


Architecture Components are available from Google's Maven repository. To use them, follow these steps:

Add the Google Maven repository

Android Studio projects aren't configured to access this repository by default.

To add it to your project, open the build.gradle file for your project (not the ones for your app or module) and add the highlighted line as shown below:

allprojects {
    repositories {
        jcenter()
        google()
    }
}
Add Architecture Components

Open the build.gradle file for your app or module and add the artifacts that you need as dependencies. You can add all dependencies, or choose a subset.

Note: For Kotlin based apps, make sure you use kapt instead of annotationProcessor. You should also add the kotlin-kapt plugin.

Main Dependencies

Includes LifecyclesLiveDataViewModelRoom, and Paging.

It also includes test helpers for testing LiveData as well as testing Room migrations.

dependencies {
    // ViewModel and LiveData
    implementation "android.arch.lifecycle:extensions:1.1.1"
    // alternatively, just ViewModel
    implementation "android.arch.lifecycle:viewmodel:1.1.1"
    // alternatively, just LiveData
    implementation "android.arch.lifecycle:livedata:1.1.1"

    annotationProcessor "android.arch.lifecycle:compiler:1.1.1"

    // Room (use 1.1.0-beta3 for latest beta)
    implementation "android.arch.persistence.room:runtime:1.0.0"
    annotationProcessor "android.arch.persistence.room:compiler:1.0.0"

    // Paging
    implementation "android.arch.paging:runtime:1.0.0-rc1"

    // Test helpers for LiveData
    testImplementation "android.arch.core:core-testing:1.1.1"

    // Test helpers for Room
    testImplementation "android.arch.persistence.room:testing:1.0.0"
}
Java 8 Support for Lifecycles

If your app uses Java 8, we recommend using this library instead of android.arch.lifecycle:compiler.

dependencies {
    // Java8 support for Lifecycles
    implementation "android.arch.lifecycle:common-java8:1.1.1"
}
Optional Dependencies for RxJava and ReactiveStreams

Adds optional support for Room RxJava and LiveData ReactiveStreams.

dependencies {
    // RxJava support for Room (use 1.1.0-beta3 for latest beta)
    implementation "android.arch.persistence.room:rxjava2:1.0.0"

    // ReactiveStreams support for LiveData
    implementation "android.arch.lifecycle:reactivestreams:1.1.1"

    // RxJava support for Paging
    implementation "android.arch.paging:rxjava2:1.0.0-alpha1"
}
Optional Dependencies for Guava

Adds support for Guava's Optional and ListenableFuture types in Room @Dao queries.

dependencies {
    // Guava support for Room
    implementation "android.arch.persistence.room:guava:1.1.0-beta3"
}
Alternative import for lightweight Lifecycles only

Support library depends on this lightweight import. It provides Lifecycles without LiveData or ViewModel.

dependencies {
    // Lifecycles only (no ViewModel or LiveData)
    implementation "android.arch.lifecycle:runtime:1.1.1"
    annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
}

For more information, see Add Build Dependencies.

当Maven项目出现 'error adding module to project:null' 报错时,可以尝试以下几种解决办法: ### 检查项目配置文件 - **检查 `pom.xml` 文件**:确保 `pom.xml` 文件格式正确,无语法错误。特别是 `<modules>` 标签及其子标签的配置是否正确。例如,以下是一个简单的 `pom.xml` 中 `modules` 配置示例: ```xml <project> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>parent-project</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>module1</module> <module>module2</module> </modules> </project> ``` 要保证子模块的名称与实际目录名称一致。 ### 清理项目缓存 - **清除IDE缓存**:不同的IDE清理缓存的方式不同。以IntelliJ IDEA为例,可以通过以下步骤清理缓存: 1. 关闭IntelliJ IDEA。 2. 找到IntelliJ IDEA的缓存目录,一般在系统用户目录下的 `.IntelliJIdeaXX/system` (XX为版本号)。 3. 删除该目录下的 `caches` 文件夹。 4. 重新打开IntelliJ IDEA并重新加载项目。 ### 检查Maven设置 - **更新Maven配置**:确保Maven的 `settings.xml` 文件配置正确,特别是镜像、仓库路径等配置。可以尝试更新Maven的本地仓库,删除本地仓库中可能损坏的文件,然后重新构建项目。例如,在命令行中执行以下命令: ```bash mvn clean install ``` ### 检查文件系统权限 - **确保文件和目录可读写**:检查项目文件和目录的权限,确保IDE和Maven有足够的权限对其进行操作。特别是在Linux或macOS系统中,要保证当前用户对项目目录有读写权限。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值