31

Android Studio 4.1 的 Dagger 导航更新

 3 years ago
source link: https://mp.weixin.qq.com/s?__biz=MzAwODY4OTk2Mg%3D%3D&%3Bmid=2652068343&%3Bidx=1&%3Bsn=407a27b721f50b80fd57238b3f6e4dbc
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

a2eeAnJ.png!mobile

在使用 Dagger 的项目中,您是否因为要弄清楚 Dagger 的依赖关系而产生疑惑?依赖项来自 @Inject 修饰的构造器、@Binds 还是 @Provides 修饰的方法?它有限定符吗?要搞清楚这个并非易事。

难道不能一键获取相关信息甚至得到更多内容吗?当然可以!

最新的 Android Studio 4.1 版本 (目前已经在稳定版渠道发布) 为我们带来了一个新的 " 间距图标 " (在编辑器和代码行号之间的图标,如下图所示),通过点击这个图标, 我们可以轻松地在 Dagger 的关联代码间进行导航 ,包括依赖项的生产者、消费者、组件、子组件以及模块。您同样可以在 Find usages 中获取相同的信息。

在 Android Studio 4.2 中加入了更多针对 Hilt 的支持,除了上述列出的 Dagger 功能之外,您还可以更便捷使用入口点 (entry points) 导航功能。

feAjme3.gif!mobile

在 Android Studio 中新增的 Dagger 导航支持

正如您所看到的,在 Android 应用中 Dagger 关系的导航从未如此简单。新版本 Android Studio 将支持 一键精确查找 提供依赖项方法的功能。

实践

在 Android Studio 4.1 Canary 7 之后,您可以在使用 Dagger 的项目中看到一个新的间距图标:

N3ayyuJ.png!mobile

Android Studio 中新的 Dagger 间距图标

它们的点击效果是:

  • 向上箭头的图标: 提供类型的地方 (即依赖项来自何处)

  • 树形的图标: 类型被当作依赖项使用的地方

让我们通过 GitHub 上的示例应用  architecture-samples 的 Dagger 分支 (dev-dagger) ,来展示新功能的使用。

  • architecture-samples 的 Dagger 分支 (dev-dagger)

    https://github.com/android/architecture-samples/tree/dev-dagger

了解依赖项来自哪里

在一个使用 Dagger 注入的类中,如果您在依赖项上点击带有向上箭头的间距图标,您将跳转到使用 Dagger 注解提供对应类型的方法。

在下面的例子中,TasksViewModel 依赖了 TasksRepository。点击间距图标即可跳转到 AppModuleBinds 类中提供 TasksRepository 对象的 @Bind 方法:

BvAn63J.gif!mobile

了解依赖项来自哪里

支持限定符

如上所述,如果使用了限定符提供依赖项,此功能将会帮您精确跳转到提供方法处。

DefaultTasksRepository 依赖了被限定符修饰的 TasksDataSource。点击间距图标将跳转到 AppModule 中使用限定符提供对应类型的方法。

UNri2uy.gif!mobile

支持限定符

依赖项在哪里被使用

当您有一个使用 Dagger 注解提供依赖项的方法时,您可以点击带有向下箭头的间距图标来跳转到依赖项的使用处。如果该依赖项被多个消费者使用,您可以在列表中选择目标进行跳转。

在我们的项目中,DefaultTasksRepository 使用在了不同的 ViewModels 中,具体是哪些呢?您可以通过点击提供方法的间距图标来了解 (此处使用 @Binds):

6FziiaI.gif!mobile

了解依赖项在哪里被使用

Hilt 入口点

当您在一个 Hilt 的入口点 (entry point) 时,这个间距图标可以帮助您导航到依赖的来源。我们撰写了一个在 Android 中 将 Dagger 迁移到 Hilt 的 codelab ,在这个 codelab 里,您可通过 android-dagger-to-hilt 工程的 interop 分支 的代码来试用这个功能。

Fbiyiqz.gif!mobile

在入口点可以导航到依赖的来源

  • Hilt 入口点 (entry point)

    https://developer.android.google.cn/training/dependency-injection/hilt-android#not-supported

  • 将您的应用从 Dagger 迁移到 Hilt (英文内容)

    https://codelabs.developers.google.com/codelabs/android-dagger-to-hilt/#0

  • android-dagger-to-hilt 工程的 interop 分支

    https://github.com/googlecodelabs/android-dagger-to-hilt/tree/interop

Find Usages 功能

在 Android Studio 中可以使用 Find usage 功能找到您 Dagger/Hilt 代码之间的关系。

如果您右键点击 AppModuleBinds 类的 bindRepository 方法,选择 Find usage,然后就会看到如下类似的界面内容:

ieyEZr2.png!mobile

为 bindRepository 方法使用 Find usage 功能

赶快尝试起来!如果您在使用中出现任何问题,请及时向我们 反馈问题

  • 反馈问题

    https://issuetracker.google.com/issues/new?component=192708&template=840533&title=%5BPlease+title+your+report%5D+%23dagger-support

umuauan.png!mobile

推荐阅读

qEbI7f7.png!mobile

Y7v67nV.png!mobile

fq6bUrn.png!mobile

点击屏末    |   查看 Android 官方文档里关于依赖注入和 Hilt 的相关指南

3IF7nu7.gif!mobile

v2QBZvN.png!mobile


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK