SoFunction
Updated on 2025-03-02

Kotlin + Flow implements Android application initialization task startup library

characteristic

The Android application initialization task startup library implemented by Kotlin + Flow.

  • Support modularity and load tasks by module
  • You can specify the worker process name. Main means running only in the main process, all means running in all processes, the default value is all
  • You can specify that tasks can be executed only on the worker thread
  • It is possible to specify that tasks are executed only in debug mode
  • Can specify that tasks are executed after compliance conditions are met
  • You can specify task priority and determine the execution order of synchronization tasks without dependencies in the same module.
  • Can specify a list of dependency tasks to detect cyclic dependencies
  • Schedule tasks using Flow
  • Only more than 200 lines of code, simple and clear
  • Time-consuming statistics

Introduce dependencies

Project address:/czy1121/ini…

repositories { 
  maven { url "/ezy/repo/raw/android_public/"}
} 
dependencies {
  implementation ":init:0.9.0" 
  kapt ":init-compiler:0.9.0" 

  // Use init-startup instead of init to automatically initialize using the Jetpack Startup library  // No need to call ()  implementation ":init-startup:0.9.0" 
}

use

Add module in <application>

<meta-data android:name="modules" android:value="app" />

Define a task by annotating the @Init and InitTask interfaces

@Init
class OneInit : InitTask {
  override fun execute(app: Application) {
    (TAG, "this is ${} in ${().name}")
  }
}

Configure task information by annotating the parameters of @Init

@Target()
@Retention()
annotation class Init(
  val process: String = "all",    // Specify the worker process name, main means running only in the main process, all means running in all processes  val background: Boolean = false,  // Whether to execute tasks on the worker thread  val debugOnly: Boolean = false,   // Whether to perform tasks only in DEBUG mode  val compliance: Boolean = false,  // Is compliance required?  val depends: Array&lt;String&gt; = [],  // Dependency task list  val priority: Short = 0       // 
)

APT will collect task information by module and generate a task loader (InitLoader_$moduleName), which is used to add tasks to TaskList.

class Task(
  val name: String,          // The task name collected by APT is in the format of "$moduleName:${}"  val background: Boolean = false,  // Whether to execute tasks on the worker thread  val priority: Int = 0,       // The priority of the process operation, if the value is small, execute first  val depends: Set&lt;String&gt; = setOf(), // Dependency task list, only "${}" is required for the same module, and "$moduleName:${}" is required for the same module.  val block: () -&gt; Unit = {},     // Tasks to be executed) {
  val children: MutableSet&lt;Task&gt; = mutableSetOf() // Subtask list}

Core category

  • TaskList is responsible for holding and adding tasks
  • TaskManager is responsible for scheduling tasks and supports adding switch tasks (no service is used only as a switch, and can be manually triggered and completed, and it will be tried to perform its subtasks)

Dependless asynchronous tasks, executed in parallel on child threads
Dependless synchronization tasks, executed sequentially in the main thread
Dependable tasks, ensure that there is no circular dependency, and the dependent tasks are executed first

  • InitManager is responsible for finding the task loader for each module and starting initialization. It uses a compliance switch to enable the relevant tasks to be executed after compliance is determined.

You can collect tasks without using InitManager

val taskList = TaskList(app).apply {
  add("task1") { 
  }  
  add("task2", depends = setOf("t1")) { 
  }  
  add("task3", depends = setOf("task1")) { 
  }  
}

val manager = TaskManager(taskList, setOf("t1"))
()

// ...

// Complete switch task t1("t1")

The above is the detailed content of Kotlin + Flow to implement the Android application initialization task startup library. For more information on implementing the Android application initialization task startup library, please pay attention to my other related articles!