Android Jetpack 之 App Startup

       We usually introduce more or less third-party SDKs in project development. Basically, they need to be initialized when they are introduced. Most of the SDK initializations are initialized in Application's onCreate(), such as the following:

class MyApplication:Application() {

    override fun onCreate() {
        super.onCreate()
        MySdk().init(this)
        MySdk1().init(this)
        MySdk2().init(this)
        MySdk3().init(this)
        Log.d("MyTest", "MyApplication  create")
    }

}

 

In this way, with the development of the project, more and more third-party SDKs will be introduced, which will make the initialization code in the Application very bloated, and too much will affect the startup efficiency of the entire App. Is there a solution that can make the Application less bloated and improve the startup efficiency of the App?

Have

Jetpack's AppStartup

 

How to use it? It's simple, just three steps

1. Introduce dependencies

2. Implement the Initializer interface

3. Registration in AndroidManifest.xml

 

1. Introduce dependency

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

[ 2. Implement the Initializer interface ]

class SdkInitializer : Initializer<Any> {

    override fun create(context: Context) {
        // init your sdk
        MySdk().init(context)
        Log.d("MyTest", "Initializer  create")
    }

    override fun dependencies(): List<Class<out Initializer<*>>> {
        Log.d("MyTest", "Initializer  dependencies")
        return emptyList()
    }

}

[ 3. Registration in AndroidManifest.xml ]

<provider
            android:name="androidx.startup.InitializationProvider"
            android:authorities="${applicationId}.androidx-startup"
            android:exported="false"
            tools:node="merge">
            <meta-data
                android:name="com.leo.dicaprio.myutilapp.startup.SdkInitializer"
                android:value="androidx.startup" />
</provider>

The main point here is that only the meta-data-name can be changed for the registered content of AndroidManifest.xml. It is best not to change other places, nor does it need to be changed.

 

It's that simple. Put all the SDKs that need to be initialized in the create() and dependencies() interfaces of Initializer. Which interface should you put in?

If the sdk1 you introduced does not need to depend on other sdk2, you only need to put sdk1 into create() for initialization, as follows:

class SdkInitializer : Initializer<Any> {

    override fun create(context: Context) {
        // init your sdk
        MySdk1().init(context)
        Log.d("MyTest", "Initializer  create")
    }

    override fun dependencies(): List<Class<out Initializer<*>>> {
        Log.d("MyTest", "Initializer  dependencies")
        // 如果没有需要初始化的sdk,返回 空列表即可
        return emptyList()
    }

}

 

If the sdk1 you imported needs to depend on other sdk2, then you need to put sdk2 into dependencies() , and then put sdk1 into create() for initialization,

Because dependencies()  precedes create()   callback, ensure that sdk2 is initialized first, as follows:

SdkInitializer

class SdkInitializer : Initializer<Any> {

    override fun create(context: Context) {
        // init your sdk
        MySdk().init(context)
        Log.d("MyTest", "Initializer  create")
    }

    override fun dependencies(): List<Class<out Initializer<*>>> {
        Log.d("MyTest", "Initializer  dependencies")
        return listOf(Sdk2Initializer::class.java)
    }

}

 

SdkInitializer2

class Sdk2Initializer : Initializer<Any> {

    override fun create(context: Context) {
        // init your sdk
        MySdk2().init(context)
        Log.d("MyTest", "Initializer  create2")
    }

    override fun dependencies(): List<Class<out Initializer<*>>> {
        Log.d("MyTest", "Initializer  dependencies2")
        return emptyList()
    }

}

 

Note that only one Initializer needs to be registered, Sdk2Initializer does not need to be registered in AndroidManifest.xml, otherwise an error will be reported...

 

Start the application, we look at the log:

/MyTest: Initializer  dependencies
/MyTest: Initializer  dependencies2
/MyTest: Initializer  create2
/MyTest: Initializer  create

Look at the log available: first initialize sdk2, then initialize sdk1

 

Principle

The above just talked about how to use it, but why can it improve startup efficiency? First look at the registration of Initializer in AndroidManifest.xml,

        <provider
            android:name="androidx.startup.InitializationProvider"
            android:authorities="${applicationId}.androidx-startup"
            android:exported="false"
            tools:node="merge">
            <meta-data
                android:name="com.leo.dicaprio.myutilapp.startup.SdkInitializer"
                android:value="androidx.startup" />
        </provider>

In fact, Initializer is a ContentProvider. The execution of ContentProvider precedes Application, and the process is as follows:

 

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

 

We moved the initialization of the SDK to the ContentProvider before Application. This only avoided the bloated Application code, and did not see any improvement in efficiency, because how much time should be spent to initialize, and how much time should be spent.

 

However, do you remember? As mentioned above, only one Initializer needs to be registered, and multiple Initializers are not required and cannot be registered. In fact, the reason why AppStartup can improve efficiency is precisely here. No matter how many SDKs you need to initialize, you only need to register an Initializer (ie: a ContentProvider).

Quite simply, the more ContentProviders registered, the more time it takes for ContentProvider to start. But registering only one ContentProvider can certainly improve efficiency.

 

Please leave a message if you have any questions above, thank you

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/Leo_Liang_jie/article/details/108404923