Android体系结构组件是一组库,可帮助您设计健壮,可测试和可维护的应用程序。从用于管理UI组件生命周期和处理数据持久性的类开始。
Google I/O 大会上也对新的组件架构有一些介绍,详细可以看这个链接。
上面对这些组件进行了一些简单得介绍,接下来自然是要如何向我们的项目中引入这些组件了。这些都是Google官方推出的组件,当然首先需要在你的project的build.gradle中文件中引入google()啦,向下面这样就好:
allprojects { repositories { google() jcenter() } } 复制代码
关于LifeCycles与ViewModel以及LiveData的引入,只需要根据自己的需求在下面进行选择就好:
dependencies { def lifecycle_version = "2.0.0" // ViewModel and LiveData implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version" // alternatively - just ViewModel implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version" // For Kotlin use lifecycle-viewmodel-ktx // alternatively - just LiveData implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version" // alternatively - Lifecycles only (no ViewModel or LiveData). Some UI // AndroidX libraries use this lightweight import for Lifecycle implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version" annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" // For Kotlin use kapt instead of annotationProcessor // alternately - if using Java8, use the following instead of lifecycle-compiler implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version" // optional - ReactiveStreams support for LiveData implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version" // For Kotlin use lifecycle-reactivestreams-ktx // optional - Test helpers for LiveData testImplementation "androidx.arch.core:core-testing:$lifecycle_version" } 复制代码
dataBinding的使用需要在app的build.gradle中加入下面的配置:
android { ... dataBinding { enabled = true } } 复制代码
Navigation需要的声明依赖:
dependencies { def nav_version = "2.1.0-alpha02" implementation "androidx.navigation:navigation-fragment:$nav_version" // For Kotlin use navigation-fragment-ktx implementation "androidx.navigation:navigation-ui:$nav_version" // For Kotlin use navigation-ui-ktx } 复制代码
Paging需要的声明依赖:
dependencies { def paging_version = "2.1.0" implementation "androidx.paging:paging-runtime:$paging_version" // For Kotlin use paging-runtime-ktx // alternatively - without Android dependencies for testing testImplementation "androidx.paging:paging-common:$paging_version" // For Kotlin use paging-common-ktx // optional - RxJava support implementation "androidx.paging:paging-rxjava2:$paging_version" // For Kotlin use paging-rxjava2-ktx } 复制代码
Room需要的声明依赖:
dependencies { def room_version = "2.1.0-alpha06" implementation "androidx.room:room-runtime:$room_version" annotationProcessor "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor // optional - Kotlin Extensions and Coroutines support for Room implementation "androidx.room:room-ktx:$room_version" // optional - RxJava support for Room implementation "androidx.room:room-rxjava2:$room_version" // optional - Guava support for Room, including Optional and ListenableFuture implementation "androidx.room:room-guava:$room_version" // Test helpers testImplementation "androidx.room:room-testing:$room_version" } 复制代码
WorkManager需要的声明依赖:
dependencies { def work_version = "2.0.1" // (Java only) implementation "androidx.work:work-runtime:$work_version" // Kotlin + coroutines implementation "androidx.work:work-runtime-ktx:$work_version" // optional - RxJava2 support implementation "androidx.work:work-rxjava2:$work_version" // optional - Test helpers androidTestImplementation "androidx.work:work-testing:$work_version" } 复制代码
本节主要对本系列中会介绍到的组件进行了一些初步的了解,我也是在一边的学习一边的进行记录,Architecture Components是JetPack中的一部分,也是非常重要的一部分,Google这两年一直在对这一套的架构进行不断的更新,就是想要解决Android开发中的很多痛点,以后Architecture Components作为Android开发的基础架构也是必然的趋势,所以就让我们现在一起来学习它吧。