Android Startup Analysis

Rouse
The Startup
Published in
6 min readSep 16, 2020

Android Startup provides an application startup that can more simple and efficient way to initialize components. Developers can use Android Startup to simplify the Startup sequence, and explicitly set the initialization sequence and the dependencies between components. Meanwhile, Android Startup support synchronous and asynchronous wait, rely on manual control execution time and directed acyclic graph topological sort way to ensure that the internal depends on the initialization of the component order.

Android Startup after several rounds of iteration has to be more perfect, support the function of the scene is more diversity, if you want to use Android Startup new features, will depend on the upgrade to the latest version

In the previous why I abandoned the Jetpack App Startup? are provided in the article a comparison chart with App Startup, now has been a change.

Core content in the contrast diagram, detailed analysis according to the comparison chart below the Android implementation principle of Startup.

Configuration

Manual configuration

Manual configuration is through StartupManager Builder(), nature is very simple, use Builder mode to initialize some necessary parameters, and then to get the StartupManager instance, and then start Android Startup.

Automatic Configuration

Another way is automatically configured, developers do not need to manually invoke.

To implement the principle of this configuration is: Android Startup interior is via a ContentProvider to realize the automatic configuration, in Android ContentProvider initialization time between Application of attachBaseContext and onCreate.

So Android Startup using this feature will initialize logic is encapsulated into a custom StartupProvider.

After a StartupProvider, need to do is to parse the next AndroidManife.xml provider label under the configured Startup and Config.

Of the resolution were part of StartupInitializer() class, through its discoverAndInitialize() method can obtain the data to parse.

The core logic is:

  1. Through the ComponentName() for specified StartupProvider
  2. Through getProviderInfo() to obtain corresponding StartupProvider meta-data under data
  3. Traverse meta-data array
  4. According to the predetermined value beforehand to match the corresponding name
  5. Finally through the reflection to obtain the corresponding name instance

In the parsing Startup process, in order to reduce Startup configuration, use doInitialize() method to automatically create dependent Startup, and inspection on circular dependencies in advance.

Depend on the support

Initialization of the component is initialized before the dependent components must pass dependencies() are stated. Stating will be in the subsequent parsing, after a dependent component; priority has been completed at the same time will depend on the component has been completed the callback onDependenciesCompleted() method. The execution order is decided by directed graph topological sort.

The closed-loop processing

Upon processing of the closed-loop, on the one hand, in the automatic configuration link doInitialize() method will be processed.

The current Startup joining initializes, at the same time traverse dependencies() rely on an array, recursive calls doInitialize().

In the process of recursion, if in initialize exist in the corresponding uniqueKey (here for the Startup of a unique identifier) is sent on behalf of depending on each other, is dependent on the ring.

Again, on the other hand, the subsequent digraph topological sort optimization will ring processing.

In the process of sorting, optimization will be executed with the main thread in the main thread of Startup classification, and will not be in the process of classification on heavy processing, focus on the current Startup whether the main thread to perform again. So the last as long as the two classifications are not equal to the sum of the size of the Startup combined means ring, which depends on each other.

Relying on the callback

Before relying on the callback to recognize an interface ManagerDispatcher

In ManagerDispatcher has three interface methods, respectively, used to manage Startup execution logic, guarantee the preparing work before execution, distribution in the process of execution with execution after the callback. So dependent on the callback nature also among them.

Call logic is encapsulated to notifyChildren() method.The final call Startup onDependenciesCompleted() method.

So we can be rewritten in the initialize components onDependenciesCompleted() method, so as to get the results returned after the completion of the dependent components. For example Sample of SampleSyncFourStartup.

This is, of course, in the current components depend on the component returns results, Android Startup has offered at any time to query execution of arbitrary components, and support for any returns the result of the components has been completed.

Provide Android Startup StartupCacheManager to realize these functions. The specific ways used by looking at the Sample to obtain.

Manual to inform

The above depends on the callback is introduced, it is automatically after completion of calls to rely on a series of operations. Android Startup also offers a manual notify dependent tasks completed.

Notification Settings manually by manualDispatch() method open.It will cooperate with onDispatch() done together.

In ManagerDispatcher interface implementation class notifyChildren() method, if open the manual notification, the automatic notification process will not go, call toNotify() method, but the current component will be the Dispatcher is added to the registry.Waiting for onDispatche() manual call to awaken toNotify() execution.

Specific implementation examples can see SampleManualDispatchStartup

Topology optimization

Android Startup initialization in the relationship between component and component is this directed acyclic graph.

To Sample in a demo, for example:

We will each Startup edge point to target for a degree. According to this rule is easy to work out the four Startup into degrees.

  1. SampleFirstStartup: 0
  2. SampleSecondStartup: 1
  3. SampleThirdStartup: 2
  4. SampleFourthStartup: 3

So what’s the use of this into the degree? According to the topological sort of AOV net structure topology sequence algorithm mainly performs the following two steps, until there is no into the vertex degree is 0.

  1. Select a vertex and output into the degree of 0;
  2. Is removed from the network all the vertices and the edges

End of the cycle, if the vertices of the output are less than the number of vertices in the net, then output “loop” information, otherwise the output sequence of the vertex is a kind of topological sequence.

According to the steps above, can be concluded that the above four Startup output order:

SampleFirstStartup -> SampleSecondStartup -> SampleThirdStartup -> SampleFourthStartup

The output of the above order is an initialized execution sequence between components. Such guarantee normal execution of the dependence between components, also ensures that initializes the component order of execution of the optimal solution, which depends on the shortest waiting time between components, as well as to check if there is a link between the dependent components.

Now that we have the solutions and implementation steps, the next thing to do is to use code out.

With the above steps, I believe this code to be able to understand.

In addition to the above-described functions, Android Startup also supports the Systrace pile, to provide users with a systematic analysis of the initialization time-consuming process in detail; Initializes the component’s accurate time consuming to collect statistics, convenient for users to download and uploaded to the specified server and so on.

The analysis of the core function of Android Startup temporarily over here, hope to be able to help you.

Of course, I sincerely invite you to join in the construction of the Android Startup, if you have any good suggestions please feel free to comment.

--

--

Rouse
The Startup

Forget all the reasons why it won’t work and believe the one reason why it will.