Startup是什么
目前startup版本:1.0.0-alpha02。不建议现在用。
在应用程序启动时初始化组件简单、高效的方式。显示设置初始化顺序。
允许您定义共享单个内容提供程序组件初始化。不是每个需要初始化的组件定义单独的ContentProvider。显著提高应用程序启动时间。
ContentProvider的onCreate介于Application的attachBaseContext和onCreate之间。App Startup是建立在这个基础上封装。
简单使用
dependencies {
implementation 'androidx.startup:startup-runtime:1.0.0-alpha02'
}
自动初始化
模拟三个库需要初始化SdkA、SdkB、SdkC
class SdkA {
companion object{
fun getInstance():SdkA{
return Instance.instance
}
}
private object Instance{
val instance = SdkA()
}
}
class SdkB {
companion object{
fun getInstance():SdkB{
return Instance.instance
}
}
private object Instance{
val instance = SdkB()
}
}
class SdkC {
companion object{
fun getInstance():SdkC{
return Instance.instance
}
}
private object Instance{
val instance = SdkC()
}
}
创建初始化对象
class SdkAInitializer : Initializer<SdkA> {
companion object{
private const val TAG = "startup-SdkAInit-"
}
override fun create(context: Context): SdkA {
Log.d(TAG, "create: ")
return SdkA.getInstance()
}
override fun dependencies(): MutableList<Class<out Initializer<*>>> {
Log.d(TAG, "dependencies: ")
return Collections.emptyList()
}
}
class SdkBInitializer : Initializer<SdkB>{
companion object{
private const val TAG = "startup-SdkBInit-"
}
override fun create(context: Context): SdkB {
Log.d(TAG, "create: ")
return SdkB.getInstance()
}
override fun dependencies(): MutableList<Class<out Initializer<*>>> {
Log.d(TAG, "dependencies: ")
return mutableListOf(SdkAInitializer::class.java)
}
}
class SdkCInitializer : Initializer<SdkC>{
companion object{
private const val TAG = "startup-SdkCInit-"
}
override fun create(context: Context): SdkC {
Log.d(TAG, "create: ")
return SdkC.getInstance()
}
override fun dependencies(): MutableList<Class<out Initializer<*>>> {
Log.d(TAG, "dependencies: ")
return mutableListOf(SdkBInitializer::class.java)
}
}
oncreate方法进行对象初始化;dependencies 方法添加依赖关系;这里是C-->B-->A模拟的依赖关系
然后在清单文件配置InitializationProvider
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
<!-- 自动初始化-->
<meta-data
android:name="com.example.myapplication.startup.SdkCInitializer"
android:value="androidx.startup" />
</provider>
手动初始化
创建SdkD ,SdkDInitializer
class SdkD {
companion object{
fun getInstance():SdkD{
return Instance.instance
}
}
private object Instance{
val instance = SdkD()
}
}
class SdkDInitializer : Initializer<SdkD>{
companion object{
private const val TAG = "startup-SdkDInit"
}
override fun create(context: Context): SdkD {
Log.d(TAG, "create: ")
return SdkD.getInstance()
}
override fun dependencies(): MutableList<Class<out Initializer<*>>> {
Log.d(TAG, "dependencies: ")
return Collections.emptyList()
}
}
手动初始化清单配置
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
<!-- 自动初始化-->
<meta-data
android:name="com.example.myapplication.startup.SdkCInitializer"
android:value="androidx.startup" />
<!-- 手动初始化-->
<meta-data
android:name="com.example.myapplication.startup.SdkDInitializer"
android:value="androidx.startup"
tools:node="remove" />
</provider>
在需要的地方手动初始化调用
AppInitializer.getInstance(this).initializeComponent(SdkDInitializer::class.java)
总结
App Startup提供了一个ContentProvider来运行的初始化,避免写在Application中,也避免每个库单独使用ContentProvider(Leack Canary2等第三方库)耗时问题。可以自定义初始化顺序,每个初始化依赖都要实现Initializer接口,会导致文件增多。目前还处于alpha版本。不建议现在使用到项目中。