如何使用缺少 Application() class 的 Android 应用程序?

How to use Android applications lacking Application() class?

许多 Google 最近的示例 Android 应用程序,例如 Kotlin Sunflower sample for Jetpack,似乎没有子 class 的文件应用程序()class。他们直接投入到活动中。

这是新的最佳实践吗?

如何在不覆盖应用程序中的 onCreate() 的情况下使用 Dagger2 或 Koin 之类的东西 class?我还没有阅读不涉及对 Application.onCreate().

的一些更改的依赖注入方法

我正在努力学习 Android/Kotlin,我正在使用 Google 的样本作为我的指导。

如果 DI 问题的答案是将 MyApplication.kt 文件添加到缺少文件的项目中,我该怎么做? AndroidManifest.xml 文件是否需要更改或其他任何内容?

或者我应该在 JetPack/Architecture 组件世界中以其他方式进行吗?

如果在其他地方已经提出并回答了这个问题,我们深表歉意。我对此很陌生,甚至不知道缺少 MyApplication 文件的应用程序的术语,所以我很难搜索。

提前致谢

约翰

应用程序 class 用于维护应用程序生命周期的全局状态。即使您不扩展它,基本实现仍在使用。请参阅 the documentation for Application 了解发生了什么。

A lot of Google's recent sample Android apps, such as the Kotlin Sunflower sample for Jetpack, don't seem to have a file that subclasses the Application() class. They just jump straight into the activities.

Is this the new best practice?

如果您不需要应用程序 class,则不必空着。它只会使用框架提供的默认 Application

How to you use things like Dagger2 or Koin without overriding onCreate() in the Application class? I'm yet to read a How To for dependency injection that doesn't involve some change to Application.onCreate().

只需创建一个应用程序class。

I'm trying to learn Android/Kotlin, and I'm using Google's samples as my guide.

If the answer to the DI question is to add a MyApplication.kt file to a project that lacks one, how do I go about that? Does the AndroidManifest.xml file need changing or anything?

您必须将 android:name="fully.qualified.class.name.of.MyApplication" 添加到清单中的 <application> 标签。

Or should I do it some other way in the JetPack/Architecture Components world?

Google推荐Dagger2见https://developer.android.com/jetpack/docs/guide