Android 组件生命周期分发框架,适用于组件化,模块化,启动优化等场景

Related tags

Demo JDispatcher
Overview

JDispatcher

Android 组件生命周期分发框架,适用于组件化,模块化,启动优化等场景

功能介绍

  • 实现 Application 各个生命周期方法在所有需要的组件中分发
  • 分发顺序支持多种规则
    • 优先级:优先级值越大越先被调用
    • 依赖项:组件依赖的分发类先初始化
    • 支持自动校正在优先级和依赖项两种规则交叉使用情况下的分发顺序
  • 分发维度支持多种规则
    • 在指定进程(所有进程,主进程,非主进程)中分发
    • 在指定线程(主线程,空闲线程,工作线程)中分发,实现异步加载
    • 支持通过非阻塞式异步通知机制实现异步加载与同步加载交叉使用的情况(todo)
    • 手动延迟调用分发,实现延迟加载
    • 支持通过 ContentProvider 实现在 Application 之前超前预加载
    • 只在debug模式下分发,实现 DevTools、DoKit 等开发工具的初始化
  • 维度值采用对整型 or/and 的位操作完成多维度值的收集与识别,灵活且高效
  • 支持初始化时批量传参,可用于多项目多环境的三方 sdk 的初始化,使环境配置更统一
  • 通过注解打点,APT 增量收集分发类,降低耦合,可用于模块化,组件化场景
  • 通过拦截 AGP 构建流程实现在编译期间对分发类的扫描和排序,提高运行时性能
  • AGP Transform 增量支持编译(todo)
  • 通过 ASM 字节码插桩实现分发表和 Application 生命周期回调方法的自动注入,集成更高效
  • 支持统计所有分发类的初始化时间,可用于启动优化的统计与排查

框架结构

  • jdispatcher-annotation

    • 模块类型:apply plugin: 'java'
    • 模块描述:声明编译时所需的注解类以及公共类等
  • jdispatcher-compiler

    • 模块类型:apply plugin: 'java'
    • 模块描述:编译期(.java--.class阶段) 收集和处理整个工程中的Dispatch注解信息并通过 javapoet 生成辅助类文件 JDispatcher$$Group_hash.java
  • jdispatcher-plugin

    • 模块类型:apply plugin: 'groovy'

    • 模块描述:编译期(.class--.dex阶段) 自定义Transform拦截AGP的构建过程,找到所有Dispatch并排序

    • 模块主要工作:

      • 第一个:IDispatch 的分发流程
        • 扫描到所有 APT 生成的 JDispatcher$$Group_hash.java 文件
        • 反射获取收集到的 Map<String, DispatchItem> atlas)
        • 通过 atlas 集合收集到的 DispatchItem 实现对 IDispatch 对象的反射实例化
        • 按照 DispatchItem 的排序规则完成排序操作
        • 将排好序的 IDispatch 集合通过字节码插桩到 JDispatcher 中,运行时执行对所有 IDispatch 的分发操作
      • 第二个:Application 生命周期方法的自动注册流程
        • 通过调用方在gradle中配置的 Application 全类名,在自定义Transform中扫描到该类
        • JDispatcher 调用字节码注入到 onTerminate()
        • JDispatcher 调用字节码注入到 onConfigurationChanged(newConfig: Configuration)
        • JDispatcher 调用字节码注入到 onLowMemory()
        • JDispatcher 调用字节码注入到 onTrimMemory(level: Int)
  • jdispatcher-api

    • 模块类型:apply plugin: 'com.android.library'
    • 模块描述:运行时用于整个框架的初始化,运行时分发等操作

集成说明

//Step 1. Add the JitPack repository to your build file
//buildscript & allprojects
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
//dependencies
classpath 'com.github.jaydroid1024.JDispatcher:jdispatcher-plugin:$last_version'

//Step 2. Add the dependency
implementation 'com.github.jaydroid1024.JDispatcher:jdispatcher-api:$last_version'
kapt 'com.github.jaydroid1024.JDispatcher:jdispatcher-compiler:$last_version'

//Step 3. apply the plugin and config dispatcher
apply plugin: 'jdispatcher'
dispatcher {
    appCanonicalName = "com.jay.android.App"
    buildIncremental = false
    buildDebug = true
}

使用说明

class App : Application() {

    override fun onCreate() {
        super.onCreate()
        //init 已经在 InitializationProvider 中提前初始化,到这里需要分发 onCreate 或其他配置
        JDispatcher.instance
            .withDispatchExtraParam(getDispatchExtraParam())//分发参数
            .withDebugAble(true) //调试模式,打印更多日志
            .onCreate(this)//分发onCreate
        Log.d(CommonConst.TAG, "onCreate in process: " + ApiUtils.getProcessName())
    }

    private fun getDispatchExtraParam(): HashMap<String, HashMap<String, String>> {
        //为分发类指定自定义参数,用于三方key的统一收口配置
        val dispatchExtraParam = HashMap<String, HashMap<String, String>>()
        dispatchExtraParam["com.jay.android.jdispatcher.DispatcherAppDemo"] =
            if (BuildConfig.DEBUG) hashMapOf(
                Pair("key1", "value1_debug"),
                Pair("key2", "value2_debug")
            )
            else hashMapOf(
                Pair("key1", "value1_release"),
                Pair("key2", "value2_release")
            )
        return dispatchExtraParam
    }
  
  
// region如果在 app build.gradle 中配置了
// dispatcher {appCanonicalName = "com.jay.android.App"}
// 就不需要添加以下代码,dispatcher 插件会自动注入

    override fun onTerminate() {
        super.onTerminate()
        JDispatcher.instance.onTerminate()
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        JDispatcher.instance.onConfigurationChanged(newConfig)
    }

    override fun onLowMemory() {
        super.onLowMemory()
        JDispatcher.instance.onLowMemory()
    }

    override fun onTrimMemory(level: Int) {
        super.onTrimMemory(level)
        JDispatcher.instance.onTrimMemory(level)
    }
//endregion
}
//手动调用分发
JDispatcher.instance
    .manualDispatch("com.jay.android.jdispatcher.DispatcherAppDemo")
//声明分发类
@Dispatch(priority = Priority.LOW_DEFAULT, description = "DispatcherAppDemo")
public class DispatcherAppDemo extends DispatchTemplate {

    @Override
    public void onCreate(@NotNull Application app, @NotNull DispatchItem dispatchItem) {
        Log.d("Jay", "DispatcherAppDemo#onCreate" + dispatchItem);
    }

    @Override
    public void onConfigurationChanged(@NotNull Configuration newConfig) {
        Log.d("Jay", "DispatcherAppDemo#onConfigurationChanged" + newConfig);
    }

    @Override
    public void onLowMemory() {
        Log.d("Jay", "DispatcherAppDemo#onLowMemory");

    }

    @Override
    public void onTerminate() {
        Log.d("Jay", "DispatcherAppDemo#onTerminate");

    }

    @Override
    public void onTrimMemory(int level) {
        Log.d("Jay", "DispatcherAppDemo#onTrimMemory" + level);

    }
}
#Jdispatcher 混淆配置
-keep public class com.jay.android.dispatcher.**{*;}
-keep class * implements com.jay.android.dispatcher.common.IDispatch{*;}

Todo

  • 支持通过非阻塞式异步通知机制实现异步加载与同步加载交叉使用的情况

  • 通过 ContentProvider 实现在 Application 之前超前预加载

  • AGP Transform 增量支持编译

参考

  • ARouter 是一个用于帮助 Android App 进行组件化改造的框架 —— 支持模块间的路由、通信、解耦

  • AppInit 是一款 Android 应用初始化框架,基于组件化的设计思路,功能灵活,使用简单。

  • WMRouter 是一款Android路由框架,基于组件化的设计思路,有功能灵活、使用简单的特点

You might also like...
A simple chat demo for socket.io and Android

socket.io-android-chat This is a simple chat demo for socket.io and Android. You can connect to https://socket-io-chat.now.sh using this app. Installa

Google Play game services - Android samples

Google Play game services - Android Samples Copyright (C) 2014 Google Inc. Contents These are the Android samples for Google Play game services. Colle

Android common lib demo, include ImageCache, HttpCache, DropDownListView, DownloadManager, install apk silent and so on, you can find description
Android common lib demo, include ImageCache, HttpCache, DropDownListView, DownloadManager, install apk silent and so on, you can find description

android-demo 关于我,欢迎关注 微博:Trinea 主页:trinea.cn 邮箱:trinea.cn#gmail.com 微信:codek2 依赖:trinea-android-common android-auto-scroll-view-pager viewpager-indica

Cheat Sheet for Android Interviews
Cheat Sheet for Android Interviews

Android-Cheat-sheet Cheat Sheet for Android Interviews Topics Common Coding Programs Data Structure Coding Programs Android Interview Questions Java I

RxJava architecture library for Android
RxJava architecture library for Android

Reference Architecture for Android using RxJava This is an ambitious reference project of what can be done with RxJava to create an app based on strea

📚  Sample Android Components Architecture on a modular word focused on the scalability, testability and maintainability written in Kotlin, following best practices using Jetpack.
📚 Sample Android Components Architecture on a modular word focused on the scalability, testability and maintainability written in Kotlin, following best practices using Jetpack.

Android Components Architecture in a Modular Word Android Components Architecture in a Modular Word is a sample project that presents modern, 2020 app

Collection of Small Android Projects

AndroidDemoProjects ActivityRecognition: Example of using Google Play Services to recognize a user's activity, such as running, walking, or riding a b

Sample material transition animations for Android
Sample material transition animations for Android

See ListOfThings for a newer implementation. Android Material Transitions This Android project samples some Material Design-ish transitions for list i

[] Explorations around Android custom layouts

android-layout-samples Explorations around Android custom layouts, including off main thread View measure/layout passes. Sample code for: Composite Vi

Releases(0.0.7)
  • 0.0.7(Aug 4, 2021)

    • 修复字节码类型问题
    • 修复编译阶段上一次收集类的缓存未清除导致分发混乱的问题
    • Dispatch item 添加线程信息
    Source code(tar.gz)
    Source code(zip)
  • 0.0.6(Aug 3, 2021)

  • 0.0.5(Aug 2, 2021)

  • 0.0.4(Jul 31, 2021)

  • 0.0.3(Jul 30, 2021)

  • 0.0.2(Jul 29, 2021)

  • 0.0.1(Jul 29, 2021)

    • 实现 Application 各个生命周期方法在所有需要的组件中分发
    • 分发顺序支持多种规则
      • 优先级:优先级值越大越先被调用
      • 依赖项:组件依赖的分发类先初始化
      • 支持自动校正在优先级和依赖项两种规则交叉使用情况下的分发顺序
    • 分发维度支持多种规则
      • 在指定进程(所有进程,主进程,非主进程)中分发
      • 在指定线程(主线程,空闲线程,工作线程)中分发,实现异步加载
      • 手动延迟调用分发,实现延迟加载
      • 只在debug模式下分发,实现 DevTools、DoKit 等开发工具的初始化
    • 维度值采用对整型 or/and 的位操作完成多维度值的收集与识别,灵活且高效
    • 支持初始化时批量传参,可用于多项目多环境的三方 sdk 的初始化,使环境配置更统一
    • 通过注解打点,APT 收集分发类,降低耦合,可用于模块化,组件化场景
    • 通过拦截 AGP 构建流程实现在编译期间对分发类的扫描和排序,提高运行时性能
    • 通过 ASM 字节码插桩实现分发表和 Application 生命周期回调方法的自动注入,集成更高效
    • 支持统计所有分发类的初始化时间,可用于启动优化的统计与排查
    Source code(tar.gz)
    Source code(zip)
Owner
王学杰
Do base dev well, Just hammer it
王学杰
A simple app to showcase Androids Material Design and some of the cool new cool stuff in Android Lollipop. RecyclerView, CardView, ActionBarDrawerToggle, DrawerLayout, Animations, Android Compat Design, Toolbar

#Android-LollipopShowcase This is a simple showcase to show off Android's all new Material Design and some other cool new stuff which is (new) in Andr

Mike Penz 1.8k Nov 10, 2022
A simple app to showcase Androids Material Design and some of the cool new cool stuff in Android Lollipop. RecyclerView, CardView, ActionBarDrawerToggle, DrawerLayout, Animations, Android Compat Design, Toolbar

#Android-LollipopShowcase This is a simple showcase to show off Android's all new Material Design and some other cool new stuff which is (new) in Andr

Mike Penz 1.8k Nov 10, 2022
simple android grocery app using kotlin and android studio

Project Idea The idea of this project is to make a grocery android app that users can use to order the groceries they want. It doesn't contain any bac

null 0 Nov 29, 2021
Beetlebug is an open source insecure Android application with CTF challenges built for Android Penetration Testers and Bug Bounty hunters.

Beetlebug Beetlebug is a beginner-friendly Capture the Flag Android application that aims to inspire interest in Mobile Application Security. It is ge

Hafiz Abdulaziz 60 Oct 11, 2022
Do's and Don'ts for Android development, by Futurice developers

Best practices in Android development Avoid reinventing the wheel by following these guidelines. Lessons learned from Android developers in Futurice.

Futurice 20.2k Dec 31, 2022
Learning RxJava for Android by example

Learning RxJava for Android by example This is a repository with real-world useful examples of using RxJava with Android. It usually will be in a cons

Kaushik Gopal 7.6k Dec 30, 2022
A sample Android app which showcases advanced usage of Dagger among other open source libraries.

U+2020 A sample Android app which showcases advanced usage of Dagger among other open source libraries. Watch the corresponding talk or view the slide

Jake Wharton 5.7k Dec 22, 2022
Proguard configurations for common Android libraries

android-proguard-snippets Example Proguard configurations for common Android libraries. This project assumes that your ProGuard configuration is based

Kevin Schultz 4.6k Dec 18, 2022
Examples of Android applications

Android Demos This is a collection of basic android examples created by Novoda. CarouselFragment CompassExample ContactSelector CustomActivityTransiti

Novoda 2k Jan 3, 2023