Introduction to the use of Android Jetpack's App Startup library

Introduction to the use of Jetpack's App Startup library

Introduction to App Startup Library

The App Startup library provides a direct and efficient way to initialize components when the application starts. Both library developers and application developers can use "application startup" to simplify the startup sequence and explicitly set the initialization sequence.

App Startup allows you to define component initializers that share a single content provider without having to define a separate content provider for each component that needs to be initialized. This can greatly shorten the startup time of the application.

Implementation principle

Insert picture description here

As you can see, the order of execution of an application is like this. First call the attachBaseContext() method of Application, then call the onCreate() method of ContentProvider, and then call the onCreate() method of Application

App Startup also creates a ContentProvider inside and provides a set of standards for initialization . Then for other third-party libraries , you don’t need to create ContentProvider yourself anymore , just implement it according to the standard provided by App Startup. App Startup can ensure that our library is successfully initialized before App starts.

solved problem

  • Solved the problem that the code in Application is very messy after introducing a lot of third-party libraries

  • The developer of the library does not need to use ContentProvider to initialize the library in their own library, thereby improving the startup time of the application

  • Let the initialization of the library complete the initialization work before the onCreate() method of Application

.

Use of App Startup Library

1. Add dependency in build.gradle file

dependencies {
    
    
    implementation "androidx.startup:startup-runtime:1.0.0"
}

2. Implement the Initializer interface of the App Startup library

class MyInitializer implements Initializer<Unit> {
    
    

    @Override
	//进行库的初始化操作
    public MyInitializer create(Context context) {
    
    

       //在这里进行第三方库的初始化
		XXXX.initialize(context)
    }

    @Override
	//表示当前要初始化的库是否还依赖于其他的Initializer,如果有的话,就在这里进行配置
    public List<Class<Initializer<?>>> dependencies() {
    
    
        // 当前库无第三方库依赖
        return emptyList();//返回一个看的集合
    }

}

3. Configure the defined Initializer to AndroidManifest.xml

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

	    <meta-data  android:name="com.myclass.MyInitializer"
	          android:value="androidx.startup" />
</provider>

Note: Only the android:name part of the meta-data we need to specify the full path class name of our custom Initializer, the other parts cannot be modified, otherwise the App Startup library may not work properly.

At this point, the use of the App Startup library is basically over. Let's introduce any manual initialization components.

.

Manually initialize third-party libraries (lazy initialization)

1. Disable automatic initialization of a single library

To disable the automatic initialization of a single library <meta-data>, delete the entry for that library’s initializer from the list

<provider
    android:name="androidx.startup.InitializationProvider"
    android:authorities="${applicationId}.androidx-startup"
    android:exported="false"
    tools:node="merge">
    <meta-data android:name="com.myclass.MyInitializer"
              tools:node="remove" />
</provider>

Manually initialize when you need to initialize, as shown below:

AppInitializer.getInstance(context)
    .initializeComponent(MyInitializer.class);

2. Disable automatic initialization of all libraries

To disable all automatic initialization, InitializationProviderdelete the entire entry from the list

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

Manually initialize when you need to initialize, as shown below:

AppInitializer.getInstance(context)
    .initializeComponent(MyInitializer.class);

This is the end of the usage and function introduction of App Startup library. For more details, please check the official website of App Startup library. Thank you for reading!

Guess you like

Origin blog.csdn.net/weixin_42324979/article/details/110524423