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, then A gets initialized before B.

*/

@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个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值