Android技术知识Android开发Android开发

一键切换Android应用环境(Environment Swit

2018-08-11  本文已影响21人  Xiao_Mai

Environment Switcher

Environment Switcher 是一个在 Android 的开发和测试阶段,用来一键切换环境的工具。

为什么要做这个工具

做这个工具是为了方便开发和测试人员在不重新打包的情况下快速切换环境。

相信大家都遇到过下面的某些场景。

Environment Switcher 应运而生

Environment Switcher 就是为了解决以上问题而设计的,它具有以下几个特点:

使用方法

  1. 配置项目的 build.gradle

    java 版

    dependencies {
        ...
        implementation 'com.xiaomai.environmentswitcher:environmentswitcher:1.3'
        debugAnnotationProcessor 'com.xiaomai.environmentswitcher:environmentswitcher-compiler:1.3'
        releaseAnnotationProcessor 'com.xiaomai.environmentswitcher:environmentswitcher-compiler-release:1.3'
    }
    

    kotlin 版

    apply plugin: 'kotlin-kapt'
    ...
    dependencies {
        ...
        implementation 'com.xiaomai.environmentswitcher:environmentswitcher:1.3'
        kaptDebug 'com.xiaomai.environmentswitcher:environmentswitcher-compiler:1.3'
        kaptRelease 'com.xiaomai.environmentswitcher:environmentswitcher-compiler-release:1.3'
    }
    
  2. 编写 EnvironmentConfig 文件

    例如:项目中包含两个模块,分别是 Music、News,而且每个模块的地址都不同。

    /**
     * 环境配置类
     */
    public class EnvironmentConfig {
    
        /**
         * 整个 App 的环境
         */
        @Module
        class App {
            @Environment(url = "https://gank.io/api/", isRelease = true, alias = "正式")
            private String online;
        }
    
        /**
         * 特殊模块 Music 的环境
         */
        @Module(alias = "音乐")
        class Music {
            @Environment(url = "https://www.codexiaomai.top/api/", isRelease = true, alias = "正式")
            private String online;
    
            @Environment(url = "http://test.codexiaomai.top/api/", alias = "测试")
            private String test;
        }
    
        /**
         * 特殊模块 News 的环境
         */
        @Module(alias = "新闻")
        class News {
            @Environment(url = "http://news/release/", isRelease = true, alias = "正式")
            private String release;
    
            @Environment(url = "http://news/test/", alias = "测试")
            private String test;
    
            @Environment(url = "http://news/test1/")
            private String test1;
    
            @Environment(url = "http://news/sandbox/", alias = "沙箱")
            private String sandbox;
        }
    }
    
  3. 点击菜单栏中的 “Build” -> “Rebuild Project”,等待编译完成。

  4. 在你的 App 中添加一个切换环境的入口,这个入口只在 debug 版显示。例如:在“我的”页面中。

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        ...
        if (!BuildConfig.DEBUG) {
            // only show in debug
            findViewById(R.id.bt_switch_environment).setVisibility(View.GONE);
            return;
        }
        
        findViewById(R.id.bt_switch_environment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // entrance of switch environment
                EnvironmentSwitchActivity.launch(getContext());
            }
        });
    }
    
  5. 获取当前模块的地址:

    EnvironmentSwitcher.getAppEnvironment(getApplication(), BuildConfig.DEBUG);
    EnvironmentSwitcher.getMusicEnvironment(getApplication(), BuildConfig.DEBUG);
    EnvironmentSwitcher.getNewsEnvironment(getApplication(), BuildConfig.DEBUG);
    

更多使用介绍可参考 Demo

demo.gif
上一篇 下一篇

猜你喜欢

热点阅读