Skip to content

qq549631030/Applike

Repository files navigation

Android启动初始化插件

此插件用于android组件化开发时,各模块启动初始化。

原理参考:

Android组件化开发实践(八):组件生命周期如何实现自动注册管理

Android组件化开发实践(九):自定义Gradle插件

代码参考:

Android-AppLifecycleMgr

作者未将之上传到jcenter,本人将之仿写后上传到mavenCentral方便大家使用

下载

根目录的build.gradle中:

buildscript {
    mavenCentral()
    dependencies {
        classpath "com.github.qq549631030:applike-plugin:1.0.9"
    }
}

所有模块目录的build.gradle模块中:

apply plugin: 'com.android.application'
//apply plugin: 'com.android.library'

dependencies {
	implementation 'com.github.qq549631030:applike-api:1.0.9'
	annotationProcessor 'com.github.qq549631030:applike-compiler:1.0.9'
}

app模块中:

apply plugin: 'com.android.application'
apply plugin: 'cn.hx.applike'

使用

在每个模块定义一个XXXAppLike实现IAppLike接口,并加上@AppLike注解

@AppLike
class AppAppLike : IAppLike {
    override fun getPriority(): Int {
        return 1
    }

    override fun onCreate(context: Context) {
        //做启动初始化工作
    }

    override fun onTerminate(context: Context) {
    }
}

在Application的onCreate和onTerminate方法中调用AppLikeManager的对应方法

class AppApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        AppLikeManager.onCreate(this)
    }

    override fun onTerminate() {
        AppLikeManager.onTerminate(this)
        super.onTerminate()
    }
}

这样当App启动时所有的XXXAppLike的onCreate方法都会调用,可以通过getPriority()决定调用的优先级,值越大越先调用