jetpack_Jetpack的新功能

jetpack

Android Jetpack is a suite of libraries aimed at helping you write high-quality apps easily, supporting older versions of the Android OS. Now, two years after the launch of Jetpack, we’ve seen tremendous adoption by apps, from large developer teams to those just getting started. And that’s just the beginning: today, we’re launching new libraries, along with major updates to existing ones, that we’ve been working on over the past year. Here’s a round up of the latest updates in Jetpack — an extended version of our What’s new in Jetpack talk!

Android Jetpack是一套库,旨在帮助您轻松编写高质量的应用程序,并支持较旧版本的Android OS。 现在,在Jetpack推出两年后,我们看到了从大型开发人员团队到刚上手的应用程序所广泛采用的应用程序。 这仅仅是开始:今天,我们正在启动新的库,以及过去一年来我们一直在努力的现有库的重大更新。 这是Jetpack最新更新的摘要-Jetpack谈话的新增功能的扩展版本!

What’s new in Jetpack
Jetpack的新功能

Alpha的新功能 (New in Alpha)

Hilt — Jetpack推荐的依赖注入库 (Hilt — Jetpack’s recommended library for Dependency Injection)

Hilt is a new Android library which simplifies dependency injection (DI) in your application. Hilt lets you focus on just the important parts of defining and injecting bindings without worrying about managing all of the DI setup and wiring.

Hilt是一个新的Android库,可简化应用程序中的依赖项注入 (DI)。 Hilt使您可以仅关注定义和注入绑定的重要部分,而不必担心管理所有DI设置和接线。

Built on top of Dagger, Hilt benefits from it’s compile-time correctness, improved runtime performance, and scalability. Hilt adds integration with Jetpack libraries and Android framework classes. For example, to inject parameters of a ViewModel you can now annotate the ViewModel constructor with @ViewModelInject and then annotate the Fragment with @AndroidEntryPoint:

Hilt构建于Dagger之上,得益于其编译时正确性,改进的运行时性能和可伸缩性。 Hilt添加了与Jetpack库和Android框架类的集成。 例如,要注入的参数ViewModel ,你现在可以注释ViewModel与构造@ViewModelInject然后注释Fragment@AndroidEntryPoint

<!-- Copyright 2019 Google LLC.	
   SPDX-License-Identifier: Apache-2.0 -->


class SearchViewModel @ViewModelInject constructor(
    private val repository: SearchRepository
): ViewModel() { … }


@AndroidEntryPoint
class SearchFragment : Fragment() {
    val viewModel: SearchViewModel by viewModels()
}

Find out more about Hilt and discover more links to related resources in our launch blog post.

在我们的启动博客文章中找到有关Hilt的更多信息,并找到指向相关资源的更多链接。

分页3-增量加载和显示数据 (Paging 3 — load and display data incrementally)

Paging is a library that helps you load and display small chunks of data incrementally. Today we’re releasing Paging 3 — a complete rewrite of the library using Kotlin coroutines. This new release adds highly requested features like separators, headers, footers and list transformations and APIs for observing list loading state and methods for retry and refresh.

分页是一个库,可帮助您增量加载和显示小块数据。 今天,我们发布了Paging 3-使用Kotlin coroutines对库的完整重写。 这个新版本增加了要求很高的功能,例如分隔符,页眉,页脚和列表转换,以及用于观察列表加载状态的API和用于重试和刷新的方法。

For example, with Paging 3, the data source can be defined by extending the PagingSource class and implementing the load suspend function, where you can directly call other suspend functions:

例如,与寻呼3中,数据源可以通过扩展定义PagingSource类并实现load suspend功能,在这里可以直接调用其他暂停功能:

<!-- Copyright 2019 Google LLC.	
   SPDX-License-Identifier: Apache-2.0 -->


class MyPagingSource : PagingSource<Key, Value>() {
    override suspend fun load(params: LoadParams<Key>): LoadResult<Key, Value> {
        try {
            val result = api.requestPage(params.key)
            return Page(
                data = result.items,
                nextKey = result.nextKey
            )
        } catch(error: IOException) {
            return Error(error)
        }
    }
}

For more information about Paging 3, check out the documentation and our codelab.

有关分页3的更多信息,请查看文档和我们的代码实验室

应用启动-在应用启动时初始化组件 (App Startup — initialize components at application startup)

The App Startup library provides a straightforward, performant way to initialize components at application startup. Instead of defining separate content providers for each component you need to initialize, App Startup allows you to define component initializers that share a single content provider. This can significantly improve app startup time.

App Startup库提供了一种直接,高效的方法来在应用程序启动时初始化组件。 App Startup允许您定义共享单个内容提供程序的组件初始化程序,而不必为需要初始化的每个组件定义单独的内容提供程序。 这可以大大缩短应用程序的启动时间。

Here’s how App Startup can be used to improve the initialization time of WorkManager, a library that uses a ContentProvider under the hood, after disabling the WorkManager’s own ContentProvider:

这是在禁用WorkManager自己的ContentProvider之后,可以使用App Startup来改善WorkManager的初始化时间的方法,WorkManager是一个在后台使用ContentProvider的库:

<!-- Copyright 2019 Google LLC.	
   SPDX-License-Identifier: Apache-2.0 -->


class WorkManagerInitializer : Initializer<WorkManager> {
    override fun create(context: Context): WorkManager {
        val configuration = Configuration.Builder()
            .setMinimumLoggingLevel(Log.DEBUG)
            .build()
        WorkManager.initialize(context, configuration)
        return WorkManager.getInstance(context)
    }
    
    override fun dependencies() : List<Class<out Initializer<*>>> = emptyList()
}

For more information on App Startup, check out the official documentation.

有关App Startup的更多信息,请查阅官方文档

自动填充IME集成 (Auto-fill IME integrations)

Android 11 introduces platform APIs for keyboards to display autofill suggestions from apps such as password managers. Jetpack’s Autofill API makes it easier for keyboards and autofill services to take advantage of this new feature via its InlineSuggestionUi class. Autofill services can use this class to create compatible suggestions; keyboards can use it to customize the style of the suggestions.

Android 11引入了用于键盘的平台API,以显示来自密码管理器等应用程序的自动填充建议。 Jetpack的Autofill API通过其InlineSuggestionUi类使键盘和自动填充服务更容易利用此新功能。 自动填充服务可以使用此类来创建兼容建议。 键盘可以使用它来自定义建议的样式。

Image for post
Keyboard displays card number autofill suggestion
键盘显示卡号自动填充建议

具有核心动画和SeekableAnimatedVectorDrawable的动画更轻松 (Easier animations with core-animation and SeekableAnimatedVectorDrawable)

To make implementing and testing animations easier, we’ve added two new libraries: androidx.core:core-animation and androidx.core:core-animation-testing. We’ve also introduced a new API SeekableAnimatedVectorDrawable as part of androidx.vectordrawable library.

为了androidx.core:core-animation动画的实现和测试,我们添加了两个新的库: androidx.core:core-animation androidx.core:core-animation-testing 。 我们还引入了新的API SeekableAnimatedVectorDrawable作为androidx.vectordrawable库的一部分。

core-animation backports all the features added to the Animator API since Ice Cream Sandwich, such as pause/resume and seek.

core-animation向后移植自Ice Cream Sandwich以来添加到Animator API的所有功能,例如暂停/继续和搜索。

SeekableAnimatedVectorDrawable, based on core-animation, is a new, seekable alternative to AnimatedVectorDrawable (AVD). It uses the same format as AVD and adds the ability to seek, pause, and resume playback. Seekable AVDs cannot use the render thread so should only be preferred to AVD when the new class’s additional features are required.

基于核心动画的SeekableAnimatedVectorDrawableAnimatedVectorDrawable (AVD)的一种新的,易于寻找的替代方法。 它使用与AVD相同的格式,并增加了查找,暂停和继续播放的功能。 可搜索的AVD无法使用渲染线程,因此仅在需要新类的附加功能时才应首选AVD。

在Android Studio中使用Database Inspector调试数据库 (Debug your database with Database Inspector in Android Studio)

Image for post
Using Database inspector in Android Studio
在Android Studio中使用数据库检查器

Debugging issues with your database is now easier in Android Studio 4.1 Beta with the new Database Inspector. This tool enables you to inspect, query, and modify SQLite databases in your running app. Whether you’re using Room or SQLite directly, you can start debugging your databases by selecting View > Tool Windows > Database Inspector from the menu bar. Find out more about the tool and how to use it in this blog post.

使用新的数据库检查器,现在在Android Studio 4.1 Beta中更容易调试数据库问题。 使用此工具,您可以在正在运行的应用程序中检查,查询和修改SQLite数据库。 无论您是直接使用Room还是SQLite,都可以通过从菜单栏中选择“ 视图”>“工具窗口”>“数据库检查器”来开始调试数据库。 在此博客文章中找到有关该工具以及如何使用它的更多信息

WindowManager —对设备尺寸的更好支持 (WindowManager — better support for device form factors)

The WindowManager library is a new addition to Android Jetpack that aims to help application developers support new form factors like foldables, hinged screens, and more. It provides a common API surface for different WindowManager features on both old and new platform versions.

WindowManager库是Android Jetpack的新增功能,旨在帮助应用程序开发人员支持可折叠,铰链式屏幕等新形状。 它为新旧平台版本上的不同WindowManager功能提供了一个通用的API表面。

The initial release provides support for different types of foldable devices already on and coming to the market, so that application developers can target entire categories of hardware configurations. Check out the overview in this blog post and the samples to learn more.

初始版本为已经投入市场并投放市场的各种类型的可折叠设备提供支持,以便应用程序开发人员可以针对整个类别的硬件配置。 请查看此博客文章中的概述以及示例以了解更多信息。

MotionLayout —为Android构建流畅的交互式动画 (MotionLayout — build fluid and interactive animations for Android)

The MotionLayout API extends the rich capabilities of ConstraintLayout to help Android developers manage complex motion and widget animation in their apps. With MotionLayout you can model your animations as transitions between ConstraintSets and easily integrate animations with common views like RecyclerView and ViewPager. Android Studio 4.0 also includes the Motion Editor, a graphical tool for creating and previewing animations that use MotionLayout.

MotionLayout API扩展了ConstraintLayout的丰富功能,可帮助Android开发人员在其应用程序中管理复杂的运动和小部件动画。 使用MotionLayout您可以将动画建模为ConstraintSets之间的过渡,并轻松地将动画与常见的视图(例如RecyclerViewViewPager集成ViewPager 。 Android Studio 4.0还包括Motion Editor,Motion Editor是用于创建和预览使用MotionLayout动画的图形工具。

Image for post
Create, edit, and preview MotionLayout animations in the Motion Editor
在运动编辑器中创建,编辑和预览MotionLayout动画

更新现有库 (Updates to existing libraries)

导航 (Navigation)

Navigation 2.3 adds support for dynamic feature modules which allow you to download pieces of your application as the user needs it, significantly reducing the initial download size of your app. Now, you can navigate to these modules as if they are part of the base APK. Along with allowing query parameters, deep links now support custom actions and mime types.

导航2.3添加了对动态功能模块的支持,这些功能使您可以根据用户需要下载应用程序的各个部分,从而大大减小了应用程序的初始下载大小。 现在,您可以导航到这些模块,就像它们是基本APK的一部分一样。 除了允许查询参数外,深层链接现在还支持自定义操作和mime类型。

A new API for returning a result, allows you to query any back stack entry and set the result on its SavedStateHandle. The new TestNavHostController enables you to access the Navigation back stack and set the current destination in tests.

一个用于返回结果的新API,允许您查询任何后退堆栈条目,并在其SavedStateHandle上设置结果。 通过新的TestNavHostController您可以访问向后导航堆栈并在测试中设置当前目标。

工作经理 (WorkManager)

The latest releases of WorkManager, add support for long-running or important work that should be kept alive by the operating system using foreground services.

最新版本的WorkManager增加了对长期运行或重要工作的支持,这些工作应由操作系统使用前台服务保留下来。

To make it easier to diagnose issues with your WorkManager tasks, we added a new diagnostics API that allows you to peek into the internal state of WorkManager and dump its state into logcat:

为了更轻松地诊断WorkManager任务中的问题,我们添加了新的诊断API,该API可让您查看WorkManager的内部状态并将其状态转储到logcat中:

Recently completed work:
Id Class Name Job Id State Unique Name Tags
88e31476–50d5–4a3a-855a-7c158d61543e com.example.DiagnosticsWorker null SUCCEEDED com.example.DiagnosticsWorker
939d3d81–4a24–4920-ab6a-2a5b850f377b com.example.ToastWorker null SUCCEEDED com.example.ToastWorker
59ab8eb1–2645–446d-8811–1e7b887ab5bc com.example.ForegroundWorker null CANCELLED com.example.ForegroundWorker
7a6c23d2-a1f9–4477-af77–1f16b45cd765 com.example.ForegroundWorker 0 SUCCEEDED com.example.ForegroundWorker


Running work:
Id Class Name Job Id State Unique Name Tags
7c8bc01f-a60c-4b63-a6aa-2055d4d9d88e com.example.DiagnosticsWorker 4 RUNNING com.example.DiagnosticsWorker

To help you to avoid common mistakes when using WorkManager, we’ve also added Lint rules to flag them. Other API updates include: support for setting and observing intermediate progress for workers; improvements to the query API for current workers; and improvements to the in-process scheduler used to run your workers.

为了帮助您避免使用WorkManager时的常见错误,我们还添加了Lint规则来标记它们。 其他API更新包括:支持设置和观察工作人员的中间进度 ; 改进了当前工作人员的查询API; 以及用于运行您的工作程序的进程内调度程序的改进。

基准测试 (Benchmark)

Image for post
Sampled traces in Android Studio
Android Studio中的采样跟踪

The new alpha release of the Benchmark library integrates with CPU profiling so you can profile your benchmarks and then view the method or sampled traces right in Android Studio. We’ve also added support for memory allocation tracking, so that you can optimize the time spent allocating and reduce the load on garbage collection.

Benchmark库的新alpha版本CPU分析集成在一起,因此您可以分析基准,然后直接在Android Studio中查看方法或采样的跟踪。 我们还增加了对内存分配跟踪的支持,以便您可以优化分配时间并减少垃圾收集的负担。

权限 (Permissions)

To make it easier to work with permissions we introduced the new ActivityResult APIs. These APIs simplify requesting permissions by replacing requestPermissions with the RequestPermission contract, but also provide type-safe contracts for common intents, like taking a picture or prompting the user to open a document.

为了更轻松地使用权限,我们引入了新的ActivityResult API。 这些API通过用RequestPermission合同替换requestPermissions简化请求权限,而且还为常见意图(例如拍照或提示用户打开文档)提供类型安全的合同。

游戏SDK (Games SDK)

The Android Games SDK, launched earlier this year, is now also part of Jetpack and available on the Google Maven Repository. The SDK currently offers a Frame Pacing API and Android Performance Tuner. Find out more about the Games SDK from the official documentation.

今年早些时候推出的Android游戏SDK现在也是Jetpack的一部分,可在Google Maven存储库中使用。 该SDK当前提供了Frame Pacing API和Android Performance Tuner。 从官方文档中找到有关Games SDK的更多信息。

相机X (CameraX)

There’s a lot of variation in cameras across Android devices; CameraX runs on 90% of them. Since CameraX reached beta last February, we’ve been focusing on reliability to ensure the best behavior of the API across a large range of devices. Our CameraX test lab runs our automated test suite on device types representing over 400M active devices in use.

跨Android设备的相机存在很多差异; CameraX可在其中90%上运行。 自从CameraX于去年2月达到Beta版以来,我们一直专注于可靠性,以确保API在各种设备上的最佳性能。 我们的CameraX测试实验室在代表使用中超过400M有源设备的设备类型上运行我们的自动化测试套件。

The latest version of CameraX brings improvements to the PreviewView widget. It now handles interactions with your application lifecycle and view pagers reliably. It is also optimized to transparently use SurfaceView under the hood on devices that would benefit from its performance enhancements. This results in less buffering and better power efficiency.

CameraX最新版本对 PreviewView小部件进行了改进。 现在,它可以处理与应用程序生命周期的交互并可靠地查看寻呼机。 它还经过优化,可以在设备的内部透明使用SurfaceView ,这将受益于其性能增强。 这样可以减少缓冲并提高电源效率。

Check out the extensive CameraX documentation and samples to learn more about the APIs.

查看大量的CameraX文档样本,以了解有关API的更多信息。

安全 (Security)

Jetpack’s Security library provides safe and easy to use file-based crypto abstractions like EncryptedFile and EncryptedSharedPreferences. Jetpack Security takes advantage of Android’s Keystore, which provides hardware-backed storage and operational safety. Jetpack Security is now at Release Candidate 2 for Marshmallow+, and 1.1.0 alpha for Lollipop+ support.

Jetpack的安全性库提供了安全且易于使用的基于文件的加密抽象,例如EncryptedFileEncryptedSharedPreferences 。 Jetpack Security利用Android的Keystore的优势,后者提供了硬件支持的存储和操作安全性。 现在, Jetpack Security可以用于棉花糖+的候选发布版2,以及用于Lollipop +支持的1.1.0 alpha发行版。

AppCompat (AppCompat)

AppCompat provides backports for a wide variety of UI elements and platform features, from the Material theme, to widgets like the Toolbar, to dark theme. In the latest releases, we’ve added Lint rules that allow you to better understand which attributes come from AppCompat and which from the framework, and ensure you use the right one. We’ve also made substantial stability improvements to AppCompat’s implementation of dark theme.

AppCompat为从材料主题到工具Toolbar类的小部件到深色主题的各种UI元素和平台功能提供了反向移植。 在最新版本中 ,我们添加了Lint规则,使您可以更好地了解哪些属性来自AppCompat,哪些属性来自框架,并确保使用正确的属性。 我们还对AppCompat深色主题的实现进行了实质性的稳定性改进。

网页套件 (Webkit)

Jetpack’s Webkit library adds a new API in 1.2.0 release to force dark mode for its content. When this API is enabled, WebView will render sites in dark theme where supported. Where websites don’t support dark theme, the API will invert certain colors.

Jetpack的Webkit库在1.2.0版本中添加了新的API,以强制其内容采用暗模式。 启用此API后, WebView将在受支持的地方以深色主题呈现网站。 如果网站不支持深色主题,则API会反转某些颜色。

Jetpack Compose — Android的新UI工具包 (Jetpack Compose — Android’s new UI toolkit)

Jetpack Compose, Android’s new, modern UI toolkit, is now in Developer Preview 2. This release adds many new features: View interoperability, more Material UI components, dark theme support, new UI testing and animation APIs, initial support for ConstraintLayout, improvements to state management, integrations with observable streams and RTL support. Android Studio previews can now be interactive, and there have been many compiler improvements.

Android最新的现代UI工具包Jetpack Compose现在位于Developer Preview 2中。此版本增加了许多新功能:查看互操作性,更多的Material UI组件,深色主题支持,新的UI测试和动画API,对ConstraintLayout初始支持,对状态管理,与可观察流的集成以及RTL支持。 Android Studio预览现在可以是交互式的,并且在编译器方面已有许多改进。

Find out more about all the updates from the What’s new in Compose talk, and then try it out and give us feedback.

Compose新增功能对话中找到有关所有更新的更多信息,然后尝试一下并给我们反馈

This was a quick tour of all the Jetpack updates of the last few months. With 90+ libraries available, we know that it can be hard to find the one you need. To fix this, we redesigned the Jetpack webpage and added, among others, a handy API picker to quickly go from the problem you have to the Jetpack library that can solve it.

这是过去几个月中所有Jetpack更新的快速浏览。 拥有90多种库,我们知道很难找到所需的库。 为了解决这个问题,我们重新设计了Jetpack网页,并添加了一个方便的API选择器,以快速将您遇到的问题转移到可以解决问题的Jetpack库中。

To find out more about the Jetpack libraries, stay tuned for new videos, blog posts, and codelabs coming out in the Jetpack week (week of July 20th) of the 11 Weeks of Android.

要了解有关Jetpack库的更多信息,请继续关注“ Android 11周”的Jetpack周(7月20日这一周)的新视频,博客文章和代码实验室。

翻译自: https://medium.com/androiddevelopers/whats-new-in-jetpack-1891d205e136

jetpack

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值