features

Kotlin + Flow implementation of Android application initialization task start library.

  • Supports modularization and loads tasks by modules
  • You can specify a worker process name. Main indicates that the worker process is run only in the main process. All indicates that the worker process is run in all processes
  • You can specify that the task is executed only on the worker thread
  • You can specify that tasks are executed only in debug mode
  • Tasks can be assigned to be executed after compliance conditions are met
  • You can specify task priorities and determine the execution sequence of non-dependent synchronization tasks in the same module
  • Can specify a list of dependency tasks, can detect cyclic dependencies
  • Schedule tasks using Flow
  • Just over 200 lines of code, simple and clear
  • Elapsed time statistics

Introduction of depend on

Project address: github.com/czy1121/ini…

repositories { 
    maven { url "https://gitee.com/ezy/repo/raw/android_public/"}
} 
dependencies {
    implementation "Me. Reezy. Init: init: 0.9.0" 
    kapt "Me. Reezy. Init: init - compiler: 0.9.0" 

    // Using init-startup instead of init can be automatically initialized using the Jetpack Startup library
    // No need to call initManager.init () in application.oncreate
    implementation "Me. Reezy. Init: init - startup: 0.9.0" 
}
Copy the code

use

Add modules to

in androidmanifest.xml

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

Define a task through the @init and InitTask interface annotations

@Init
class OneInit : InitTask {
    override fun execute(app: Application) {
        Log.e(TAG, "this is ${javaClass.simpleName} in ${Thread.currentThread().name}")}}Copy the code

Configure the task information by annotating the @init parameter

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
annotation class Init(
    val process: String = "all".// Specify the worker process name. Main indicates that only the main process is run, and all indicates that all processes are run
    val background: Boolean = false.// Whether the task is executed on the worker thread
    val debugOnly: Boolean = false.// Whether to execute tasks only in DEBUG mode
    val compliance: Boolean = false.// Whether compliance is required
    val depends: Array<String> = [],    // List of dependent tasks
    val priority: Short = 0             // 
)
Copy the code

APT collects task information by module and generates a task loader (InitLoader_$moduleName), which is used to add tasks to the TaskList

class Task(
    val name: String,                   $moduleName:${clazz.simplename}"
    val background: Boolean = false.// Whether the task is executed on the worker thread
    val priority: Int = 0.// Priority of the process to run
    val depends: Set<String> = setOf(), ${clazz.simplename} $moduleName:${clazz.simplename}"
    val block: () -> Unit = {},         // The task to be performed
) {
    val children: MutableSet<Task> = mutableSetOf() // Subtask list
}
Copy the code

Core classes

  • TaskListResponsible for holding and adding tasks
  • TaskManagerResponsible for scheduling tasks, support to add switch tasks (no business only as a switch, can be manually triggered to complete, and try to execute sub-tasks)
    • Asynchronous tasks with no dependencies, executed in parallel on child threads
    • Non-dependent synchronization tasks are executed sequentially on the main thread
    • For dependent tasks, ensure that there are no circular dependencies and the dependent tasks are executed first
  • InitManagerThe task loader, responsible for finding each module and initiating initialization, uses a compliance switch to execute the related tasks when 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"))
manager.start()

// ...

// Complete switch task T1
manager.trigger("t1")
Copy the code