重点 (Top highlight)

Dependency injection (DI) is a technique widely used in programming and well suited to Android development, where dependencies are provided to a class instead of creating them itself. By following DI principles, you lay the groundwork for good app architecture, greater code reusability, and ease of testing. Have you ever tried manual dependency injection in your app? Even with many of the existing dependency injection libraries today, it requires a lot of boilerplate code as your project becomes larger, since you have to construct every class and its dependencies by hand, and create containers to reuse and manage dependencies.

依赖项注入 (DI)是一种广泛用于编程的技术,非常适合Android开发,该类将依赖项提供给类,而不是自己创建依赖项。 通过遵循DI原则,您将为良好的应用程序体系结构,更高的代码可重用性和易于测试奠定基础。 您是否曾尝试在应用程序中进行手动依赖项注入? 即使使用当今许多现有的依赖项注入库,由于您的项目越来越大,它仍需要大量样板代码,因为您必须手动构造每个类及其依赖项,并创建容器以重用和管理依赖项。

By following DI principles, you lay the groundwork for good app architecture, greater code reusability, and ease of testing.

通过遵循DI原则,您将为良好的应用程序体系结构,更高的代码可重用性和易于测试奠定基础

The new Hilt library defines a standard way to do DI in your application by providing containers for every Android class in your project and managing their lifecycles automatically for you. Hilt is currently in alpha, try it in your app and give us feedback using this link.

通过为项目中的每个Android类提供容器并为您自动管理其生命周期,新的Hilt库定义了一种在应用程序中执行DI的标准方法 。 Hilt目前处于Alpha状态 ,请在您的应用中进行尝试,并使用此链接向我们提供反馈。

Hilt is built on top of the popular DI library Dagger so benefits from the compile time correctness, runtime performance, scalability, and Android Studio support that Dagger provides. Due to this, Dagger’s seen great adoption on 74% of top 10k apps of the Google Play Store. However, because of the compile time code generation, expect a build time increase.

Hilt基于流行的DI库Dagger构建,因此可以从Dagger提供的编译时间正确性,运行时性能,可伸缩性和 Android Studio支持中受益。 因此,Dagger在Google Play商店的前10k顶级应用中占74%的广泛采用率。 但是,由于生成了编译时代码,因此预期编译时间会增加。

Since many Android framework classes are instantiated by the OS itself, there’s an associated boilerplate when using Dagger in Android apps. Unlike Dagger, Hilt is integrated with Jetpack libraries and Android framework classes and removes most of that boilerplate to let you focus on just the important parts of defining and injecting bindings without worrying about managing all of the Dagger setup and wiring. It automatically generates and provides:

由于许多Android框架类是由操作系统本身实例化的,因此在Android应用中使用Dagger时会有一个关联的样板。 与Dagger不同,Hilt与Jetpack库和Android框架类集成在一起,并删除了大部分样板,使您可以专注于定义和注入绑定的重要部分 ,而不必担心管理所有Dagger设置和接线。 它会自动生成并提供:

  • Components for integrating Android framework classes with Dagger that you would otherwise need to create by hand.

    用于将Android框架类与Dagger 集成的组件,否则需要手工创建。

  • Scope annotations for the components that Hilt generates automatically.

    Hilt自动生成的组件的范围注释

  • Predefined bindings and qualifiers.

    预定义的绑定和限定符

Best of all, as Dagger and Hilt can coexist together, apps can be migrated on an as-needed basis.

最重要的是, 由于Dagger和Hilt可以共存,因此可以根据需要迁移应用程序

行动中 (Hilt in action)

Just to show you how easy to use Hilt is, let’s perform some quick DI in a typical Android app. Let’s make Hilt inject an AnalyticsAdapter into our MainActivity.

只是为了向您展示Hilt的易用性,让我们在典型的Android应用中执行一些快速DI。 让我们让Hilt将AnalyticsAdapter注入到MainActivity

First, enable Hilt in your app by annotating your application class with the @HiltAndroidApp to trigger Hilt’s code generation:

首先,通过使用@HiltAndroidApp注释应用程序类以触发Hilt的代码生成,从而在应用程序中启用Hilt:

@HiltAndroidAppclass MyApplication : Application() { ... }

Second, tell Hilt how to provide instances of AnalyticsAdapter by annotating its constructor with @Inject:

第二,告诉希尔特如何提供的实例AnalyticsAdapter通过注释它的构造@Inject

class AnalyticsAdapter @Inject constructor() { ... }

And third, to inject an instance of AnalyticsAdapter into MainActivity, enable Hilt in the activity with the @AndroidEntryPoint annotation and perform field injection using the @Inject annotation:

第三,将AnalyticsAdapter的实例注入MainActivity ,在活动中使用@AndroidEntryPoint批注启用Hilt并使用@Inject批注执行字段注入:

@AndroidEntryPointclass MainActivity : AppCompatActivity() {  @Inject lateinit var analytics: AnalyticsAdapter  override fun onCreate(savedInstanceState: Bundle?) {    super.onCreate(savedInstanceState)    // analytics instance has been populated by Hilt    // and it's ready to be used  }}

For more information, you can easily check out what the new annotations do in the cheat sheet section below.

有关更多信息,您可以在下面的备忘单部分中轻松查看新注释的功能。

随附Jetpack支持! (Comes with Jetpack support!)

You can use your favourite Jetpack libraries with Hilt out of the box. We’re providing direct injection support for ViewModel and WorkManager in this release.

您可以在开箱即用的情况下使用喜爱的Jetpack库。 在此版本中,我们为ViewModel和WorkManager提供直接注入支持

For example, to inject a Architecture Components ViewModel LoginViewModel into a LoginActivity: annotate LoginViewModel with @ViewModelInject and use it in the activity or fragment as you’d expect:

例如,要将架构组件ViewModel LoginViewModel注入LoginActivity :用@ViewModelInject注释LoginViewModel @ViewModelInject预期在活动或片段中使用它:

class LoginViewModel @ViewModelInject constructor(  private val analyticsAdapter: AnalyticsAdapter): ViewModel { ... }@AndroidEntryPointclass LoginActivity : AppCompatActivity() {  private val loginViewModel: LoginViewModel by viewModels()  override fun onCreate(savedInstanceState: Bundle?) {    super.onCreate(savedInstanceState)    // loginViewModel is ready to be used  }}

Learn more about Jetpack support in the docs.

在文档中了解有关Jetpack支持的更多信息。

开始使用Hilt (Start using Hilt)

If you’re intrigued by Hilt and want to learn more about it, here’s some resources for you to learn in the way you prefer:

如果您对Hilt感兴趣,并且想了解更多有关此的信息,请按照以下偏好的方式学习一些资源:

Hilt入门 (Getting started with Hilt)

Learn how to add Hilt in your Android app with this guide.

借助本指南,了解如何在您的Android应用中添加Hilt。

文献资料 (Documentation)

If you’re new to DI or Dagger, check out our guide to add Hilt to an Android app. Alternatively, if you already know Dagger, we’re also providing documentation on dagger.dev.

如果您不熟悉DI或Dagger,请查看我们的指南,将Hilt添加到Android应用中 。 另外,如果您已经了解Dagger,我们还将提供有关dagger.dev的文档 。

If you’re just curious about the new annotations and what you can do with Hilt, check out this cheat sheet in the section below.

如果您只是对新的注解以及Hilt的功能感到好奇,请在以下部分中查看该备忘单。

对于Dagger用户 (For Dagger users)

If you’re already using Dagger or dagger.android in your app, check out this migration guide or the codelab mentioned below to help you switch to Hilt. As Dagger and Hilt can coexist together, you can migrate your app incrementally.

如果您已经在应用程序中使用了Dagger或dagger.android,请查看此迁移指南或下面提到的代码实验室,以帮助您切换到Hilt。 由于Dagger和Hilt可以共存,因此您可以逐步迁移应用程序。

代码实验室 (Codelabs)

To learn Hilt in a step-by-step approach, we just released two codelabs:

为了逐步学习Hilt,我们刚刚发布了两个代码实验室:

  • Using Hilt in your Android app

    在Android应用中使用Hilt

  • Migrate from Dagger to Hilt

    从匕首迁移到击剑

样例代码 (Sample code)

Do you want to see how Hilt is used in existing apps? Go check its usage in the Google I/O 2020 app and in the dev-hilt branch of the Android architecture-samples Github repository.

您想查看在现有应用中如何使用Hilt吗? 在Google I / O 2020应用程序和Android 体系结构示例Github存储库的dev-hilt分支中检查其用法。

反馈 (Feedback)

Hilt is currently in alpha, try it in your app and give us feedback using this link.

Hilt目前处于Alpha状态 ,请在您的应用中进行尝试,并使用此链接向我们提供反馈。

备忘单 (Cheat sheet)

This cheat sheet allows you to quickly see what the different Hilt and Dagger annotations do and how to use them.

这种小抄可以让你快速了解不同希尔特和匕首注释做,以及如何使用它们。

Download cheat sheet in PDF

下载PDF备忘单

Download in PDF here.在此处以PDF下载 。

翻译自: https://medium.com/androiddevelopers/dependency-injection-on-android-with-hilt-67b6031e62d


http://www.taodudu.cc/news/show-5179583.html

相关文章:

  • 100行的python作品详解_漫画喵的100行Python代码逆袭
  • 课题设计相关
  • 李宏毅《深度学习》(一)
  • 【通信原理】五、模拟调制系统
  • NAND flash替换问题
  • XELTEK希尔特自动序列号递增功能
  • 行式存储和列式存储
  • es索引优化(行存储、列存储、索引)
  • 列存储与行存储的对比
  • 如何在mac使用 ngrok (别听那帮人胡咧咧看我)
  • mac使用指定python版本【永久升效】
  • MAC下使用Jconsole
  • Macbook使用过程遇到的问题及解决方法
  • Mac下使用Linux
  • mac 使用docker 安装mysql
  • 【Mac】M1芯片WireShark使用
  • Mac使用npm install报错,需使用sudo
  • macbook 使用技巧
  • mac使用brew安装mysql
  • mac使用ssh密钥连接云服务器
  • mac使用tree命令
  • mac 使用mysql + navicat
  • 【Mac】Mac使用大全
  • 电子计算器
  • 我的世界怎么用计算机,我的世界计算器怎么用 全计算器使用说明
  • python材料计算程序_把python当日常的”计算器”用
  • LP光纤模式计算器
  • 编写简易计算器
  • 勘智K510连接WIFI
  • 嘉楠堪智kendryke K210资料汇总

具有Hilt的Android上的依赖注入相关推荐

  1. Android开源框架——依赖注入ButterKnife

    若对依赖注入不熟悉,请阅读博客中的另外一篇IOC控制反转浅析 介绍:ButterKnife是Square公司员工JakeWharton开发的一款针对View视图对象的依赖注入库.目的是通过依赖注入方式 ...

  2. Android开发中依赖注入的应用

    什么是依赖注入? 依赖是指一个对象持有其他对象的引用.依赖注入则是将这些依赖对象传递给被依赖对象,而不是被依赖对象自己创建这些对象.  public class MyClass{private Ano ...

  3. Android上的依赖库简介

    android不但可以引用jar包,而且还有自己的特殊jar包–aar压缩包. 一.aar的生成方法 1.直接make project生成aar 在生成AAR的时候,先创建一个Library的Modu ...

  4. Android开源框架——依赖注入Dagger

    介绍:Dagger是Square公司开发依赖注入框架,主要针对辅助类对象,而ButterKnife是针对View视图对象的. github:https://github.com/square/dagg ...

  5. 依赖注入框架-dragger2

    本文转载自:https://github.com/EvilBT/-Dagger2Demo Dagger2 是一款使用在Java和Android上的依赖注入的一个类库,用来解耦的. 简单使用@injec ...

  6. android组件浮动在activity上_Jetpack Hilt 依赖注入框架上手指南

    code小生 一个专注大前端领域的技术平台公众号回复Android加入安卓技术群 作者:LvKang-insist 链接:https://juejin.im/post/5efdff9d6fb9a07e ...

  7. 从 Dagger 到 Hilt,谷歌为何执着于让我们用依赖注入?

    来源 | 扔物线 责编 | Carol 文章开始之前,首先来看个视频: 开始 说到依赖注入,做 Android 的人都会想到一个库:Dagger:说到 Dagger,大家的反应普遍是一套三连:牛逼.高 ...

  8. Jetpack新成员,一篇文章带你玩转Hilt和依赖注入

    本文同步发表于我的微信公众号,扫一扫文章底部的二维码或在微信搜索 郭霖 即可关注,每个工作日都有文章更新. 各位小伙伴们大家早上好. 终于要写这样一篇我自己都比较怕的文章了. 虽然今年的Google ...

  9. Android 依赖注入可以更简单 —— 新版本 Dagger 2 使用教学

    今年 3 月 21 号 Dagger 2 在 2.10 版本之后针对 Android 方面做了很大的优化,使用方法也随之有了不少变化.本次改动除了让 Dagger 2 的使用更加符合控制反转原则,还针 ...

最新文章

  1. 关于linux 内存碎片指数
  2. 关于红酒的资料收集-2
  3. python 栈和队列 排序 初级数据结构
  4. C++STL常用算术生成算法
  5. Rethinking Design Patterns - from Jeff Atwood
  6. SAP S/4HANA Customer Management(CRM)模块的扩展性设计
  7. oracle 数据执行计划,Oracle 常见的执行计划步骤(explain结果的Description数据参考)...
  8. 【离散数学中的数据结构与算法】七 排列与组合三
  9. 移动开发-语音识别-调用讯飞平台提供的API
  10. SQL Server-外部联接基础
  11. 总结---JavaScript数组
  12. android o系统字体下载,fonts软件安卓下载-fonts字体 安卓版v4.0.0-PC6安卓网
  13. ext2文件系统之ext2_lookup函数源代码分析
  14. 用java求可达矩阵_ISM算法(邻接矩阵求可达矩阵)Java实现
  15. [Excel知识技能] Excel数据类型
  16. day 46 http和html
  17. 树莓派Raspberry Pi 系统搭建和智能机器人小车的组装调试
  18. AI语音机器人拿来做什么用?
  19. 面试题目总结(CNN)
  20. android 实现自动拍照,Android:调用系统相机实现拍照+裁切(兼容7.0以上系统)

热门文章

  1. OpenStack介绍说明、OpenStack架构说明、OpenStack核心服务详细说明【keystone,nova,cinder,neutron...】、OpenStack创建VM,服务间交互示例
  2. [PHB]FDN开启后手机仍然能够上网 - MTK物联网在线解答 - 技术论坛
  3. Android HDCP
  4. Robots协议(爬虫协议、机器人协议)
  5. 一年之计在于春-2014
  6. 迈克菲:如何规避Java漏洞带来的安全风险
  7. 给ssh服务添加fail2ban安全认证
  8. 【NOI2019模拟2019.7.1】为了部落 (生成森林计数,动态规划)
  9. android深入浅出binder机制,Android深入浅出之Binder机制.pdf
  10. Canvas动态改变宽高解决拉伸问题