android使用adb命令把应用变成模拟器系统应用
1.使用adb命令把apk移到模拟器(夜神自带root,安装以后切记第一次的超级权限一定要允许,如果点击了否,建议重装,反正也不费事)准备工作:1.1adb环境变量配置找到自己SDK的安装目录 把platform-tools和tools配置到环境变量中1.2 打开dos窗口 输入adb 显示一大串东西代表环境变量配置成功1.3.下载夜神模拟器,这个自己选哪个模拟器,只要获取root权限就可以开始:
1.使用adb命令把apk移到模拟器(夜神自带root,安装以后切记第一次的超级权限一定要允许,如果点击了否,建议重装,反正也不费事)
准备工作:
1.1adb环境变量配置
找到自己SDK的安装目录 把platform-tools和tools配置到环境变量中
1.2 打开dos窗口 输入adb 显示一大串东西代表环境变量配置成功
1.3.下载夜神模拟器,这个自己选哪个模拟器,只要获取root权限就可以
开始:
2.打开dos窗口
2.1在dos命令输入adb devices 查看连接的设备(尽量保持一个连接其它的都拔掉),有时候会出现以下问题
2.2然后重新连接 输入命令 adb connect 127.0.0.1:62001 ,
2.3 拓展:(这里我生成了个bat文件,生成方式是:新建一个文本文档,然后把adb connect 127.0.0.1:62001 放到文档里保存,更改文件后缀.txt为.bat 提示点击确定 后面如果夜神和android studio断开连接 直接双击你刚才创建的.bat文件就可以实现一键重连,拓展:木木模拟器重新连接的代码为 adb connect 127.0.0.1:7555 发现了吧,只是端口号不一样,网上搜索你模拟器对应的端口号 更换就能实现其它模拟器的重连)
2.4下面照着敲一遍
adb root
adb remount
2.5.下面一段命令代表把文件移动到模拟器(标红的代表你的apk的绝对路径 ,标粉色的代表apk需要移动到模拟器的路径,这里我的模拟器里面app下不是.apk文件,而是文件夹,所以我自己创建了一个文件夹,你们酌情处理)
注意你们的apk解压以后如果有lib问价夹 要把lib文件夹单独移动到 /system/lib 目录 切记
adb push E:\androidstudio\androidstudio_work\EmptyActivity\app\release\app-release.apk /system/app/EmptyActivity
2.6 到模拟器查看/system/app是否移动成功了,如果没有重新dos命令第一步.
3.安装re并把应用更改成系统应用
3.1 直接在网上搜索RE管理器,下载是个apk,夜神直接拖到桌面直接安装了
3.2打开RE 然后在/system/app 找到自己刚才移动的apk
3.3 选择里面的权限设定
3.4 全部勾选 点击确定 然后重启模拟器 重启以后他会自动安装你的应用 你也卸载不了了,想卸载的话就把权限改了保留第一竖行选中(读的那一列,表示只能读取),其他的都取消点击确定.
3.android端需要写的东西
public class ShellUtils {
public static final String COMMAND_SU = "su";
public static final String COMMAND_SH = "sh";
public static final String COMMAND_EXIT = "exit\n";
public static final String COMMAND_LINE_END = "\n";
private static String MYLOGFILEName = "uidump.xml";// 本类输出的日志文件名称
private static String MYLOG_PATH_SDCARD_DIR = "/sdcard/uidump";// 日志文件在sdcard中的路径
private static SimpleDateFormat myLogSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 日志的输出格式
private static SimpleDateFormat logfile = new SimpleDateFormat("yyyy-MM-dd");// 日志文件格式
private ShellUtils() {
throw new AssertionError();
}
/**
* 查看是否有了root权限
*
* @return
*/
public static boolean checkRootPermission() {
return execCommand("echo root", true, false).result == 0;
}
/**
* 执行shell命令,默认返回结果
*
* @param command
* command
* @param isRoot 运行是否需要root权限
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(String command, boolean isRoot) {
return execCommand(new String[] { command }, isRoot, true);
}
/**
* 执行shell命令,默认返回结果
*
* @param commands
* command list
* @param isRoot 运行是否需要root权限
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(List<String> commands,
boolean isRoot) {
return execCommand(
commands == null ? null : commands.toArray(new String[] {}),
isRoot, true);
}
/**
* 执行shell命令,默认返回结果
*
* @param commands
* command array
* @param isRoot 运行是否需要root权限
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(String[] commands, boolean isRoot) {
return execCommand(commands, isRoot, true);
}
/**
* execute shell command
*
* @param command
* command
* @param isRoot 运行是否需要root权限
* @param isNeedResultMsg
* whether need result msg
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(String command, boolean isRoot,
boolean isNeedResultMsg) {
return execCommand(new String[] { command }, isRoot, isNeedResultMsg);
}
/**
* execute shell commands
*
* @param commands
* command list
* @param isRoot 运行是否需要root权限
* @param isNeedResultMsg 是否需要返回运行结果
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(List<String> commands,
boolean isRoot, boolean isNeedResultMsg) {
return execCommand(
commands == null ? null : commands.toArray(new String[] {}),
isRoot, isNeedResultMsg);
}
/**
* execute shell commands
*
* @param commands
* command array
* @param isRoot 运行是否需要root权限
* @param isNeedResultMsg 是否需要返回运行结果
* @return <ul>
* <li>if isNeedResultMsg is false, {@link CommandResult#successMsg}
* is null and {@link CommandResult#errorMsg} is null.</li>
* <li>if {@link CommandResult#result} is -1, there maybe some
* excepiton.</li>
* </ul>
*/
public static CommandResult execCommand(String[] commands, boolean isRoot,
boolean isNeedResultMsg) {
int result = -1;
if (commands == null || commands.length == 0) {
return new CommandResult(result, null, null);
}
Process process = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = null;
StringBuilder errorMsg = null;
DataOutputStream os = null;
try {
// process = Runtime.getRuntime().exec(isRoot ? COMMAND_SH : COMMAND_SU);
process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
os = new DataOutputStream(process.getOutputStream());
for (String command : commands) {
if (command == null) {
continue;
}
// donnot use os.writeBytes(commmand), avoid chinese charset
// error
os.write(command.getBytes());
os.writeBytes(COMMAND_LINE_END);
os.flush();
}
os.writeBytes(COMMAND_EXIT);
os.flush();
result = process.waitFor();
// get command result
if (isNeedResultMsg) {
successMsg = new StringBuilder();
errorMsg = new StringBuilder();
successResult = new BufferedReader(new InputStreamReader(
process.getInputStream()));
errorResult = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) {
successMsg.append(s);
}
while ((s = errorResult.readLine()) != null) {
errorMsg.append(s);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (successResult != null) {
successResult.close();
}
if (errorResult != null) {
errorResult.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (process != null) {
process.destroy();
}
}
return new CommandResult(result, successMsg == null ? null
: successMsg.toString(), errorMsg == null ? null
: errorMsg.toString());
}
/**
* 运行结果
* <ul>
* <li>{@link CommandResult#result} means result of command, 0 means normal,
* else means error, same to excute in linux shell</li>
* <li>{@link CommandResult#successMsg} means success message of command
* result</li>
* <li>{@link CommandResult#errorMsg} means error message of command result</li>
* </ul>
*
* @author <a href="http://www.trinea.cn" target="_blank">Trinea</a>
* 2013-5-16
*/
public static class CommandResult {
/** 运行结果 **/
public int result;
/** 运行成功结果 **/
public String successMsg;
/** 运行失败结果 **/
public String errorMsg;
public CommandResult(int result) {
this.result = result;
}
public CommandResult(int result, String successMsg, String errorMsg) {
this.result = result;
this.successMsg = successMsg;
this.errorMsg = errorMsg;
}
}
/**
* 打开日志文件并写入日志
* @param mylogtype
* @param tag
* @param text
*/
public static void writeLogtoFile(String mylogtype, String tag, String text) {// 新建或打开日志文件
Date nowtime = new Date();
String needWriteFiel = logfile.format(nowtime);
String needWriteMessage = myLogSdf.format(nowtime) + " " + mylogtype + " " + tag + " " + text;
File dirPath = Environment.getExternalStorageDirectory();
File dirsFile = new File(MYLOG_PATH_SDCARD_DIR);
if (!dirsFile.exists()){
dirsFile.mkdirs();
}
//Log.i("创建文件","创建文件");
File file = new File(dirsFile.toString(), MYLOGFILEName);// MYLOG_PATH_SDCARD_DIR
if (!file.exists()) {
try {
//在指定的文件夹中创建文件
file.createNewFile();
} catch (Exception e) {
}
}
try {
FileWriter filerWriter = new FileWriter(file, true);// 后面这个参数代表是不是要接上文件中原来的数据,不进行覆盖
BufferedWriter bufWriter = new BufferedWriter(filerWriter);
bufWriter.write(needWriteMessage);
bufWriter.newLine();
bufWriter.close();
filerWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用:在使用的时候一定要注意权限(打开其它应用的界面的时候 ShellUtils.execCommand(commend, false); root权限传false 在创建系统文件的时候需要权限要传true ShellUtils.execCommand(commend, true);
)
private void initShellUtils() {
if (ShellUtils.checkRootPermission()) {
Toast.makeText(this, "有权限了", Toast.LENGTH_SHORT).show();
}
String commend2 = "/system/bin/uiautomator dump --compressed /data/local/tmp/uidu.xml";
ShellUtils.CommandResult result2 = ShellUtils.execCommand(commend2, true);
Log.e("result2", "result2-----" + result2.result + "\n" + "successMsg2-----" + result2.successMsg + "\n" + "errorMsg2-----" + result2.errorMsg + "\n\n\n\n\n");
}
你需要的权限不要忘记添加 ,我的做测试用的 就这几个
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
参考大佬资源:
https://blog.csdn.net/pihailailou/article/details/78186781
https://blog.csdn.net/xx326664162/article/details/53406933
还有个ShellUtill的连接没找到!!!!回头翻的时候发现太多了!!!!!!
更多推荐
所有评论(0)