Kotlin + Flow Implementation of Android Application Initialization Task Startup Library

  • 2021-12-11 19:09:32
  • OfStack

Directory characteristics Introducing dependency Use

Characteristic

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

Support modularization and load tasks by module You can specify the name of the worker process, main for running on the main process only, all for running on all processes, and the default value is all You can specify that tasks are executed only on worker threads You can specify that tasks are executed only in debug mode You can specify that tasks are executed after compliance conditions are met You can specify the task priority and determine the execution order of undependent synchronous tasks in the same module You can specify a list of dependent tasks and detect circular dependencies Scheduling tasks using Flow Just over 200 lines of code, simple and clear There are time-consuming statistics

Introducing dependency

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" 

  //  Use  init-startup  Substitute  init  Available  Jetpack Startup  Automatic library initialization 
  //  No need to be in  Application.onCreate  Call  InitManager.init()
  implementation "me.reezy.init:init-startup:0.9.0" 
}

Use

In AndroidManifest. xml < application > Add modules in


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

Define 1 task with annotation @ Init and InitTask interfaces


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

Configure task information by annotating @ Init parameters


@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
annotation class Init(
  val process: String = "all",    //  Specifies the worker process name, main  Indicates that only the main process runs, all  Indicates that all processes run 
  val background: Boolean = false,  //  Whether to execute the task on the worker thread 
  val debugOnly: Boolean = false,   //  Is it only in the  DEBUG  Mode execution task 
  val compliance: Boolean = false,  //  Is compliance required 
  val depends: Array<String> = [],  //  Dependent task list 
  val priority: Short = 0       // 
)

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


class Task(
  val name: String,          // APT The collected task names are formatted as  "$moduleName:${clazz.simpleName}"
  val background: Boolean = false,  //  Whether to execute the task on the worker thread 
  val priority: Int = 0,       //  The priority of process running, and the smaller one executes first 
  val depends: Set<String> = setOf(), //  Dependent task list, the same module only needs to specify "${clazz.simpleName}" Cross-module needs to specify  "$moduleName:${clazz.simpleName}"
  val block: () -> Unit = {},     //  Tasks to be performed 
) {
  val children: MutableSet<Task> = mutableSetOf() //  Subtask list 
}

Core class

TaskList is responsible for holding and adding tasks TaskManager is responsible for scheduling tasks and supports adding switch tasks (no business is only used as a switch, which can be manually triggered to complete and try to execute its sub-tasks)

Non-dependent asynchronous tasks executed in parallel on sub-threads
Non-dependent synchronization tasks, executed sequentially on the main thread
Tasks with dependencies, ensure that there is no circular dependency, and the dependent tasks are executed first

InitManager is responsible for finding the task loader of each module and starting initialization. It uses a compliance switch to make related tasks execute after determining compliance

You can dispense with the InitManager collection task


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 the switch task t1
manager.trigger("t1")

The above is Kotlin + Flow implementation of Android application initialization task startup library details, more about the implementation of Android application initialization task startup library information please pay attention to other related articles on this site!


Related articles: