Android xxxxx- KTX 库

Android xxxxx-KTX 是包含在 Android Jetpack 及其他 Android 库中的一组 Kotlin 扩展程序。KTX 扩展程序可以为 Jetpack、Android 平台及其他 API 提供简洁的惯用 Kotlin 代码。为此,这些扩展程序利用了多种 Kotlin 语言功能,其中包括:

  • 扩展函数
  • 扩展属性
  • Lambda
  • 命名参数
  • 参数默认值
  • 协程

AndroidX 模块

Android KTX 分为若干模块,每个模块包含一个或多个软件包。
Android KTX 包含[核心模块 core](androidx.core.app),该模块可为通用框架 API 提供 Kotlin 扩展程序,而且还能提供一些领域专用的扩展程序。

除了核心模块之外,所有 KTX 模块工件都会替换 build.gradle 文件中的底层 Java 依赖项。例如,您可以将 androidx.fragment:fragment 依赖项替换为 androidx.fragment:fragment-ktx。此语法有助于更好地管理版本控制,而不会增加额外的依赖项声明要求。

1、Core KTX

Core KTX 模块为属于 Android 框架的通用库提供扩展程序。这些库没有您需要添加到 build.gradle 的基于 Java 的依赖项。

要使用此模块,请将以下内容添加到应用的 build.gradle 文件中:

dependencies {
   implementation "androidx.core:core-ktx:1.3.1"
}

2、Collection KTX

Collection 扩展程序包含在 Android 的节省内存的集合库中使用的效用函数,包括 ArrayMap、LongParseArray、LruCache 等等。

要使用此模块,请将以下内容添加到应用的 build.gradle 文件中:

    dependencies {
        implementation "androidx.collection:collection-ktx:1.1.0"
    }

3、Fragment KTX

Fragment KTX 模块提供了一系列扩展程序以简化 Fragment API。

要使用此模块,请将以下内容添加到应用的 build.gradle 文件中:

dependencies {
    implementation "androidx.fragment:fragment-ktx:1.2.5"
}

4、 Lifecycle KTX

Lifecycle KTX 为每个 Lifecycle 对象定义一个 LifecycleScope。在此范围内启动的协程会在 Lifecycle 被销毁时取消。您可以使用 lifecycle.coroutineScopelifecycleOwner.lifecycleScope 属性访问 LifecycleCoroutineScope

要使用此模块,请将以下内容添加到应用的 build.gradle 文件中:

dependencies {
        implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0"
    }

5、LiveData KTX

使用 LiveData 时,您可能需要异步计算值。例如,您可能需要检索用户的偏好设置并将其传送给界面。在这些情况下,LiveData KTX 可提供一个 liveData 构建器函数,该函数会调用 suspend 函数,并将结果作为 LiveData 对象传送。

要使用此模块,请将以下内容添加到应用的 build.gradle 文件中:

dependencies {
        implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"
    }

6、Navigation KTX

Navigation 库的每个组件都有自己的 KTX 版本,用于调整 API 以使其更简洁且更符合 Kotlin 的语言习惯。

要添加这些模块,请将以下内容添加到应用的 build.gradle 文件中:

dependencies {
    implementation "androidx.navigation:navigation-runtime-ktx:2.3.0"
    implementation "androidx.navigation:navigation-fragment-ktx:2.3.0"
    implementation "androidx.navigation:navigation-ui-ktx:2.3.0"
}

7、Palette KTX

Palette KTX 模块为使用调色板提供惯用的 Kotlin 支持。

要使用此模块,请将以下内容添加到应用的 build.gradle 文件中:

dependencies {
        implementation "androidx.palette:palette-ktx:1.0.0"
    }

8、Reactive Streams KTX

利用 Reactive Streams KTX 模块可根据 ReactiveStreams 发布程序来创建可监测的 LiveData 流。

要使用此模块,请将以下内容添加到应用的 build.gradle 文件中:

dependencies {
        implementation "androidx.lifecycle:lifecycle-reactivestreams-ktx:2.2.0"
    }

9、Room KTX

Room 扩展程序增加了对数据库事务的协程支持。
要使用此模块,请将以下内容添加到应用的 build.gradle 文件中:

dependencies {
        implementation "androidx.room:room-ktx:2.2.5"
    }

10、SQLite KTX

SQLite 扩展程序将与 SQL 相关的代码封装在事务中,从而避免编写大量样板代码。

要使用此模块,请将以下内容添加到应用的 build.gradle 文件中:

dependencies {
        implementation "androidx.sqlite:sqlite-ktx:2.1.0"
    }

11、 ViewModel KTX

ViewModel KTX 库提供了一个 viewModelScope() 函数,可让您更轻松地从 ViewModel 启动协程。CoroutineScope 绑定至 Dispatchers.Main,并且会在清除 ViewModel 后自动取消。您可以使用 viewModelScope(),而无需为每个 ViewModel 创建一个新范围。

要使用此模块,请将以下内容添加到应用的 build.gradle 文件中:

dependencies {
        implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
    }

12、WorkManager KTX

WorkManager KTX 为协程提供一流的支持。

要使用此模块,请将以下内容添加到应用的 build.gradle 文件中:

dependencies {
    implementation "androidx.work:work-runtime-ktx:2.4.0"
}

13、Play Core KTX

Play Core KTX 通过向 Play Core 库中的 SplitInstallManager 和 AppUpdateManager 添加扩展函数,针对单发请求和用于监控状态更新的 Flow 添加了对 Kotlin 协程的支持。

要使用此模块,请将以下内容添加到应用的 build.gradle 文件中:

dependencies {
    implementation "com.google.android.play:core-ktx:1.8.0"
}

14、SavedStateHandle的使用

build.gradle文件中配置

android {
        dataBinding.enabled = true
        ...
        }
        dependencies {
        implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:2.2.0'
}

15、Room持久性库的使用

build.gradle文件中配置

    dependencies {
      def room_version = "2.2.5"

      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"
    }

16、添加Navigation支持

build.gradle添加以下依赖项:

dependencies {
  def nav_version = "2.3.0"

  // Java language implementation
  implementation "androidx.navigation:navigation-fragment:$nav_version"
  implementation "androidx.navigation:navigation-ui:$nav_version"

  // Kotlin
  implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
  implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

  // Feature module Support
  implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"

  // Testing Navigation
  androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
}

17、添加分页组件

build.gradle添加以下依赖项:

dependencies {
      def paging_version = "2.1.2"

      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
    }

18、协程添加依赖

build.gradle添加以下依赖项:

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'

19、RecyclerView的依赖项

如需添加RecyclerView的依赖项,您必须将Google Maven代码库添加到项目中。
在应用或模块的build.gradle文件中添加所需工件的依赖项:

dependencies {
        implementation "androidx.recyclerview:recyclerview:1.1.0"
        // For control over item selection of both touch and mouse driven selection
        implementation "androidx.recyclerview:recyclerview-selection:1.1.0-rc01"
    }
20、Coil 的依赖项

Coil:官方推荐的Kotlin图片加载库。 在 mavenCentral()进行下载
在应用或模块的build.gradle文件中添加所需工件的依赖项:

dependencies {
        implementation "io.coil-kt:coil:1.2.1"
    }
Coil 名字的由来:取 Coroutine Image Loader 首字母得来,可以看出通过 Kotlin 协程来进行图片加载,特点如下:
  • 更快:Coil 在性能上有很多优化,包括内存缓存和磁盘缓存、把缩略图保存在内存中、通过 BitmapPool 循环利用 Bitmap、自动暂停和取消网络请求等
  • 更轻量级:Coil 只有 2000 个方法,跟 Picasso 的方法数差不多,相比 Glide 和 Fresco 要轻量非常多
  • 更容易使用:Coil 的 API 充分利用 Kotlin 的新特性,而且还有丰富的拓展函数,简化和减少了很多样板代码
  • 更流行:Coil 通过 Kotlin 来开发,并且使用包含 Coroutines、okhttp、okio 和 AndroidX Lifecycles 在内的非常多流行的开源库
21、apk分包的依赖项

如需添加multidex的依赖项。
在应用或模块的build.gradle文件中添加所需工件的依赖项:

dependencies {
        implementation "androidx.multidex:multidex:1.1.0"
    }
22、 设置界面的preference依赖项

当我们点击一个Menu时,在弹出的菜单中往往会有一个设置按钮,点击“设置”后,会弹出的设置界面。而系统中的设置界面就是由我们这里说的PreferenceActivity来实现的,PreferenceActivity通过SharedPreference键值对的形式来自动保存数据,不需要我们手动来写,省去一大波操作)
如需添加multidex的依赖项。
在应用或模块的build.gradle文件中添加所需工件的依赖项:

dependencies {
        implementation "androidx.preference:preference:1.1.0"
    }
23、 Android tv 开源库 依赖项

如需添加leanback:leanback 的依赖项。
在应用或模块的build.gradle文件中添加所需工件的依赖项:

dependencies {
        implementation "androidx.leanback:leanback "
    }

你可能感兴趣的:(Android xxxxx- KTX 库)