{"id":18430762,"url":"https://github.com/sunfusheng/spi","last_synced_at":"2026-03-17T08:35:19.979Z","repository":{"id":89229604,"uuid":"173260835","full_name":"sunfusheng/SPI","owner":"sunfusheng","description":"SPI (Service Provider Interface) 助力 Android 组件化开发","archived":false,"fork":false,"pushed_at":"2020-05-24T06:58:54.000Z","size":227,"stargazers_count":13,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-07T17:42:31.511Z","etag":null,"topics":["annotation","annotation-processor","asm","module","serviceprovider","spi"],"latest_commit_sha":null,"homepage":"https://github.com/sunfusheng/SPI","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sunfusheng.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-01T08:05:11.000Z","updated_at":"2022-09-08T09:27:03.000Z","dependencies_parsed_at":"2023-06-14T12:15:25.351Z","dependency_job_id":null,"html_url":"https://github.com/sunfusheng/SPI","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sunfusheng/SPI","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunfusheng%2FSPI","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunfusheng%2FSPI/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunfusheng%2FSPI/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunfusheng%2FSPI/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sunfusheng","download_url":"https://codeload.github.com/sunfusheng/SPI/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunfusheng%2FSPI/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30619216,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-17T08:10:05.930Z","status":"ssl_error","status_checked_at":"2026-03-17T08:10:04.972Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["annotation","annotation-processor","asm","module","serviceprovider","spi"],"created_at":"2024-11-06T05:22:17.746Z","updated_at":"2026-03-17T08:35:19.944Z","avatar_url":"https://github.com/sunfusheng.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SPI\n\nSPI (Service Provider Interface) 助力 Android 组件化开发\n\n### Gradle配置:\n\n| spi-core | spi-plugin |\n| :--- | :--- |\n| [![spi-core](https://api.bintray.com/packages/sfsheng0322/maven/spi-core/images/download.svg) ](https://bintray.com/sfsheng0322/maven/spi-core/_latestVersion) | [![spi-plugin](https://api.bintray.com/packages/sfsheng0322/maven/spi-plugin/images/download.svg) ](https://bintray.com/sfsheng0322/maven/spi-plugin/_latestVersion) |\n\n\n#### project gradle\n\n``` gradle\n    dependencies {\n        ...\n        classpath 'com.sunfusheng:spi-plugin:\u003clatest-version\u003e'\n    }\n```\n\n#### module gradle\n\n``` gradle\n    apply plugin: 'com.sunfusheng.spi'\n\n    dependencies {\n        ...\n        implementation 'com.sunfusheng:spi-core:\u003clatest-version\u003e'\n    }\n```\n\n### 具体场景与使用\n\n比如在组件化开发中，主工程（app module）会依赖各个子模块（sub module），我们会在主工程中初始化各个子模块，\n通常我们的做法是在主工程中分别调用每个子模块初始化类的初始化和反初始化方法，那么问题来了，每增加一个模块就需要添加一行或若干行初始化和反初始化的代码，\n当然这样也没什么问题，但是有没有一种更好的方式呢？\n\n又比如在组件化开发中，主工程中的Activity包含若干个Fragment，每个Fragment都代表不同的业务封装在自己的模块中，\n如果我们要找到这些Fragment并加载，虽然不是什么难事但是不是不够优雅呢？\n\n那么我们是不是可以在编译时处理这些重复的劳动呢，将各个子模块的初始化类或Fragment类当成一种需要加载的服务，通过注解生成需要调用的代码入口，\n在编译时将这些代码插入到统一的注册池中去；每个子模块包括主工程对应的服务实现类（可以叫Service Provider）都继承统一一个抽象类或实现一个接口，\n这样我们只需要在主模块中找到那些实现固定抽象类或接口的服务然后统一操作就可以了，具体分解下使用步骤：\n\n#### 1、根据需求定义抽象类(abstract class)或接口(Interface)\n\n``` java\n    abstract public class AbsApplicationDelegate {\n\n        abstract public void onCreate(Application application);\n\n        public void onLowMemory() { }\n\n        public void onTrimMemory(int level) { }\n\n        public void onTerminate() { }\n    }\n```\n\n#### 2、子模块实现类继承抽象类或实现接口，并添加注解@Provide(AbsApplicationDelegate.class)\n\n``` java\n    @Provide(AbsApplicationDelegate.class)\n    public class AApplicationDelegate extends AbsApplicationDelegate {\n\n        @Override\n        public void onCreate(Application application) {\n            Log.d(\"SPI\", \"AApplicationDelegate::onCreate()\");\n        }\n    }\n```\n\n#### 3、主工程中实现所有服务(Service)提供者(Provider)的管理\n\n``` java\n    public class ApplicationDelegateManager {\n        private static final ApplicationDelegateManager mDelegatesManager = new ApplicationDelegateManager();\n        private List\u003cAbsApplicationDelegate\u003e mDelegates;\n\n        private ApplicationDelegateManager() {\n            mDelegates = ServiceProvider.getProviders(AbsApplicationDelegate.class);\n        }\n\n        public static ApplicationDelegateManager getInstant() {\n            return mDelegatesManager;\n        }\n\n        public void onCreate(Application application) {\n            for (AbsApplicationDelegate delegate : mDelegates) {\n                delegate.onCreate(application);\n            }\n        }\n\n        public void onLowMemory() {\n            for (AbsApplicationDelegate delegate : mDelegates) {\n                delegate.onLowMemory();\n            }\n        }\n\n        public void onTrimMemory(int level) {\n            for (AbsApplicationDelegate delegate : mDelegates) {\n                delegate.onTrimMemory(level);\n            }\n        }\n\n        public void onTerminate() {\n            for (AbsApplicationDelegate delegate : mDelegates) {\n                delegate.onTerminate();\n            }\n        }\n    }\n```\n\n#### 4、最后调用即可，也可以根据自己的需求解锁其他姿势\n\n``` java\n    public class MyApplication extends Application {\n\n        @Override\n        public void onCreate() {\n            super.onCreate();\n            ServiceProvider.init();\n            ApplicationDelegateManager.getInstant().onCreate(this);\n        }\n\n        @Override\n        public void onLowMemory() {\n            super.onLowMemory();\n            ApplicationDelegateManager.getInstant().onLowMemory();\n        }\n\n        @Override\n        public void onTrimMemory(int level) {\n            super.onTrimMemory(level);\n            ApplicationDelegateManager.getInstant().onTrimMemory(level);\n        }\n\n        @Override\n        public void onTerminate() {\n            super.onTerminate();\n            ApplicationDelegateManager.getInstant().onTerminate();\n            ServiceProvider.destroy();\n        }\n    }\n```\n\n\u003cbr/\u003e\n\n### 个人微信公众号\n\n\u003cimg src=\"http://ourvm0t8d.bkt.clouddn.com/wx_gongzhonghao.png\"\u003e\n\n\u003cbr/\u003e\n\n### 关于我\n\n[GitHub: sunfusheng](https://github.com/sunfusheng)\n\n[个人邮箱: sfsheng0322@126.com](https://mail.126.com/)\n\n[个人博客: sunfusheng.com](http://sunfusheng.com/)\n\n[简书主页](http://www.jianshu.com/users/88509e7e2ed1/latest_articles)\n\n[新浪微博](http://weibo.com/u/3852192525)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsunfusheng%2Fspi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsunfusheng%2Fspi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsunfusheng%2Fspi/lists"}