程序员android技术

Android KTX

2020-08-17  本文已影响0人  农民工Alan

Android KTX 是包含在 Android Jetpack 及其他 Android 库中的一组 Kotlin 扩展程序。KTX 扩展程序可以为 Jetpack、Android 平台及其他 API 提供简洁的惯用 Kotlin 代码。为此,这些扩展程序利用了多种 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"
    }
上一篇下一篇

猜你喜欢

热点阅读