Android-startup is an open source library for startup optimization. What does this open source library do?

Android-startup provides a simpler and more efficient way to initialize components at application startup. Developers can use it to simplify startup sequences and explicitly set the dependency between the initialization order and components. Meanwhile, Android-startup supports synchronous and asynchronous wait, thread optimization and multi-process, and ensures the initialization sequence of internal dependent components by means of directed acyclic graph topological sorting.

It is currently supported by more than 400 stars. Thank you for your support!

Of course, if there is something wrong, please point out directly, I will humbly accept! You are also welcome to join the discussion.

I’ve written two articles about Android-startup:

Why did I abandon Jetpack’s App Startup?

Faster! This is the Android Startup library I want!

The first article mainly introduces the origin of Android-startup. Why do I design this open source library and what problems can this open source library solve?

In this article, I started thinking through App Startup, a new member of Jetpack. It is a Startup optimization library launched by Jetpack. It solves the dependency relationship between different Startup tasks through dependencies. Reduce ContentProvider abuse. For example, various third-party libraries now provide contentProviders in their own libraries to reduce explicit initialization. Relying on too many of these third-party libraries in your project can lead to abuse of ContentProviders, which can lead to longer App startup times. If the number of contentProviders is too large during startup, it will still take a lot of startup time.

This is one of the biggest advantages of App Startup.

However, in my research and use, I found a fatal shortcoming of it, that is, it does not support asynchronous task dependency, let alone synchronous and asynchronous, asynchronous and asynchronous waiting, which does not provide.

While we can manually create child threads in the create() method to perform asynchronous tasks, what if one asynchronous task depends on another? Where is the unified logical processing after multiple asynchronous tasks are completed? Where is the callback after the dependency task is done? Or do you rely on notifications when the task is done? And execution order optimization between dependencies?

So with these questions in mind, I started trying to optimize Jetpack’s App Startup.

In the process of continuous trial and optimization, the Android-startup open source library was created.

In the article I also give the android startup and App startup function comparison table, here I take out to show you, this is the latest version of the current iteration

indicators App Startup Android Startup
Manual configuration
Automatic configuration
Depend on the support
The closed-loop processing
The thread of control
Asynchronous waiting
Relying on the callback
Manual to inform
Topology optimization
Time-consuming statistics
Thread priority
Multiple processes

Android-startup not only supports all the functions of App Startup, but also extends and optimizes multiple functions based on it. If you are using App Startup, you can ensure a stress-free transition to Android-startup.

If you want to understand the basics of Android-startup, you can go straight to the first article.

If you want to learn more about how Android-startup works, you can read the second article.

The second article was published on hongyang public account.

Thanks again for honyang’s recognition of Android-startup. It was a great inspiration for me to contribute to open source as well.

The second article mainly introduces the configuration principle, dependency support principle, closed-loop processing logic, provided threads, asynchronous wait implementation logic, dependency callback principle, manual notification principle and task execution order topology optimization algorithm of Android-startup.

You’ll get a better understanding of Android-startup and how to use it.

Android-startup has been updated to 1.0.6. The dependency methods are as follows

Dependencies {implementation 'com. Rousetime. Android: android - startup: 1.0.6'}Copy the code

Thread priority and multi-process support are two of the most recent updates to Android-startup.

Thread priority

To address scheduling issues between threads, the ThreadPriority annotation is provided in Android-startup to declare thread priorities for different tasks.

@MustBeDocumented
@Retention
@Target(AnnotationTarget.CLASS)
annotation class ThreadPriority(val priority: Int = Process.THREAD_PRIORITY_DEFAULT)
Copy the code

It is also easy to use, just need to specify the priority in the corresponding AndroidStartup.

@ThreadPriority(Process.THREAD_PRIORITY_URGENT_AUDIO)
class SamplePriorityFirstStartup : AndroidStartup<String>() {
 
    override fun create(context: Context): String? {
        val i = buildString {
            repeat(1000000) {
                append("$it")
            }
        }
        return SamplePriorityFirstStartup::class.java.simpleName
    }
 
    override fun callCreateOnMainThread(): Boolean = false
 
    override fun waitOnMainThread(): Boolean = false
 
}
Copy the code

Thus for SamplePriorityFirstStartup specifies a higher thread priority. Get more scheduling time segments during CPU execution.

To test this, we added two androidStartups with a lower background thread priority and a default thread priority.

Class SamplePrioritySecondStartup: AndroidStartup < String > () {/ / create internal logic consistent with SamplePriorityFirstStartup... } @ThreadPriority(Process.THREAD_PRIORITY_BACKGROUND) class SamplePriorityThirdStartup : AndroidStartup < String > () {/ / create internal logic consistent with SamplePriorityFirstStartup... }Copy the code

For demonstration purposes, add them to StartupManager for initialization using manual configuration.

list.add(SamplePriorityThirdStartup())
list.add(SamplePrioritySecondStartup())
list.add(SamplePriorityFirstStartup())
 
StartupManager.Builder()
    .setConfig(config)
    .addAllStartup(list)
    .build(this)
    .start()
    .await()
Copy the code

Us to forecast, according to the priority setting, should be a high priority SamplePriorityFirstStartup priority over, and then is the default priority SamplePrioritySecondStartup performed, The last is setting the background priority SamplePriorityThirdStartup performed.

Since android-startup has statistics on the time taken by tasks, you can view their execution time.

|================================================================= | Startup Name | SamplePriorityThirdStartup | ----------------------- | -------------------------------------- | Call On Main Thread | false | -----------------------  | -------------------------------------- | Wait On Main Thread | false | ----------------------- | -------------------------------------- | Cost Times | 834 ms |================================================================= | Startup Name | SamplePrioritySecondStartup | ----------------------- | -------------------------------------- | Call On Main Thread | false | -----------------------  | -------------------------------------- | Wait On Main Thread | false | ----------------------- | -------------------------------------- | Cost Times | 778 ms |================================================================= | Startup Name | SamplePriorityFirstStartup | ----------------------- | -------------------------------------- | Call On Main Thread | false | -----------------------  | -------------------------------------- | Wait On Main Thread | false | ----------------------- | -------------------------------------- | Cost Times | 610 ms |================================================================= | Total Main Thread Times | 25 ms | = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =Copy the code

Through the comparison, the log output above SamplePriorityFirstStartup shortest execution time, perfect verified before we predicted results.

Multiple processes

Android-startup supports multi-process task initialization. Different tasks can be initialized in different processes.

Android-startup provides the MultipleProcess annotation to handle the initialization of tasks between different processes.

MustBeDocumented
@Retention
@Target(AnnotationTarget.CLASS)
annotation class MultipleProcess(val process: String)
Copy the code

MultipleProcess is used in a similar way to the ThreadPriority annotation, simply by adding it to the corresponding initialization task and specifying the name of the multiprocess.

@MultipleProcess(":multiple.process.service", ":multiple.test")
class SampleMultipleFourthStartup : AndroidStartup<String>() {
 
    override fun create(context: Context): String? {
        Thread.sleep(1000)
        return SampleMultipleFourthStartup::class.java.simpleName
    }
 
    override fun callCreateOnMainThread(): Boolean = false
 
    override fun waitOnMainThread(): Boolean = false
}
Copy the code

In the code above we specify the initialization for SampleMultipleFourthStartup process name: multiple. Process. The service with: multiple. Test.

For example, we configure a multi-process service in AndroidManifest

<service
    android:name=".MultipleProcessService"
    android:process=":multiple.process.service" />
Copy the code

So that we in the service of the initialization task in the process of how, will screen out don’t include: multiple. Process. The task of the service process.

If MultipleProcess is not set, the current initialization task supports all processes by default.

The test code will not be shown here, because it involves multi-process interaction, the code may be quite large, so if you want to see the complete code can be directly viewed on Github.

Android-startup multiprocess setup is simple to use.

The latest updates to Android-startup will be updated as soon as possible.

At the same time, I will pay close attention to the progress of Jetpack’s App Startup, learn from each other and strive to improve android-startup, and try my best to achieve the best Android Startup optimization library.

Welcome to join in the use, if you have any questions in the process of use, please come to me directly, I will answer the whole process.

Of course, if you have a good suggestion is also welcome to put forward, and sincerely invite you to join the construction of Android-startup, let us contribute to open source.

Finally pull a vote, open source is not easy, hand left lingering fragrance, little finger point a Star.

project

Android_startup: Provides a simpler and more efficient way to initialize components at application startup. Developers can use Android-startup to simplify the startup sequence and explicitly set the dependency between the initialization order and the components. Meanwhile, Android-startup supports synchronous and asynchronous wait, and ensures the initialization sequence of internal dependent components by means of directed acyclic topology sorting.

AwesomeGithub: Based on Github client, pure exercise project, support componentized development, support account password and authentication login. Kotlin language for development, the project architecture is based on Jetpack&DataBinding MVVM; Popular open source technologies such as Arouter, Retrofit, Coroutine, Glide, Dagger and Hilt are used in the project.

Flutter_github: a cross-platform Github client based on Flutter, corresponding to AwesomeGithub.

Android-api-analysis: A comprehensive analysis of Knowledge points related to Android with detailed Demo to help readers quickly grasp and understand the main points explained.

Daily_algorithm: an algorithm of the day, from shallow to deep, welcome to join us.