07/23
2014

Android 开发测试环境切换工具

之前放出了 Android 开发测试环境切换工具的源码,这里简单介绍下功能、使用和原理。
项目地址:android-switch-env@Github,欢迎 Star For 及提交 Pull Request,截图如下:
screenshot
1. 功能介绍
现在大多开发人员都是通过程序中 isDebug 变量表示是否是 Debug 模式,从而确定后台服务器是接入测试环境还是开发环境

那么如果没有程序代码呢?如果是已上线某个老版本呢?

这个项目的主要功能是修改 Host 文件,从而达到将对线上环境的访问全部指向到测试环境的目的
PS:手机必须开启了 Root 权限

 

2. 使用
在 SD 卡根目录下 Trinea 文件夹内新建文件 switch-env-host.txt,内容为测试环境和开发环境对应的 Host
在 SD 卡根目录下 Trinea 文件夹内新建文件 switch-env-app-info.txt,内容为 App 信息,每行信息为包名 空格 应用名

 

3. 原理
修改 /etc/hosts文件内容,原理是执行命令:
mount -o rw,remount /system
echo “127.0.0.1 localhost” > /etc/hosts
echo “185.31.17.184 github.global.ssl.fastly.net” >> /etc/hosts
chmod 644 /etc/hosts
其中 echo 的两行内容只是示例,代码如下:

List<String> commnandList = new ArrayList<String>();
commnandList.add("mount -o rw,remount /system");
commnandList.add("echo \"127.0.0.1 localhost\" > /etc/hosts");
commnandList.add("echo \"185.31.17.184 github.global.ssl.fastly.net\" >> /etc/hosts");
commnandList.add("chmod 644 /etc/hosts");
CommandResult result = ShellUtils.execCommand(commnandList, true);

用echo命令改hosts文件不用重启可以直接生效。

 

更多关于 ShellUtils 使用的可见:
Android Java执行Shell命令
Android APK root权限静默安装

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

One thought on “Android 开发测试环境切换工具

  1. 博主,请教你个问题,dependencies{ compile ‘com.nhaarman.listviewanimations:library:2.6.0′}用gradle 构建项目时只需加入这个就可以导入对应项目 ,我现在也怎么才能给我自己的项目提供一个这样的地址呢?谢谢博主