击剑编排软件

Following on from an official site of the Dagger, Hilt is built on top of the popular Dependency Injection (DI) library Dagger to provides a standard way to incorporate Dagger dependency injection into an Android application.

˚Following从的官方网站匕首,刀柄是建立在流行的依赖注入的顶部(DI)库匕首提供了一个标准的方法,将匕首依赖注入到一个Android应用程序。

The goals of Hilt are:

Hilt的目标是:

- To simplify Dagger-related infrastructure for Android apps.- To create a standard set of components and scopes to ease setup, readability/understanding, and code sharing between apps.- To provide an easy way to provision different bindings to various build types (e.g. testing, debug, or release).

-为Android应用程序简化与Dagger相关的基础结构。-创建一组标准的组件和范围以简化应用程序之间的设置,可读性/理解和代码共享。例如测试,调试或发布)。

If you are unfamiliar with Dagger, please checkout a series which I think it is very detailed and easiest to understand to start with Dagger.

如果您不熟悉Dagger,请查看一系列我认为非常详细且最容易理解的Dagger系列。

  • Dagger 2. Part I. Basic principles, graph dependencies, scopes.

    Dagger 2.第一部分。基本原理,图形依赖关系,范围。

Until now, we have had many introductions and tutorials to setup, migrate,… to use Hilt.

到现在为止,我们已经有许多介绍和教程来进行设置,迁移……以使用Hilt。

  • Dependency injection with Hilt.

    Hilt的依赖注入

  • Migrating your Dagger app to Hilt.

    将您的Dagger应用迁移到Hilt

But we have very few articles on how to work with Hilt in the Dynamic Feature world.

但是,在动态功能世界中,关于如何使用Hilt的文章很少。

In this post, we will see the Hilt configuration way in a project use the dynamic feature module (DFM) and how to deal with @ViewModelInject to inject SavedStateHandle into a ViewModel in DFM.

在本文中,我们将看到项目中使用动态功能模块( DFM )的Hilt配置方式,以及如何处理@ViewModelInjectSavedStateHandle注入DFM中的ViewModel中。

Nowaday in the Android’s products developement, the choice of modularization architecture (multi-module) to get many advantages is obvious. Applying DI for that architecture to manage instances by each scope is also obvious. If your multi-module project is composed of regular Gradle modules, then you can use Dagger by in the usual way. However, if your project is composed of multi DFM module then everything will be different.

如今,在Android产品的开发中,获得许多优势的模块化架构(多模块)的选择显而易见。 将DI应用于该体系结构以按每个作用域管理实例也很明显。 如果您的多模块项目由常规Gradle模块组成,则可以按常规方式使用Dagger。 但是,如果您的项目是由多个DFM模块组成的,那么一切都会有所不同。

In DFM, the way that modules usually depend on each other is inverted. Therefore, Hilt cannot process annotations in feature modules. You must use Dagger to perform dependency injection in your DFM.

在DFM中,模块通常相互依赖的方式是相反的。 因此,Hilt无法处理功能模块中的注释。 您必须使用Dagger在DFM中执行依赖项注入。

DI in DFM is still a complex technique, partly because it is abstract. Before Hilt was born, one of the most Dagger implementations in DFM is:

DFM中的DI仍然是一项复杂的技术,部分是因为它是抽象的。 在Hilt出生之前,DFM中最Dagger的实现之一是:

  • Gapo Dashboard

    Gapo资讯主页

Now, Hilt has arrived to reduce more Java/Kotlin boilerplate and it is the future.

现在,Hilt已经到来以减少更多Java / Kotlin样板,这是未来。

It is time to play Hilt together.

是时候一起玩Hilt了。

1.使用3个模块创建一个新项目:应用,核心(库模块)和功能(功能模块)。 (1. Create a new project with 3 modules: app, core (Library Module) and feature (Feature Module).)

App depends on Core. Feature depends on App and Core.

应用取决于Core。 功能取决于App和Core。

2.添加依赖项。 (2. Adding dependencies.)

  • First, add the hilt-android-gradle-plugin plugin to your project's root build.gradle file:

    首先,将hilt-android-gradle-plugin插件添加到项目的根build.gradle文件中:

buildscript {    ...    dependencies {        ...

    }}
  • Then, apply the Gradle plugin and add these dependencies in your modules’ .../build.gradle file:

    然后,应用Gradle插件,并将这些依赖项添加到模块的.../build.gradle文件中:

...apply plugin: 'kotlin-kapt'apply plugin: 'dagger.hilt.android.plugin' // not needed for feature moduleandroid {    ...}dependencies {

    implementation 'androidx.hilt:hilt-common:1.0.0-alpha02'    implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02'    kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha02'}

3.编码 (3. Coding)

  • In app module:

    在应用程序模块中:

@HiltAndroidApp
class MyApplication : Application() {@Inject@UserModelSingletonQualifierlateinit var singletonUserModel: UserModeloverride fun onCreate() {super.onCreate()  singletonUserModel.value += "MyApplication"}
}
  • In core module

    在核心模块中

@Module
@InstallIn(ApplicationComponent::class)
object CoreModule {@Provides@Singleton@UserModelSingletonQualifierfun provideUserModel() = UserModel(value = "Singleton")
}

Let run app, everything is fine. Code is really very concise.

让我们运行应用程序,一切都很好。 代码真的非常简洁。

Then do the same thing for FeatureActivity in feature module, we will get a exception because Hilt cannot process annotations in feature modules.

然后对功能模块中的FeatureActivity做同样的事情,我们将得到一个例外,因为Hilt无法处理功能模块中的注释。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kienht.dagger.hilt.dfm/com.kienht.dagger.hilt.feature.Feature2Activity}: java.lang.ClassCastException: com.kienht.dagger.hilt.dfm.DaggerMyApplication_HiltComponents_SingletonC$ActivityRetainedCImpl$ActivityCImpl cannot be cast to com.kienht.dagger.hilt.feature.FeatureActivity_GeneratedInjector

We also can find this issue in Dagger repository.

我们也可以在Dagger存储库中找到此问题

To resolve this issue we need to manual providing dependencies.

要解决此问题,我们需要手动提供依赖项。

  • In core module:

    在核心模块中:

We need to expose dependencies for feature module depend on them. And also create a custom SavedStateViewModelFactory to inject anything into ViewModel.

我们需要公开功能模块所依赖的依赖关系。 并且还创建一个自定义SavedStateViewModelFactory,以将任何内容注入ViewModel中。

@Module
@InstallIn(ActivityComponent::class)
object ActivityViewModelModule {@Providesfun provideSavedStateViewModelFactory(application: Application,activity: Activity,viewModelFactories: @JvmSuppressWildcards Map<String, Provider<ViewModelAssistedFactory<out ViewModel>>>,): DFMSavedStateViewModelFactory {val owner = activity as SavedStateRegistryOwnerval defaultArgs = activity.intent?.extrasval delegate = SavedStateViewModelFactory(application, owner, defaultArgs)return DFMSavedStateViewModelFactory(owner, defaultArgs, delegate, viewModelFactories)}
}
  • In feature module

    在功能模块中

class FeatureActivity : AppCompatActivity(R.layout.feature_activity) {@Inject@UserModelSingletonQualifierlateinit var singletonUserModel: UserModel@Injectlateinit var savedStateViewModelFactory: DFMSavedStateViewModelFactoryprivate val featureActivityViewModel by viewModels<FeatureActivityViewModel> { savedStateViewModelFactory }override fun onCreate(savedInstanceState: Bundle?) {inject()super.onCreate(savedInstanceState)Timber.e("singleton userModel = $singletonUserModel")singletonUserModel.value += " => FeatureActivity"Timber.e("userModel = ${featureActivityViewModel.userModel}")}
}

We have done. Hilt is so easy to use. I would like to emphasize again, Hilt is future.

我们做完了。 刀柄非常易于使用。 我想再次强调一下,希尔特是未来

There are some things to note:

有一些注意事项:

  1. Any feature modules need to shared objects in Application scope have to depend on ModuleDependencies. For example, the CoreModuleDependencies, we can expose any objects which are provided through Modules are installed in ApplicationComponent. Then every object needed to inject will be injected from EntryPoint.

    需要在应用程序范围内共享对象的任何功能模块都必须依赖ModuleDependencies 。 例如,对于CoreModuleDependencies ,我们可以公开通过ApplicationComponent安装的模块提供的任何对象。 然后将从EntryPoint注入需要注入的每个对象。

  2. FeatureActivityModule includes the FeatureActivityViewModel_HiltModule which is generated through processing @ViewModelInject annotation by Hilt to provide a FeatureActivityViewModel instance.

    FeatureActivityModule包含FeatureActivityViewModel_HiltModule ,它是通过Hilt处理@ViewModelInject批注以提供FeatureActivityViewModel实例而生成的。

  3. To retrieve a ViewModel instance in feature module we needed a custom ViewModelFactory and a ViewModelStoreOwner. DFMSavedStateViewModelFactory take responsibility for creating and injection and especially for SavedStateHandle. ViewModelStoreOwner depends on determined scope (activity, fragment or navigation graph).

    要在功能模块中检索ViewModel实例,我们需要一个自定义的ViewModelFactory和一个ViewModelStoreOwner。 DFMSavedStateViewModelFactory负责创建和注入,尤其是SavedStateHandle 。 ViewModelStoreOwner取决于确定的范围(活动,片段或导航图)。

More details about dealing Hilt in Fragment to use the shared ViewModel in activity scope or navigation graph scope will be described in this repository. Thanks for reading!

在此存储库中将描述有关在Fragment中处理Hilt以在活动范围或导航图范围中使用共享ViewModel的更多详细信息。 谢谢阅读!

翻译自: https://proandroiddev.com/hilt-and-jetpack-integrations-in-the-dynamic-feature-world-51196e4f31cf

击剑编排软件


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

相关文章:

  • TSA优化算法——模仿航海过程中外套的喷气推进和蜂群行为(Matlab代码实现)
  • 世界上最小的微电子机器人由“喷气发动机”推动
  • 喷气侵蚀测试仪的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  • 一种具有60度机头的喷气式飞翼式飞机
  • html飞机翼布局,2.11 融会贯通:绘制喷气式飞机 - HTML5 Canvas 实战
  • nasa 开源_NASA喷气推进实验室的开源数据驱动发现
  • 2022年全球市场可穿戴喷气背包总体规模、主要生产商、主要地区、产品和应用细分研究报告
  • 喷气式滑雪设备的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  • 返回顶部的喷气火箭
  • UE4喷气背包功能的实现--延续上篇自定义移动组件
  • 走向机器学习的喷气时代
  • 全球与中国喷气发动机燃料市场深度研究分析报告
  • 喷气背包男孩 - Jetpack Boys
  • 喷气大脑(jetBrain)快捷键指南
  • 具有120度大机翼的喷气式飞机
  • android喷气背包游戏截图
  • #161:喷气背包
  • Vue中常见的指令及其含义?
  • vue3系统入门和项目实战,创建vue3项目的步骤
  • vue全家桶学多久能上手项目,vue全家桶插件有哪些
  • vlog能否成为短视频源码的下一个风口?
  • 安利几款简单实用的软件给大家
  • vue怎么用照片做视频,vue可以直接拍照片吗
  • 去掉标题栏和全屏显示
  • vue 音频问题
  • 鸿蒙照搬vue,VUEVlog鸿蒙版
  • 转行游戏建模,是因为游戏建模的薪资符合你期望的收入水平吗?
  • 游戏建模行业市场井喷式爆发,你还在闷头死磕?
  • 3D游戏建模市场井喷式爆发,你还要再等吗?
  • 转行3D游戏建模前一定要思考的3大问题,很多人就这样被耽误了!

击剑编排软件_动态功能世界中的击剑和喷气背包集成相关推荐

  1. 击剑编排软件_击剑和匕首注释备忘单

    击剑编排软件 Hilt defines a standard way to do dependency injection (DI) in your application by providing ...

  2. c# 虚拟机加密软件_在C#中构建一个虚拟软件电话,该软件电话可以在您的呼叫中心中作为振铃组...

    c# 虚拟机加密软件 本文重点介绍C#中环组的开发. 任何呼叫中心的有效性不仅取决于运营商的行为,还取决于呼叫中心的技术背景. 学习完本指南后,您将能够创建振铃组分机(即"虚拟软件电话&qu ...

  3. 我的世界java版如何看坐标_我的世界中怎么查看坐标,坐标系统详解

    本篇教程将通过图文的形式一步步教你在我的世界中查看理解坐标系统(XYZ). 坐标系统解释 我的世界地图有XYZ3个坐标,通过XYZ来显示你所处地图的区域. 下面是每个坐标的详解: X - 显示你在地图 ...

  4. 我的世界java版mac切视角_我的世界中怎么切换视角 大神手把手教学

    导读 本篇教程将通过图文的形式一步步教你在我的世界中怎么在第一人称视角和第三人称视角间切换.在我的世界中,你会以第一人称视角来开始一个新的世界. 我的世界中的视角 在我的世界中有3种不同的视角,让我们 ...

  5. 折纸折痕设计软件_折纸简介中的自适应设计

    折纸折痕设计软件 Lately Origami has been my go-to design tool for prototyping complex touch interactions. I' ...

  6. roku能不能安装软件_如何在Roku中使用Google Assistant

    roku能不能安装软件 As more of our devices connect to each other, it's always nice to know that different pr ...

  7. cad高程如何提取到cass软件_从CAD平面图中提取坐标生成数据表

    功能:在CAD中提取点的三维坐标(提取当前坐标系中坐标)直接生成Excel表,并在CAD图中生成坐标数据表,加了标点号和输出的选项.   如何安装: 1,在CAD平面图中→工具→宏→加载工程→加载下载 ...

  8. 动态域名解析服务器离线会引起什么_动态域名解析过程中可能出现的问题及解决方案...

    动态域名在企业中应用非常广泛,金万维动态域名作为一款平稳运行10余年的软件,已是被业界所熟知.该系统由两部分构成,一部分是客户端,运行在用户的主机上:另一部分是服务器,由金万维负责运行. 谓动态域名解 ...

  9. 分布式链接跟踪服务_微服务世界中的分布式跟踪

    分布式链接跟踪服务 微服务已成为未开发应用程序的默认选择. 毕竟,从业人员认为,微服务提供了完全数字化转型所需的解耦类型,从而使各个团队的创新速度比以往任何时候都快. 微服务只不过是常规的分布式系统, ...

最新文章

  1. bs4库的prettify()方法|粉饰的意思。就是多了换行!
  2. 雅客EXCEL(7)-EXCEL居家常用必备函数(vlookup,IF,AND,OR)
  3. 打造微量元素产业-丰收节交易会·李喜贵:签约南国健康产业
  4. iOS用户设计指南-特别说明
  5. 【推荐一位Python大佬】 从程序员到创业者,再到自由职业
  6. 前端学习(2632):vuex刷新丢失
  7. 使用docker运行dotnetcore站点
  8. CLIP再创辉煌!西南交大MSRA提出CLIP4Clip,进行端到端的视频文本检索!
  9. 流行-Manifold【0】-维基百科中文版本解释
  10. java中文乱码_Java中文乱码问题的解决方案
  11. 用matlab实现傅里叶变换,matlab实现傅里叶变换
  12. OpenModelica使用入门
  13. java pdf添加图片_Java 给 PDF 设置背景图片
  14. 傅里叶入门--动手演示波形叠加
  15. Keil中部分Error Warning解决方法记录
  16. 如何在VC中加减日期及 CTime COleDateTime的常用操作和比较
  17. 注册GitHub时,无法验证您的验证码响应
  18. Nginx根据域名分发不同端口
  19. 创建Android定时器的5种方法
  20. 跳频技术之母--海蒂·拉玛

热门文章

  1. HUAWEI P20为你打造智慧云上生活
  2. android时钟小程序,超详细,用canvas在微信小程序上画时钟教程
  3. 网页优化中,如果图片大小不做定义,则页面需要重新渲染,速度受到影响
  4. U盘恢复出厂 (使用U盘装机大师
  5. iOS compare字符串的比较
  6. 区块链北大肖老师学习笔记4
  7. torch.triu_indices
  8. ETL kettle读取Api接口
  9. 漏洞扫描工具-Nikto
  10. android按钮点击和抬起,android Button 的按下和抬起事件监听