App Startup
- 減少App啟動(dòng)時(shí)間
- 減少App在組件的初始化時(shí)間
主動(dòng)初始化
- 實(shí)現(xiàn)Initializer 并實(shí)現(xiàn)里面的方法赃泡,在onCreate里面進(jìn)行你要做的初始化操作
- 然后配置到manifest即可(下面演示結(jié)果又具體的)
手動(dòng)初始化
- 實(shí)現(xiàn)Initializer 并實(shí)現(xiàn)里面的方法延旧,在onCreate里面進(jìn)行你要做的初始化操作
- AppInitializer.getInstance(context).initializeComponent(xxx::class.java)就行了
禁用單個(gè)組件的自動(dòng)初始化
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
<meta-data android:name="com.example.ExampleLoggerInitializer"
tools:node="remove" />
</provider>
- 只是需要在<meta-data> 標(biāo)簽里面甲捏,給對(duì)應(yīng)的啟動(dòng)項(xiàng)添加一個(gè)node="remove"或者刪除這個(gè)啟動(dòng)項(xiàng)
- 您tools:node="remove"在條目中使用而不是簡單地刪除條目,以確保合并工具還從所有其他合并清單文件中刪除條目
禁用所有組件的自動(dòng)初始化
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove" />
- 給provider添加node="remove"就是靜止整個(gè)啟動(dòng)項(xiàng)目湃崩,當(dāng)然你也可以刪除
使用方法
- 實(shí)現(xiàn)Initializer接口,并實(shí)現(xiàn)里面的兩個(gè)方法
/**
* {@link Initializer}s can be used to initialize libraries during app startup, without
* the need to use additional {@link android.content.ContentProvider}s.
*
* @param <T> The instance type being initialized
*/
public interface Initializer<T> {
/**
* Initializes and a component given the application {@link Context}
* 用來做你需要初始化的一些操作的
*
* @param context The application context. app的上下文
*/
@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.
* <br/>
* For e.g. if a {@link Initializer} `B` defines another
* {@link Initializer} `A` as its dependency, then `A` gets initialized before `B`.
* 你初始化話的這個(gè)東西有沒有依賴于其他初始化操作,如果有就在這兒返回相應(yīng)的Initializer實(shí)現(xiàn)類呀狼,沒有的話就直接返回一個(gè)空的集合
*/
@NonNull
List<Class<? extends Initializer<?>>> dependencies();
}
效果演示
- 創(chuàng)建一個(gè)需要初始化的對(duì)象
class AAA {
fun showMessage() {
Log.e("執(zhí)行了", "123")
}
companion object {
var aaa: AAA? = null
fun initialize(context: Context): AAA {
if (aaa == null) {
//延遲2s
Thread.sleep(2000)
aaa = AAA()
}
return aaa!!
}
}
}
- 普通初始化看看結(jié)果
application里面
class App:Application() {
override fun onCreate() {
super.onCreate()
Log.e("執(zhí)行了",System.currentTimeMillis().toString())
AAA.initialize(this)
}
}
manifest里面
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".App"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.JetPack">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
主頁面
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Log.e("執(zhí)行了",System.currentTimeMillis().toString())
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
AAA.initialize(this).showMessage()
}
}
執(zhí)行結(jié)果
到主頁面基本上耗時(shí)2s多
- startup初始化看看結(jié)果
application里面
class App:Application() {
override fun onCreate() {
super.onCreate()
Log.e("執(zhí)行了",System.currentTimeMillis().toString())
// AAA.initialize(this)
}
}
初始化的類
class WorkInitializer:Initializer<AAA> {
override fun create(context: Context): AAA {
return AAA.initialize(context)
}
override fun dependencies(): MutableList<Class<out Initializer<*>>> {
return mutableListOf()
}
}
manifest里面
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".App"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.JetPack">
<provider
android:authorities="${applicationId}.androidx-startup"
tools:node="merge"
android:name="androidx.startup.InitializationProvider"
android:exported="false">
<meta-data
android:name="com.silver.jetpack.startup.WorkInitializer"
android:value="androidx.startup" />
</provider>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
主頁面
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Log.e("執(zhí)行了",System.currentTimeMillis().toString())
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
AAA.initialize(this).showMessage()
}
}
執(zhí)行結(jié)果
結(jié)果就很明顯了