Android Jetpack 之 App Startup,卧槽
作者:互联网
*/
@NonNull
T create(@NonNull Context context);
/**
-
@return A list of dependencies that this {@link Initializer} depends on. This is
-
used to determine initialization order of {@link Initializer}s.
-
-
For e.g. if a {@link Initializer}
B
defines another -
{@link Initializer}
A
as its dependency, thenA
gets initialized beforeB
.
*/
@NonNull
List<Class<? extends Initializer<?>>> dependencies();
}
create 方法用于进行对象的初始化;
dependencies 方法用于定义需要在当前对象初始化之前进行初始化的对象对应的 Initializer。
SdkA 的对应的 SdkAInitializer:(SdkBInitializer、SdkCInitializer的code和SdkAInitializer的结构 一模一样)
class SdkAInitializer :Initializer{
val TAG = “SdkInitializer”
override fun create(context: Context): SdkA {
Log.d(TAG, "SdkA create: ")
return SdkA.getInstance()
}
override fun dependencies(): MutableList<Class<out Initializer<*>>> {
Log.d(TAG, "SdkA dependencies: ")
//SdkA 的初始化不需要其他库的依赖,所以这里 dependencies 方法返回一个空列表。
return Collections.emptyList()
}
}
接着在 manifest 中定义 InitializationProvider:
<provider
android:name=“androidx.startup.InitializationProvider”
android:authorities="${applicationId}.androidx-startup"
android:exported=“false”
tools:node=“merge”>
<meta-data
android:name=“com.noway.startup.SdkAInitializer”
android:value=“androidx.startup” />
<meta-data
android:name=“com.noway.startup.SdkBInitializer”
android:value=“androidx.startup” />
<meta-data
android:name=“com.noway.startup.SdkCInitializer”
android:value=“androidx.startup” />
run App 查看log:在App create之前SdkA、SdkB、SdkC依次先dependencies后create
2020-10-10 10:04:07.894 20431-20431/com.noway.startup D/SdkInitializer: App attachBaseContext:
2020-10-10 10:04:07.901 20431-20431/com.noway.startup D/SdkInitializer: SdkA dependencies:
2020-10-10 10:04:07.901 20431-20431/com.noway.startup D/SdkInitializer: SdkA create:
2020-10-10 10:04:07.902 20431-20431/com.noway.startup D/SdkInitializer: SdkB dependencies:
2020-10-10 10:04:07.902 20431-20431/com.noway.startup D/SdkInitializer: SdkB create:
2020-10-10 10:04:07.902 20431-20431/com.noway.startup D/SdkInitializer: SdkC dependencies:
2020-10-10 10:04:07.902 20431-20431/com.noway.startup D/SdkInitializer: SdkC create:
2020-10-10 10:04:07.904 20431-20431/com.noway.startup D/SdkInitializer: App onCreate:
2020-10-10 10:04:08.117 20431-20431/com.noway.startup D/SdkInitializer: MainActivity onCreate:
从log可以看出,dependencies 方法的执行顺序是 A→B→C,create 方法的执行顺序是 A → B → C。这里的 create 方法的顺序正是对象创建的顺序。
App Startup 的互相依赖使用:
====================
假设SdkB初始化需要依赖SdkC,需要先让SdkC初始化,只需要修改SdkB的code,把返回的空集合改为SdkC
class SdkBInitializer :Initializer{
val TAG = “SdkInitializer”
override fun create(context: Context): SdkB {
Log.d(TAG, "SdkB create: ")
return SdkB.getInstance()
}
override fun dependencies(): MutableList<Class<out Initializer<*>>> {
Log.d(TAG, "SdkB dependencies: ")
//SdkB 依赖 SdkC
return mutableListOf(SdkCInitializer::class.java)
// return Collections.emptyList()
}
}
run App 查看log:
2020-10-10 10:12:21.233 20922-20922/com.noway.startup D/SdkInitializer: App attachBaseContext:
2020-10-10 10:12:21.239 20922-20922/com.noway.startup D/SdkInitializer: SdkA dependencies:
2020-10-10 10:12:21.239 20922-20922/com.noway.startup D/SdkInitializer: SdkA create:
-
2020-10-10 10:12:21.239 20922-20922/com.noway.startup D/SdkInitializer
- SdkB dependencies:
2020-10-10 10:12:21.240 20922-20922/com.noway.startup D/SdkInitializer: SdkC dependencies:
2020-10-10 10:12:21.240 20922-20922/com.noway.startup D/SdkInitializer: SdkC create:
2020-10-10 10:12:21.241 20922-20922/com.noway.startup D/SdkInitializer: SdkB create:
2020-10-10 10:12:21.242 20922-20922/com.noway.startup D/SdkInitializer: App onCreate:
2020-10-10 10:12:21.357 20922-20922/com.noway.startup D/SdkInitializer: MainActivity onCreate:
从log可以看出,dependencies 方法的执行顺序是 A → B → C,create 方法的执行顺序是 A → C → B。
App Startup 的手动初始化(延时初始化)使用:
============================
当应用运行后,所有的 SDK 都进行了初始化。可是所有的 SDK 都在启动阶段进行初始化,会导致启动速度的变慢,某些 SDK 如何在需要使用的时候才进行初始化?
再创建一个 SdkD(和SdkA一样)需要在 manifest 中通过 标签中只需要定义 SdkDInitializer,并且添加 tools:node=“remove”:
<provider
android:name=“androidx.startup.InitializationProvider”
android:authorities="${applicationId}.androidx-startup"
android:exported=“false”
tools:node=“merge”>
<meta-data
android:name=“com.noway.startup.SdkAInitializer”
android:value=“androidx.startup” />
<meta-data
android:name=“com.noway.startup.SdkBInitializer”
android:value=“androidx.startup” />
<meta-data
android:name=“com.noway.startup.SdkCInitializer”
android:value=“androidx.startup” />
<meta-data
标签:10,noway,Jetpack,卧槽,startup,20922,SdkInitializer,Android,com 来源: https://blog.csdn.net/m0_66144765/article/details/122430350