Decrease startup time with Jetpack App Startup




Posted by Yacine Rezgui, Developer Advocate and Rahul Ravikumar, Software Engineer



Jetpack image



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 setup



To 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.

Komentar

Postingan populer dari blog ini

Android Neural Networks API 1.3 and PyTorch Mobile support

Turning it up to 11: Android 11 for developers

Students Learn Android App Development with Google Developer Student Clubs