AndroidX App Startup is a new addition to the Jetpack suite of libraries. This article takes an initial look at it, and focuses on 3 things: Why it was created, what it is, and how you can get started using it.
AndroidX App Startup是Jetpack库套件的新增功能。 本文对它进行了初步研究,并着重于三件事:创建它的原因,它是什么以及如何开始使用它。
为什么要启动AndroidX App? (Why AndroidX App Startup?)
It isn’t uncommon for libraries to run some initialization logic on app startup. Libraries such as WorkManager and Lifecycle run this initialization code on app startup using a ContentProvider
, which allows to automatically run the initialization once the app goes through a cold start before Application.onCreate()
is invoked.
库在应用程序启动时运行一些初始化逻辑的情况并不少见。 诸如WorkManager和Lifecycle之类的库在使用ContentProvider
应用程序启动时运行此初始化代码,当应用程序经过冷启动后,在调用Application.onCreate()
之前,它可以自动运行初始化。
Users expect apps to be fast to load, an app that’s slow to start provides a bad user experience. Having multiple ContentProvider
s run on the app startup slows down the load time of the app. Hence the AndroidX App Startup library!
用户期望应用程序能够快速加载,而启动缓慢的应用程序会带来糟糕的用户体验。 在应用启动时运行多个ContentProvider
会减慢应用的加载时间。 因此,AndroidX App启动库!
什么是AndroidX应用启动? (What Is AndroidX App Startup?)
AndroidX App Startup is a recent addition to the Android Jetpack suite of libraries. It provides a performant* way to initialize components at app startup by avoiding the use of a separate ContentProvider
for each library, thus eliminating the setup cost that comes with each of them. AndroidX App Startup uses a single ContentProvider
to run the initialization of all the dependencies.
AndroidX App Startup是Android Jetpack库套件的最新添加。 通过避免对每个库使用单独的ContentProvider
,它提供了一种在应用程序启动时初始化组件的Performance *方法,从而消除了每个组件附带的安装成本。 AndroidX App Startup使用单个ContentProvider
来运行所有依赖项的初始化。
![Image for post](https://img-service.csdnimg.cn/img_convert/f728ac888f2796a73256f04606c1c350.png)
![Image for post](https://img-service.csdnimg.cn/img_convert/6ab45347f4e8de620565e8493ed51cd6.png)
如何使用AndroidX应用启动? (How To Use AndroidX App Startup?)
依赖导入 (Dependency Import)
Add the following dependency to your build.gradle
file.
将以下依赖项添加到build.gradle
文件中。
implementation "androidx.startup:startup-runtime:1.0.0-alpha01"
组件初始化 (Component Initialization)
To initialize a component, implement Initializer
.
要初始化组件,请实现Initializer
。
class MyInitializer: Initializer<MyDependency> {
override fun create(context: Context): MyDependency {
val myDependency: MyDependency = /* Run initialization/configuration logic */
return myDependency
}
override fun dependencies(): List<Class<out Initializer<*>>> {
return listOf(AnotherDependencyInitializer::class.java,
YetAnotherDependencyInitializer::class.java)
}
}
When defining your Initializer
, you’ll have to implement 2 methods:
定义Initializer
,您必须实现2种方法:
create(Context)
: Provides the applicationContext
, and is expected to return an instance ofMyDependency
, which represents the class thisInitializer
is setting up.create(Context)
:提供应用程序Context
,并期望返回MyDependency
的实例,该实例表示此Initializer
设置的类。dependencies()
: Returns a list of dependencies required to be initialized before the current dependency can be initialized. This sets the order theInitializer
s are run at app startup. IfInitializerA
depends onInitializerB
, andInitializerB
depends onInitializerC
, thenInitializerC
is run first, followed byInitializerB
and finallyInitializerA
.dependencies()
:返回可以初始化当前依赖项之前需要初始化的依赖项列表。 这设置了Initializer
程序在应用启动时运行的顺序。 如果InitializerA
取决于InitializerB
,并InitializerB
取决于InitializerC
,然后InitializerC
首先运行,其次是InitializerB
终于InitializerA
。
Let’s see an example of this. Assume you’re building a face detection app, where the user sees a camera preview, and boxes are drawn around detected faces. Your Initializers may look like this.
让我们来看一个例子。 假设您正在构建一个面部检测应用程序,用户可以在其中查看相机预览,并在检测到的面部周围绘制方框。 您的初始化器可能看起来像这样。
class CameraInitializer : Initializer<Camera> {
override fun create(context: Context): Camera {
val camera = /* Set up, configure and/or initialize the camera */
return camera
}
override fun dependencies() = emptyList<>()
}
class FaceDetectorInitializer : Initializer<FaceDetector> {
override fun create(context: Context): FaceDetector {
val faceDetector = /* Set up and configure the face detector */
return faceDetector
}
override fun dependencies() = listOf(CameraInitializer::class.java)
}
The CameraInitializer
sets up the camera. If you’re using the CameraX library for instance, this can include setting up a custom camera configuration using CameraXConfig
.
CameraInitializer
设置相机。 例如,如果您使用CameraX库,则可以包括使用CameraXConfig
设置自定义相机配置。
The FaceDetectorInitializer
sets up the face detector, and requires the camera to have been initialized first. This may be because the face detector requires some information about the device’s camera characteristics to properly initialize.
FaceDetectorInitializer
设置人脸检测器,并要求首先将相机初始化。 这可能是因为面部检测器需要一些有关设备相机特性的信息才能正确初始化。
When the app first loads, App Startup initializes CameraInitializer
before FaceDetectorInitializer
.
当应用首次加载时,应用启动CameraInitializer
在FaceDetectorInitializer
之前初始化CameraInitializer
。
组件自动初始化 (Component Automatic Initialization)
For a component to be automatically initialized at app startup, declare its Initializer
in the manifest file in a <meta-data>
entry.
对于要在应用程序启动时自动初始化的组件,请在清单文件中的<meta-data>
条目中声明其Initializer
程序。
<manifest>
<application>
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
<!-- Automatically run MyInitializer at app startup -->
<meta-data
android:name="com.example.MyInitializer"
android:value="androidx.startup" />
</provider>
</application>
</manifest>
App Startup is able to automatically detect components to initialize once the app loads by reading the <meta-data>
entries under the InitializationProvider
manifest entry.
通过读取InitializationProvider
清单条目下的<meta-data>
条目,App Startup能够自动检测要加载应用程序的组件。
Only high-level Initializer
s have to be declared this way for automatic initialization. If InitializerA
depends on InitializerB
, only declaring a <meta-data>
entry for InitializerA
in the manifest in enough to automatically initialize both components.
对于自动初始化,只需要以这种方式声明高级Initializer
。 如果InitializerA
取决于InitializerB
,只声明一个<meta-data>
入境InitializerA
在清单中足以自动初始化两种组分。
Going back to the face detection app example from the previous section, you’d only need to declare FaceDetectorInitializer
in your app’s manifest file for both FaceDetectorInitializer
and CameraInitializer
to automatically be run on app startup.
回到上一部分的人脸检测应用程序示例,您只需在应用程序的清单文件中声明FaceDetectorInitializer
, FaceDetectorInitializer
和CameraInitializer
即可在应用程序启动时自动运行。
<manifest>
<application>
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
<!-- Automatically runs FaceDetectorInitializer and CameraInitializer at app startup -->
<meta-data
android:name="com.example.FaceDetectorInitializer"
android:value="androidx.startup" />
</provider>
</application>
</manifest>
组件延迟初始化 (Component Lazy Initialization)
Lazy initialization is useful in scenarios where a component’s initialization doesn’t have to necessarily run on app startup, for example when the component isn’t necessary. A social media app that requires users to verify their identity by taking a selfie doesn’t necessarily need to initialize the camera on app startup since the camera isn’t used frequently on the app.
惰性初始化在不必在应用程序启动时一定要进行组件初始化的情况下很有用,例如在不需要该组件时。 要求用户通过自拍来验证自己身份的社交媒体应用程序不一定需要在应用程序启动时初始化摄像头,因为该应用程序并不经常使用该摄像头。
Another reason to disable automatic initialization is when the initialization cost is too expensive and might significantly increase the app startup time. In this scenario, initializing the component the first time it’s needed would make for a better user experience.
禁用自动初始化的另一个原因是,初始化成本太高,可能会大大增加应用程序的启动时间。 在这种情况下,在第一次需要组件时进行初始化会带来更好的用户体验。
To prevent App Startup from automatically initializing a component, you can abstain from adding its Initializer
’s <meta-data>
manifest entry (or removing it if you’ve already added it).
为防止App Startup自动初始化组件,您可以放弃添加其Initializer
的<meta-data>
清单条目(如果已添加,则将其删除)。
You can also use tools:node="remove"
to make sure the manifest merger tool removes it from all other merged manifest files. This can be used for instance to disable automatic initialization for a component in one of the dependencies of your app (which actually seems like a risky thing to do).
您还可以使用tools:node="remove"
来确保清单合并工具将其从所有其他合并的清单文件中删除。 例如,可以使用它来禁用应用程序依赖项之一中的组件的自动初始化(这实际上似乎很冒险)。
<manifest>
<application>
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
<!-- Manually/Lazily run MyInitializer -->
<meta-data
android:name="com.example.MyInitializer"
tools:node="remove" />
</provider>
</application>
</manifest>
To manually initialize a component (and its dependencies), use AppInitializer
.
要手动初始化组件(及其依赖项),请使用AppInitializer
。
AppInitializer.getInstance(context).initializeComponent(MyInitializer::class.java)
Once a component is initialized, manually initializing it again by calling AppInitializer.initializeComponent()
doesn’t run the initialization logic again.
初始化组件后,通过调用AppInitializer.initializeComponent()
手动将其再次初始化AppInitializer.initializeComponent()
不会再次运行初始化逻辑。
结论 (Conclusion)
In summary:
综上所述:
App Startup is a Jetpack library that provides a way to initialize components at app startup using a single
ContentProvider
.App Startup是Jetpack库,它提供了一种使用单个
ContentProvider
在应用程序启动时初始化组件的方法。App Startup automatically initializes components (and their dependencies) declared in the manifest file under the
InitializationProvider
entry.App Startup会自动初始化清单文件中
InitializationProvider
条目下声明的组件(及其依赖项)。- App Startup allows controlling the order in which components are initialized. App Startup允许控制组件初始化的顺序。
- App Startup provides a way to lazily initialize components. 应用启动提供了一种延迟初始化组件的方法。
Want to learn more about app startup in Android? Check out:
想更多地了解Android中的应用启动? 退房:
翻译自: https://proandroiddev.com/androidx-app-startup-698855342f80