内容简介:Application startup time is a critical metric for any application. Users expect apps to be responsive and fast to load. When an application does not meet this expectation, it can be disappointing to users. This poor experience may cause a user to rate your
Application startup time is a critical metric for any application. Users expect apps to be responsive and fast to load. When an application does not meet this expectation, it can be disappointing to users. This poor experience may cause a user to rate your app badly on the Play store, or even abandon your app altogether.
Jetpack App Startup is a library that provides a straightforward, performant way to initialize components at application startup. Both library developers and app developers can use App Startup to streamline startup sequences and explicitly set the order of initialization.
Apps and libraries often rely on having components ( WorkManager
, ProcessLifecycleObserver
, FirebaseApp
etc.) initialized before Application.onCreate()
. This is usually achieved by using content providers to initialize each dependency. Instead of defining separate content providers for each component that needs to be initialized, App Startup lets you define initializers that share a single content provider. This significantly improves app startup time, usually by ~2ms per content provider. App Startup also helps you further improve startup performance by making it really easy to initialize components lazily . When App Startup goes stable, we will be updating our libraries like `WorkManager` and `ProcessLifecycle` to benefit from this as well.
App Startup supports API level 14 and above.
How to use it
Gradle setupTo use App Startup in your library or app, add the following dependency to your gradle file:
repositories { google() maven() } dependencies { implementation "androidx.startup:startup-runtime:1.0.0-alpha02" }Define an Initializer
To be able to use App Startup in your application, you need to define an Initializer . This is where you define how to initialize and specify your dependencies. Here’s the interface you need to implement:
interface Initializer<out T: Any> { fun create(context: Context): T fun dependencies(): List<Class<out Initializer<*>>> }
As a practical example, here’s what an Initializer
that initializes WorkManager might look like:
class WorkManagerInitializer : Initializer<WorkManager> { override fun create(context: Context): WorkManager { val configuration = Configuration.Builder() .setMinimumLoggingLevel(Log.DEBUG) .build() WorkManager.initialize(context, configuration) return WorkManager.getInstance(context) } // This component does not have any dependencies override fun dependencies() = emptyList<Class<out Initializer<*>>>() }
Note: This example is purely illustrative. This Initializer
should actually be defined by the WorkManager library.
Lastly, we need to add an entry for WorkManagerInitializer
in the AndroidManifest.xml
:
<provider android:name="androidx.startup.InitializationProvider" android:authorities="${applicationId}.androidx-startup" android:exported="false" tools:node="merge"> <!-- This entry makes WorkManagerInitializer discoverable. --> <meta-data android:name="com.example.WorkManagerInitializer" android:value="androidx.startup" /> </provider>
How it works
App Startup uses a single content provider called InitializationProvider
. This content provider discovers initializers by introspecting the <meta-data>
entries in the merged AndroidManifest.xml
file. This happens before Application.onCreate()
.
After the discovery phase, it subsequently initializes a component after having initialized all its dependencies. Therefore, a component is only initialized after all its dependencies have been initialized.
Lazy initialization
We highly recommend using lazy initialization to further improve startup performance. To make initialization of a component lazy, you need to do the following:
Add a tools:node="remove"
attribute to the <meta-data>
entry for the Initializer
. This disables eager initialization.
<provider android:name="androidx.startup.InitializationProvider" android:authorities="${applicationId}.androidx-startup" android:exported="false" tools:node="merge"> <!-- disables eager initialization --> <meta-data android:name="com.example.WorkManagerInitializer" tools:node="remove" /> </provider>
To lazily initialize WorkManagerInitializer
you can then use:
// This returns an instance of WorkManager AppInitializer.getInstance(context) .initializeComponent(WorkManagerInitializer.class);
Your app now initializes the component lazily. For more information, please read our detailed documentation here .
Final thoughts
App Startup is currently in alpha-02
. Find out more about how to use it from our documentation . Once you try it out, help us make it better by giving us feedback on the issue tracker .
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
ACM国际大学生程序设计竞赛亚洲区预选赛真题题解
郭炜 / 电子工业 / 2011-7 / 49.00元
ACM国际大学生程序设计竞赛(ACM International Collegiate Programming Contest,简称ACM/ICPC)是世界上历史最悠久,规模最大、最具声望的程序设计竞赛,一直受到众多国际知名大学的重视,全球著名IT公司更是争相招募竞赛的优胜者。 该项赛事分为各大洲预选赛和全球总决赛两个阶段。北京大学多次在亚洲区预选赛中负责命题工作,是中国在ACM/ICPC命......一起来看看 《ACM国际大学生程序设计竞赛亚洲区预选赛真题题解》 这本书的介绍吧!