Android组件化初始化框架
- 支持异步
- 支持组件化
- 支持任务依赖
- 支持优先级
- 支持多进程
定义任务:
@AppInitTask(
id = "main2",
background = false,
process = [AppInitTask.PROCESS_MAIN],
priority = AppInitTask.PRIORITY_NORMAL,
dependencies = ["main1"]
)
class MainAppInit : IAppInitTask {
override fun execute(application: Application) {
AppInit.logger().info(Consts.TAG, "main2 execute start, sleep 2000L.")
SystemClock.sleep(2000L)
AppInit.logger().info(Consts.TAG, "main2 execute end.")
}
}
- id 任务id
- background 任务是否运行在后台线程
- process 任务运行在哪个进程
- priority 任务优先级,值越小优先级越高
- dependencies 任务的依赖
Gradle配置
kapt {
arguments {
arg("APPINIT_MODULE_NAME", project.name)
}
}
Application初始化
class App : Application() {
override fun onCreate() {
super.onCreate()
AppInit.openDebug()
AppInit.init(this, onTaskComplete = {
Log.i(Consts.TAG, "task complete: $it")
}) {
Log.i(Consts.TAG, "all task complete")
}
}
}
利用auto-register
插件在编译时完成注入,避免运行时扫描dex影响性能
根build.gradle:
buildscript {
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
// ...
classpath "com.billy.android:autoregister:1.4.2"
}
}
app build.gradle:
plugins {
// ...
id 'auto-register'
}
/** AppInitTask auto register */
autoregister {
registerInfo = [
[
'scanInterface' : 'me.hacket.appinit.annotation.AppInitTaskRegister',
'codeInsertToClassName' : 'me.hacket.appinit.api.AppInitAutoRegister',
// 未指定codeInsertToMethodName,默认插入到static块中,故此处register必须为static方法
'codeInsertToMethodName': 'load',
'registerMethodName' : 'register',
'include' : [
// 需要扫描的类(正则表达式,包分隔符用/代替,如: com/billy/android/.*),默认为所有的类
"me/hacket/appinit/*/*/.*",
]
]
]
}
Copyright 2021 hacket
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.