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

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值