All Projects → xiazunyang → discovery

xiazunyang / discovery

Licence: other
辅助Android开发者在多模块工程间跨模块获取接口(或抽象类)的实现类的开源库,可实现模块的顺序初始化、业务的动态组合等实现。基于AGP和ASM开发。

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to discovery

C.sh
Inline C/asm in Bash
Stars: ✭ 241 (+423.91%)
Mutual labels:  asm
fastquery
FastQuery(Method of fast database query) 基于Java语言. 他的使命是:简化Java操作数据层.做为一个开发者,仅仅只需要设计编写DAO接口即可,在项目初始化阶段采用ASM生成好实现类. 因此,开发代码不得不简洁而优雅.从而,大幅度提升开发效率.
Stars: ✭ 63 (+36.96%)
Mutual labels:  asm
Uatu
Android方法调用跟踪 ; 方法耗时统计 ; 方法调用参数以及返回值跟踪 ; 方法调用替换;方法hook
Stars: ✭ 93 (+102.17%)
Mutual labels:  asm
8086-cheatsheet
8086 Microprocessor Cheat sheet with Programs
Stars: ✭ 81 (+76.09%)
Mutual labels:  asm
kasm
Assembler library for Kotlin
Stars: ✭ 40 (-13.04%)
Mutual labels:  asm
FutureDOS
A futuristic DOS
Stars: ✭ 46 (+0%)
Mutual labels:  asm
Advanced Go Programming Book
📚 《Go语言高级编程》开源图书,涵盖CGO、Go汇编语言、RPC实现、Protobuf插件实现、Web框架实现、分布式系统等高阶主题(完稿)
Stars: ✭ 16,436 (+35630.43%)
Mutual labels:  asm
Games
The Games app has two features which are listing and showing detail of games.
Stars: ✭ 15 (-67.39%)
Mutual labels:  modularization
neskell
A Haskell 6502 Emulator
Stars: ✭ 56 (+21.74%)
Mutual labels:  asm
android-kick-start-modular
Android Kick Start Project Template Framework FrogoBox || Admob, MVVM Archictecture || Clean Architecture Modularization
Stars: ✭ 16 (-65.22%)
Mutual labels:  modularization
woodpecker-client
异常日志收集客户端 环境隔离版本
Stars: ✭ 51 (+10.87%)
Mutual labels:  asm
ZipArchive
A single-class pure VB6 library for zip with ASM speed
Stars: ✭ 38 (-17.39%)
Mutual labels:  asm
boot2flappy
Flappy Bird as bootable UEFI executable
Stars: ✭ 48 (+4.35%)
Mutual labels:  asm
APISearch-Plugin-x86
APISearch Plugin (x86) - A Plugin For x64dbg
Stars: ✭ 46 (+0%)
Mutual labels:  asm
APIInfo-Plugin-x86
APIInfo Plugin (x86) - A Plugin For x64dbg
Stars: ✭ 42 (-8.7%)
Mutual labels:  asm
Recaf
The modern Java bytecode editor
Stars: ✭ 3,374 (+7234.78%)
Mutual labels:  asm
Huawei Zabbix Templates
Here are same Zabbix Templates for same Huawei devices like the Switches S5700 and S6700 and the router NE20/NE40 series series
Stars: ✭ 34 (-26.09%)
Mutual labels:  routers
512-byte-vm
A VM image in 512 bytes. Yes, you read it right.
Stars: ✭ 29 (-36.96%)
Mutual labels:  asm
dcc
Direct/Interactive C Compiler
Stars: ✭ 18 (-60.87%)
Mutual labels:  asm
19CSE100-Solving-and-Algorithmic-Thinking
19CSE100 - Problem Solving and Algorithmic Thinking (PSAT) - 1st Sem - B.E. (CSE-CYS) - ASC, CBE.
Stars: ✭ 76 (+65.22%)
Mutual labels:  modularization

Discovery

通过AGP实现的在Android工程多模块之间获取接口或抽象类的实现类的实例的辅助工具。
通过在接口或抽象类上添加@Discoverable注解、并在实现类上添加@Implementation注解,就可以在工程中的任意模块中通过Discoveries类获取该接口或抽象类的实例,辅助开发者在模块之间访问数据。

相比ARouter等路由框架的服务发现功能,Discovery主要功能在编译期间工作,不会在运行时扫描dex,有更好的性能。
相比ServiceLoaderDiscovery支持抽象类,以及可以获取实现类的class对象,可以适配更丰富的其它框架。

演示工程:https://github.com/xiazunyang/DiscoveryDemo.git

原理

Discovery会在编译时扫描每个类文件,并将所有标记的类的信息通过ASM注册到Discoveries类中。

安装

当前最新版本:Maven Central

  1. 在根模块的build.gradle的适当位置添加以下代码:

    buildscript {
       repositories {
           ...
           mavenCentral()
       }
       dependencies {
           ...
           //添加Discovery插件
           classpath("cn.numeron:discovery.plugin:latest_version")
       }
    }
  2. 在业务模块的build.gradle文件中添加以下代码:

    api("cn.numeron:discovery.library:latest_version")
  3. 在主模块的build.gradle文件中添加以下代码:

    plugins {
        id("com.android.application")
        ...
        //应用Discovery插件
        id("discovery")
    }

使用

  • 获取其它模块的业务服务

    1. 声明接口时标记@Discovrable注解
    @Discoverable
    interface ISignInService {
    
        /** 判断当前是否已登录 */
        suspend fun isSignIn(context: Context): Boolean
    
        /** 通过用户名和密码进行登录 */
        suspend fun signInByPassword(username: String, password: String)
    
    }
    1. 在任意模块中实现该接口,要求拥有无参构造方法,并标记Implementation注解
    @Implementation
    class SignInServiceImpl: ISignInService {
    
        override suspend fun isSignIn(context: Context): Boolean {
            TODO("判断是否已经登录")
        }
    
        override suspend fun signInByPassword(username: String, password: String) {
            TODO("根据提供的账号密码进行登录")
        }
    
    }
    1. 在任意模块的代码中通过Discoveries获取接口实例
    lifecycleScope.launch {
        val signInService = Discoveries.getInstance<ISignInService>()
        if (!signInService.isSignIn(requireContext())) {
            //未登录, do something...
        }
    }
  • 获取所有模块中的所有实例

    1. 在基础模块中声明初始化接口
    @Discoverable
    interface IInitializer {
    
        fun init(application: Application)
    
    }
    1. 在其它模块中实现该接口
    //需要初始化的A模块
    @Implementation(order = 0)
    class AModuleInitializer: IInitializer {
        override fun init(application: Application) {
            //init a module
        }
    }
    
    //需要初始化的B模块
    @Implementation(order = 10)
    class BModuleInitializer: IInitializer {
        override fun init(application: Application) {
            //init b module
        }
    }
    1. Application中获取所有实例并初始化
    class MyApplication: Application() {
    
        override fun onCreate() {
            //获取所有IInitiator的实现,并执行init方法
            val initializerList = Discoveries.getAllInstances<IInitializer>()
            initializerList.forEach {
                //order数值小的实现类优先调用
                it.init(this)
            }
        }
    
    }

版本更新记录

  • 1.4.1

    • Implementation注解中添加order属性,用于给实现类排序。
  • 1.4.0

    • 添加抽象类的支持,不再强制要求参数是接口。
    • 不强制要求实现类拥有无参构造,但是Discovery不再参与创建这一类实现类的实例。
    • Discoveries中新增两个方法用于获取实现类的Class,方便用户自己创建它们的实例。
  • 1.3.3

    • getAllInstances没有获取到任何实例的时候,不再抛出异常,改为返回一个空的列表。
  • 1.3.2

    • ASMOption降级至ASM7
  • 1.3.1

    • 修复ASM织入了错误的代码的问题。
  • 1.3.0

    • 存在致命错误,请使用1.3.1版本
    • 去除注解处理器模块,使配置简化。查看之前的配置流程
    • 修复增量编译的一些问题。
  • 1.2.2

    • 使用字符串作为配置名称,不再需要冗长的导包。
    • 编译时检查Implementation标记的类,要求必需拥有无参构造方法。
  • 1.2.1

    • Implementation注解标记的类实现了多个接口时,会忽略掉未被Discovrable注解标记的接口。
  • 1.2.0

    • 新增Discovery的配置选项,可配置实现类的处理方式。
    • 默认为Scan模式,即全局扫描,可配置为Mark模式,需要使用Implementation注解标记实现类,可免去扫描过程,以节省编译时间。
  • 1.1.0

    • 注解处理器新增APT的实现,兼容java项目,与KSP任选其一即可。
  • 1.0.0

    • 正式发布,由KSPAGP实现主要功能。
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].