Android App Startup

Tram Ho

Jetpack recently introduced a new library, AndroidX App Startup, to use to initialize components when launching applications in a simple and effective way. This article we will learn together about this library.

1. Why use the AndroidX App Startup

Normally, in an Android application, it is possible to use some libraries that run the initialization logic when starting the application. For example, WorkManager and Lifecycle are initialized when the application is run. Users always want to initialize these components as quickly as possible, but sometimes having multiple ContentProvider launched can slow down the initialization time of the application, making the user experience will not be good. . So App Startup was created to overcome this situation by instead of declaring separate ContentProvider for individual components at initialization, App Startup allows you to define a single ContentProvider for all of the components. That will greatly improve the application startup time.

2. How to use it

To use App Startup in the application, add the following setting to the gradle file:

2.1. Initialization

Create class implement Initializer

For example, the application is dependent on WorkManager and needs to be initialized to run the application. Declare a class WorkManagerInitializer implement Initializer

  • The create () method, containing all the work needed to initialize the component and return 1 instance T
  • method dependencies () return 1 list of other Initializers on which the initializer currently depends. Use this method to control the initialization order of the components when the application launches

The dependencies () return 1 list is empty because WorkManager does not depend on any other library.

For example, there is another library called ExampleLogger, it depends on WorkManager. That is, you need to initialize WorkManager first, then initialize ExampleLogger.

2.2. Set up manifest entries

App Startup has a content provider that InitializationProvider uses to declare in AndroidManifest the initializer component. You need to declare the initializer in the meta-data tag pair in the InitializationProvider entry. App Startup will then call the method dependencies () and get the components in it to initialize.

You do not need to add meta-data tag pairs for WorkManagerInitializer because WorkManagerInitializer is a dependency of ExampleLoggerInitializer. That is when initializing ExampleLoggerInitializer, WorkManagerInitializer has also been initialized already.

The tools: node = “merge” attribute ensures that the manifest merger resolves conflicts correctly.

Run lint check

The App Startup Library includes a set of lint rules that you can use to check that you have correctly identified your component initialization tools. You can perform these lint checks by running ./gradlew: app: lintDebug from the command line.

2.3. Self-run initialization component

Normally when you use App Startup, the InitializationProvider object uses an entity named AppInitializer to automatically run initialization components when starting the application. However, you can also use AppInitializer directly to manually initialize components that your application does not need at startup. This is called lazy initialization and it can help minimize startup.

First, you must turn off automatic initialization for any component you want to manually initialize.

2.3.1. Disable automatic initialization with 1 independent component

Use tools: node = “remove”

Note: when auto-initialization is disabled, auto-initialization for dependent components is also used

2.3.2. Turn off auto-initialization for all components

If auto-initialization is disabled for a component, you can use AppInitializer to initialize the component itself and its dependencies.

Refer

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

Share the news now

Source : Viblo