SDK initializes without intrusion and obtains Application

Get into the habit of writing together! This is the 10th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

1.SDK initializes without intrusion and obtains Application

无侵入初始化SDK并获取ApplicationIt means that the business party does not need to manually call the initialization function of the SDK.

This has to use one of the four basic components of Android . The timing of ContentProviderits execution is located after and before , and there is no need to manually call the program.ApplicationattchBaseContext()ApplicationonCreate()

So we can customize ContentProviderthe application to complete the automatic initialization of the SDK and obtain the application.

class CPDemo : ContentProvider() {
    override fun attachInfo(context: Context?, info: ProviderInfo?) {
        super.attachInfo(context, info)
        //编写SDK初始化逻辑,并获取Application
        val application = context?.applicationContext
    }

    override fun onCreate(): Boolean = true
}
复制代码

Simply rewrite ContentProviderand attachInfoexecute the initialization logic of the SDK.

The well-known memory leak detection library LeakCanary, Google's official one ProcessLifecycleOwner, uses this principle.

However, if every third-party library is borrowed ContentProviderto complete non-intrusive initialization, it will inevitably cause too much customization ContentProvider, which directly increases the startup time:

image.png

In order to avoid ContentProvidertoo many problems, Google officially provides a App Startuplibrary, which is mainly used by SDK providers to achieve non-invasive initialization:

    implementation("androidx.startup:startup-runtime:1.1.1")
复制代码

该官方库会将所有用于初始化的ContentProvider合并成一个,减少启动的耗时

The basic usage is as follows:

class CPDemo2 : Initializer<Unit> {
    override fun create(context: Context) {
        //执行初始化逻辑
    }

    override fun dependencies(): MutableList<Class<out Initializer<*>>> = mutableListOf()
}
复制代码

Then AndroidManifestregister again:

<provider
    android:name="androidx.startup.InitializationProvider"
    android:authorities="${applicationId}.androidx-startup"
    android:exported="false"
    tools:node="merge">
    <meta-data
        android:name="com.example.gitlinux.CPDemo2"
        android:value="androidx.startup" />
</provider>
复制代码

For more usage, please refer to Guo Shen's article: New members of Jetpack, you can understand it in one App Startup

2. Is it really good for kotlin functions to omit the return value type?

Programs that often use kotlin know that in some scenarios, kotlin functions do not need to explicitly declare the return value type, which is to improve development efficiency, such as:

fun test() = ""
复制代码

For simple functions, although the return type of the method is omitted, we can still directly see that the return value type of this method is String, but other methods are called in the method, such as:

fun test() =  request()

//随便一个函数,这个函数体中还会调用其他的函数
fun request() = otherFun()

fun otherFun() = "hahaha"
复制代码

In this case, if we want to know test()the return value type of the method, we must first pass the request()function and then jump to the method otherFunto know test()the return value type of the method, which reduces the development efficiency for the program.

So I think the scenario of using kotlin functions to omit the return value type should have a premise:该函数的返回值类型程序能够很容易推断出来(尽量不依赖其他函数)

Reference article

A new member of Jetpack, you can understand it in one App Startup

Guess you like

Origin juejin.im/post/7085122222058111012