By Yacine Rezgui

Application startup time is a key indicator of application performance. Once the application is launched, users expect a quick response and content to load, and are disappointed when it doesn’t. This poor experience may result in users giving your app a low rating on the Play Store and not even using it again.

The Jetpack App Startup library initializes components at application Startup in a simple, efficient way. Both library and application developers can use App Startup to simplify the Startup process and explicitly specify the initialization order.

Applications and libraries often need to initialize components, such as WorkManager, ProcessLifecycleObserver, FirebaseApp, etc., before application.onCreate (). This is often done by using contentProviders to initialize different dependencies. With App Startup, instead of defining a separate ContentProvider for each component to initialize, you can define multiple Initializers to share the same ContentProvider. Each loss of a ContentProvider typically generates about 2ms of revenue, which can significantly improve application launch time. App Startup also helps you further improve Startup performance by simplifying delayed initialization of components. When App Startup is in stable release, we will update libraries such as WorkManager and ProcessLifecycle to benefit from it.

App Startup supports API 14 or later.

How to use

Configuration Gradle

To use App Startup in your dependency library or application, you need to add the following dependencies to your Gradle file:

Repositories {Google () Maven ()} dependencies {implementation "Androidx. Startup :startup-runtime:1.0.0"}Copy the code

Definition, Initializer

To use App Startup in your application, you need to define an Initializer. Here you can define how to initialize and specify dependencies on other initializers. This is the interface you need to implement:

interface Initializer<out T: Any> {
    fun create(context: Context): T
    fun dependencies(a): List<Class<out Initializer<*>>>
}
Copy the code

In a real case, Initializer for the WorkManager might look like this:

class WorkManagerInitializer : Initializer<WorkManager> {
    override fun create(context: Context): WorkManager {
        val configuration = Configuration.Builder()
            .setMinimumLoggingLevel(Log.DEBUG)
            .build()
        WorkManager.initialize(context, configuration)
        return WorkManager.getInstance(context)
    }
    // This component does not require any dependencies
    override fun dependencies(a) = emptyList<Class<out Initializer<*>>>()
}
Copy the code

Tip: This example is purely illustrative, and Initializer should actually be defined by the WorkManager dependency library.

Finally, we need to add the WorkManagerInitializer entry to androidmanifest.xml:

<provider
    android:name="androidx.startup.InitializationProvider"
    android:authorities="${applicationId}.androidx-startup"
    android:exported="false"
    tools:node="merge">
    <! -- This entry makes WorkManagerInitializer discoverable. -->
    <meta-data android:name="com.example.WorkManagerInitializer"
          android:value="androidx.startup" />
</provider>
Copy the code

The working principle of

App Startup uses a ContentProvider called InitializationProvider. The ContentProvider finds the Initializer by looking for the entry in the merged AndroidManifest.xml file. This happens before application.oncreate () is called.

After completing the previous phase, all dependencies of the component are loaded before loading it. Therefore, a component can be initialized only after ensuring that all of its dependencies have been initialized.

Lazy initialization

We strongly recommend that you use delayed initialization to further improve startup performance. You can implement delayed initialization for components by adding tools:node=”remove” to Initializer under the

entry, which will disable instant initialization:

<provider
    android:name="androidx.startup.InitializationProvider"
    android:authorities="${applicationId}.androidx-startup"
    android:exported="false"
    tools:node="merge">

    <! -- Disable instant initialization -->
    <meta-data android:name="com.example.WorkManagerInitializer"
              tools:node="remove" />
</provider>
Copy the code

To implement the deferred initialization of WorkManagerInitializer, you can perform the following operations:

/ / returns a WorkManager instance AppInitializer. Here the getInstance (context). InitializeComponent (WorkManagerInitializer. Class);Copy the code

At this point, your application has delayed initializing this component. You can read our detailed documentation.

The last

App Startup is currently in version 1.0.0. You can find more information on how to use it in our documentation. If you have any problems in use, please feedback to us in time to help us better improve.