Skip to content

Latest commit

 

History

History
189 lines (168 loc) · 4.86 KB

README.md

File metadata and controls

189 lines (168 loc) · 4.86 KB

AppInit

Android初始化框架,支持组件化

特性

  1. 支持Application生命周期分发
  2. 支持异步
  3. 支持组件化
  4. 支持任务依赖
  5. 支持任务的优先级
  6. 支持指定任意进程初始化
  7. 支持App StartUp
  8. 支持KAPT/KSP

引入

在需要初始化的组件module的build.gradle中引入

plugins {
    // ...
    id 'kotlin-kapt'
}
kapt {
    arguments {
        arg("APPINIT_MODULE_NAME", project.name)
    }
}
dependencies {
    implementation 'io.github.hacket:appinit-api:1.0.1'
    kapt 'io.github.hacket:appinit-compiler:1.0.1'
}

也可以用KSP,速度比KAPT要快不少

// root build.gradle
plugins {
    id 'com.google.devtools.ksp' version '1.7.20-1.0.7' apply(false)
}

// app build.gradle
plugins {
    // ...
    id 'com.google.devtools.ksp'
}
ksp {
    arg("APPINIT_MODULE_NAME", project.name)
}
dependencies {
    implementation 'io.github.hacket:appinit-api:1.0.1'
    kapt 'io.github.hacket:appinit-compiler-ksp:1.0.1'
}

基本使用

定义任务: 实现IAppInitTask接口,用注解@AppInitTask标记初始化任务的属性,如后台线程、进程、优先级和依赖等

@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 任务是否运行在后台线程;默认为false
  • process 任务运行在哪个进程;默认可在所有进程任务
  • priority 任务优先级,值越小优先级越高;默认AppInitTask.PRIORITY_NORMAL
  • 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")
        }
    }
}

支持在各个组件中响应Application的生命周期回调

@AppInitTask(
    id = "main2",
    background = false,
    process = [AppInitTask.PROCESS_MAIN],
    priority = AppInitTask.PRIORITY_MAX,
    dependencies = ["main1"]
)
class MainAppInit : IAppInitTask {
    override fun onCreate(application: Application) {
        AppInit.logger().info(Consts.TAG, "main2 execute start, sleep 2000L.")
        SystemClock.sleep(2000L)
        AppInit.logger().info(Consts.TAG, "main2 execute end.")
    }

    override fun onConfigurationChanged(application: Application, newConfig: Configuration) {
        super.onConfigurationChanged(application, newConfig)
        AppInit.logger().debug(Consts.TAG, "main2 onConfigurationChanged:$newConfig.")
    }

    override fun onLowMemory(application: Application) {
        super.onLowMemory(application)
        AppInit.logger().warning(Consts.TAG, "main2 onLowMemory.")
    }

    override fun onTrimMemory(application: Application, level: Int) {
        super.onTrimMemory(application, level)
        AppInit.logger().warning(Consts.TAG, "main2 onTrimMemory, level=$level")
    }
}

其他的配置

  1. 开启debug模式
AppInit.openDebug()
  1. 自定义log
AppInit.setLogger(ILogger)

支持App StartUp

implementation 'io.github.hacket:appinit-startup:1.0.0'

appinit-auto-register插件

利用appinit-auto-register插件在编译时完成注入,避免运行时扫描dex影响性能。

在root build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        // ...
        classpath "io.github.hacket:appinit-auto-register:1.0.0"
    }
}

在app build.gradle:

plugins {
    id 'com.android.application'
    id 'appinit-auto-register' // 一定要有插件'com.android.application'
    // ...
}

License

Copyright 2020 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.