Photo by Fab Lentz on Unsplash
App Startup is a part of the library of Android Jetpack. This provides a way to initialize the components needed to start the application. In this article, let’s take the Koin library as an example to learn how the App Startup library works and what benefits it has.
Why do you use it? 🧐
We usually use the content providers
to initialize the required components when the app starts. However, the content providers
is expensive and initialized in an undetermined order. use App Startup, you can initialize the components you need to start the app, define the initialization order explicitly, and provide good performance.
How do I use it? 👀
To use this, we need to override two methods. Let’s take a Koin library as an example.
The Initializer interface defines two methods.
create()
: Returns the instance, including the operation required to initialize the component.dependencies()
: You can control the order of execution by returning a different list of initializers. For example, if you want to run the another initializer afterKoinInitializer
first, then you can use it as follows!
// another initializer classoverride fun dependencies(): MutableList<Class<out Initializer<*>>>{ // initialized after Koin in initialized return mutableListOf(KoinInitializer::class.java) }
Now, all you have to do is add a content provider
to your AndroidManifest
file.
If you want to disable single or all automatic initialization or manually initialize components, please review the official document.✨
https://developer.android.com/topic/libraries/app-startup
Under the Hood
Let’s take a look at the InitializationProvider
class that we wrote first.
This class inherits ContentProvider
and calls the discoverAndInitialize()
method of AppInitializer
if the context
is not null in the onCreate()
method. So what is AppInitializer
?
AppInitializer class
We can see that AppInitializer
is basically implemented as a Singleton Pattern. Let’s move on to the discoverAndInitialize()
method.
AppInitializer class
The code is a little long, but let’s just look at the important part. First, I’m getting the ComponentName
from on line 4 and then the ProviderInfo
. And on line 11, if metadata
is not null
, all keys are imported, and if it is valid information, we saved it in mDiscovered
and executed the doInitialize()
method.
Then let’s look at the doInitialize()
method.
After checking to see if the component
is already initialized on line 10, check to see if it exists in mInitialized
on line 18. If it does not exist, take the dependencies
list from the component
initializer
parameters on line 23 and call doInitialize()
again if it’s not empty. Then invoke the create()
method to initiate the initialization operation.
Conclusion
If you have many components that need to be initialized when you start an application, try using App StartUp. I think I can trust and use it because the official version is out.
Thanks for reading it!🙌
All codes can be checked here.
hongbeomi/HarryPotter |
![]() |
Reference