博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AAC架构系列一(初识)
阅读量:7077 次
发布时间:2019-06-28

本文共 4264 字,大约阅读时间需要 14 分钟。

1.Android Architecture Components是什么?

Android体系结构组件是一组库,可帮助您设计健壮,可测试和可维护的应用程序。从用于管理UI组件生命周期和处理数据持久性的类开始。

  • 是一个支持库,允许您使用声明性格式而不是以编程方式将布局中的UI组件绑定到应用程序中的数据源。
  • 轻松管理应用程序的生命周期。新的可帮助您管理活动和碎片生命周期。生存配置更改,避免内存泄漏并轻松将数据加载到UI中。
  • 使用构建数据对象,以便在基础数据库更改时通知视图。
  • 存储在应用程序轮换中未销毁的UI相关数据。
  • 是一个SQLite对象映射库。使用它来避免样板代码并轻松地将SQLite表数据转换为Java对象。Room提供SQLite语句的编译时检查,可以返回RxJava,Flowable和LiveData observable。
  • 是指允许用户在应用内的不同内容中导航,导入和退出的交互。
  • y可帮助您一次加载和显示小块数据。按需加载部分数据可减少网络带宽和系统资源的使用。
  • 可以轻松安排即使应用程序退出或设备重新启动也可以运行的可延迟的异步任务。

Google I/O 大会上也对新的组件架构有一些介绍,详细可以看这个。

2.向项目添加组件

上面对这些组件进行了一些简单得介绍,接下来自然是要如何向我们的项目中引入这些组件了。这些都是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"  }复制代码

3.本节总结

本节主要对本系列中会介绍到的组件进行了一些初步的了解,我也是在一边的学习一边的进行记录,Architecture Components是JetPack中的一部分,也是非常重要的一部分,Google这两年一直在对这一套的架构进行不断的更新,就是想要解决Android开发中的很多痛点,以后Architecture Components作为Android开发的基础架构也是必然的趋势,所以就让我们现在一起来学习它吧。

转载地址:http://wxjml.baihongyu.com/

你可能感兴趣的文章
libssh2进行远程运行LINUX命令
查看>>
Android Gson深入分析
查看>>
Android中自动跳转到系统设置界面
查看>>
树后台数据存储(採用webmethod)
查看>>
Android利用Fiddler进行网络数据抓包【怎么跟踪微信请求】
查看>>
memcached系列之二
查看>>
树的左旋与右旋
查看>>
Atitit. 如何判断软件工程师 能力模型 程序员能力模型 项目经理能力模型...
查看>>
每周算法讲堂,二分法
查看>>
2016第8周五
查看>>
CSS3文本溢出显示省略号
查看>>
zookeeper系列之通信模型(转)
查看>>
js动态判断密码强度&&实用的 jQuery 代码片段
查看>>
.Net开源SqlServer ORM框架SqlSugar整理
查看>>
JQuery在循环中绑定事件的问题详解
查看>>
SOCKS 5协议详解(转)
查看>>
用Inno Setup来解决.NetFramework安装问题 (转载)
查看>>
使用axis调用WebService服务端
查看>>
Linux下通过受限bash创建指定权限的账号
查看>>
php:使用XHProf查找PHP性能瓶颈
查看>>