Writing in the front

When looking at Jetpack’s official website, I found that a new App Startup component was added to Jetpack. I checked that it was updated together with Hilt and Paging 3 a few days ago, but I haven’t looked at other components yet.

The official website: https://developer.android.com/topic/libraries/app-startup

To choose ENGLISH oh, Chinese version does not have this page.

Why App Startup?

In our actual development work it is not uncommon for some third party libraries to need to be initialized at App startup, such as WorkManager and LifeCycle via ContentProvider at App startup. With ContentProvider, once the App is cold started, the ContentProvider automatically performs initialization before calling Application.onCreate().

What is App Startup?

App Startup The App Startup library provides a direct and efficient way to initialize components at Startup. All Library and app developers can use the App Startup component to simplify the Startup order and explicitly set the initialization order. With App Startup, you don’t need to define a single content provider for each component that needs to be initialized. Instead, you can share a content provider to define a component initializer to speed up the Startup of your application.

App Startup provides a ContentProvider for initializing components of a project, so that each third-party library is not initialized by a separate ContentProvider.

With App Startup, we can initialize all libraries that need to be initialized by the third party in the Application through ContentProvider. It’s a bit of a wrapper around the initialization process of the third party library.

How to use App Startup?

We can initialize a component by defining a component initializer. How do we define a component initializer? The Initializer

interface is provided by Android. You can implement the component Initializer by implementing the interface and two methods in the interface.

Let’s take a look at these two methods:

  • Create () : contains all necessary operations to initialize the component and return an instance of T;

  • Dependencies () : This method returns a list of other Initializer objects that the Initializer depends on. You can use this method to control the order in which the application is initialized when it starts.

How to ensure the initialization order of content providers when not using App Startup? In the configuration manifest, it is easy to put the

tag of the content Provider that starts first.

So let’s look at how to initialize.

There are two ways to run dependency initialization via App Startup:

  • Automatic initialization

  • Manually initialization

Either automatic initialization or manual initialization needs to be in the app or librarybuild.gradleAdd the following dependencies to the file:

Implement automatic initialization (take the official example)

Suppose your application depends onWorkManagerAnd needs to be initialized as soon as the program startsWorkManager, define aWorkManagerInitializerClass and implementInitializer<WorkManager>Interface:

As shown in the picture,dependencies()Method returns an empty list, meaning IWorkManagerI don’t need to rely on anyone to instantiate, I can do it myself. Let’s say our application relies on another one calledExampleLoggerThe library that depends on theWorkManager. This means that the initialization of the library must be ensuredWorkManagerHas already been initialized. So how do you do that? Let’s look at the official code:

The ExampleLoggerInitializer class is defined and the Initializer

interface is implemented. The Dependencies () method returns a list containing the WorkManagerInitializer, so ExampleLogger must initialize the WorkManager before it is initialized.

Tip: If you have previously used content providers to initialize components in your App, make sure to delete those content providers when using App Startup

App Startup includes a special Content Provider called InitializationProvider, which is used to find and invoke your component initializer. So what does the process look like?

  • First, pass the inspectionInitializationProviderUnder the list tag<meta-data>Tag, locate the component initializer;
  • App Startup calls all the component initializers it findsdependencies()Methods.

This means that in order for App Startup to find the component initializer, one of the following conditions must be met:

  • The component initializer is inInitializationProviderThe corresponding is configured under the manifest TAB<meta-data>The label;
  • Component initializer in a component initializer that has been founddependencies()Is listed in the method;

From what we wrote aboveWorkManagerInitializerandExampleLoggerInitializerIn both cases, to ensure that the initializer is implemented, you need to configure the following code in the manifest file:

As you can see in the figure, we only configured the

tag of ExampleLoggerInitializer, because it is dependent on the WorkManager and satisfies article 2: The dependencies of ExampleLoggerInitializer are listed in the Dependencies () method. If ExampleLoggerInitializer can be found, then WorkManagerInitializer must also be found.

Tools :node=” Merge “property is to ensure that the list merge tool may cause conflicts

The App Startup library contains a set of Lint rules that you can use to check that a component initializer is properly defined. You can perform lint checks on terminals by running the./gradlew :app:lintDebug command.

Implement manual initialization

In general, when you use App Startup,InitializationProviderObject will be usedAppInitializerAutomatically find and run initializers at App startup. However, you can also call it directly.AppInitializerTo manually initialize component initializers that do not need to be called at startup. This operation is called lazy initialization, and it reduces the time it takes to start a program. To implement manual initialization, you must first disable automatic initialization for the component you want to manually initialize. To disable automatic initialization for a single component, you can remove that component’s<meta-data>Labels, for example, we want to banExampleLoggerAutomatic initialization of:

Using tools:node=”remove” instead of removing the tag directly ensures that the manifest merge tool removes the tag from all merged files.

Example: If I disable ExampleLogger and ExampleLogger depends on the WorkManager, the WorkManager will not automatically initialize ExampleLogger.

Now that we know how to disable automatic initialization for individual components, how do we disable automatic initialization for all components and switch to manual initialization? Android provides us with this notation:

Disable auto initialization after you can use itAppInitializerManually initialize the component and its dependencies. Take a look at the official code:

With the above code, App Startup also initializes the WorkManager because ExampleLogger relies on the WorkManager.

Source code analysis

The App Startup package doesn’t have much code, just five classes


One of the core classes is InitializationProvider, which inherits ContentProvider, so we can see that in the onCreate() method, It calls the discoverAndInitialize() method of the AppInitializer class. Take a quick look at this code:


The code is simple: parse the metadata, iterate through the metadata to get the configured initializer, and call each initializer’s initialization method, doInitialize(). Now look at this method:


It can be seen that during initialization, the dependency is checked first, and if so, the dependency is initialized first.

The resources

  • https://developer.android.com/topic/libraries/app-startup
  • https://proandroiddev.com/androidx-app-startup-698855342f80

Write in the back

So much for App Startup. If you’re using Jetpack, try this component out.

If the article is helpful to you, welcome to forward, pay attention to, collect, look at oh