Root 权限扩展 — Shell 命令与系统级操作
Root vs Shizuku vs 无障碍
AutoJs6 的自动化能力有三级权限模型:
| 级别 | 依赖 | 能力范围 |
|---|---|---|
| 基础 | 无障碍服务 | 模拟点击/滑动/输入、控件选择、截屏 |
| 中级 | Shizuku(ADB) | 强制停止应用、Shell、隐藏 API |
| 高级 | Root | 系统级 Shell、修改系统设置、文件系统访问 |
检查 Root 状态
// 检查设备是否已 Root
let hasRoot = shell("su -c 'id'", true).code === 0;
console.log("Root: " + (hasRoot ? "已获取" : "未获取"));
// AutoJs6 提供的简便方法(如果有)
// console.log("Root: " + device.isRooted());
shell 模块
AutoJs6 的 Shell 模块可以执行系统命令:
普通 Shell(无需 Root)
// 执行普通 shell 命令
let result = shell("ls /sdcard", false);
console.log("输出: " + result.result);
console.log("退出码: " + result.code);
// 需要更高权限(Shizuku 或 Root)
let result2 = shell("dumpsys package com.example", true);
// 第二个参数 root=true 表示尝试用 su 执行
su 命令(Root 专用)
// 通过 su 执行 Root 命令
let result = shell("su -c 'mount -o remount,rw /system'", true);
if (result.code === 0) {
console.log("系统分区已重新挂载为可写");
} else {
console.log("需要 Root 权限");
}
常用 Root 命令
// 修改系统设置(需要 Root)
shell("su -c 'settings put system screen_brightness 200'", true);
// 挂载 /system 为可写
shell("su -c 'mount -o remount,rw /system'", true);
// 复制文件到系统目录
shell("su -c 'cp /sdcard/hosts /system/etc/hosts'", true);
// 强制停止系统进程
shell("su -c 'kill -9 $(pidof com.example)'", true);
// 批量禁用应用
shell("su -c 'pm disable-user --user 0 com.example'", true);
Root 才能做的事
修改系统设置
// 修改屏幕亮度
shell("su -c 'settings put system screen_brightness " + brightness + "'", true);
// 修改屏幕超时
shell("su -c 'settings put system screen_off_timeout " + timeout + "'", true);
// 修改音量
shell("su -c 'settings put system volume_music_stream " + volume + "'", true);
// 打开 / 关闭自动旋转
shell("su -c 'settings put system accelerometer_rotation 0'", true); // 关闭
shell("su -c 'settings put system accelerometer_rotation 1'", true); // 打开
批量应用管理
/**
* 批量禁用指定应用(Root 设备)
*/
function disableApps(packageNames) {
packageNames.forEach(pkg => {
let result = shell(
"su -c 'pm disable-user --user 0 " + pkg + "'",
true
);
if (result.code === 0) {
console.log("✓ 已禁用: " + pkg);
} else {
console.log("✗ 禁用失败: " + pkg);
}
});
}
disableApps([
"com.example.ads",
"com.example.bloatware",
"com.example.tracker"
]);
读取其他应用数据
// Root 设备可以直接访问其他应用的数据目录
// 普通设备没有这个能力
// 读取某个应用的 SharedPreferences
let prefsContent = shell(
"su -c 'cat /data/data/com.example.app/shared_prefs/config.xml'",
true
).result;
console.log("应用配置: " + prefsContent);
// 读取某个应用的数据库
let dbContent = shell(
"su -c 'sqlite3 /data/data/com.example.app/databases/main.db \".dump\"'",
true
).result;
Root 下的自动化增强
更稳定的点击
// 普通模拟点击(可能被系统限制)
click(500, 800);
// Root Shell 方式点击(更精确、更稳定)
shell("su -c 'input tap 500 800'", true);
// Root Shell 方式滑动
shell("su -c 'input swipe 500 1500 500 500 300'", true);
// input swipe x1 y1 x2 y2 duration
Root Shell 方式的优势
| 对比项 | 无障碍模拟 | Root Shell |
|---|---|---|
| 稳定性 | 受无障碍服务状态影响 | 不受影响 |
| 精度 | 准确 | 准确 |
| 后台执行 | 无障碍服务可能被清理 | Shell 不受影响 |
| 需要前提 | 无障碍服务开启 | Root 权限 |
实战:Root 自动化配置脚本
/**
* 设备初始化脚本(Root 设备)
* 一键配置开发者选项、关闭广告、禁用预装
*/
let hasRoot = shell("su -c 'id'", true).code === 0;
if (!hasRoot) {
dialogs.alert("需要 Root", "此脚本需要 Root 权限");
exit();
}
// 1. 提升屏幕刷新率(如果支持)
shell("su -c 'settings put system min_refresh_rate 120'", true);
shell("su -c 'settings put system peak_refresh_rate 120'", true);
// 2. 禁用开发者选项里的动画(更快)
shell("su -c 'settings put global window_animation_scale 0'", true);
shell("su -c 'settings put global transition_animation_scale 0'", true);
shell("su -c 'settings put global animator_duration_scale 0'", true);
// 3. 关闭后台限制
shell("su -c 'settings put global background_limit -1'", true);
// 4. 禁用预装广告应用
let bloatwares = [
"com.example.ad1",
"com.example.ad2"
];
bloatwares.forEach(pkg => {
shell("su -c 'pm disable-user --user 0 " + pkg + "'", true);
});
// 5. 替换 hosts(屏蔽广告域名)
let hosts = `
# AutoJs6 自动更新的 hosts
0.0.0.0 ad.example.com
0.0.0.0 ads.example.net
# ... 更多
`;
shell("su -c 'echo \"" + hosts + "\" > /system/etc/hosts'", true);
toast("设备初始化完成!");
Root 安全注意事项
| 风险 | 说明 | 建议 |
|---|---|---|
| 系统损坏 | 错误命令可能导致系统无法启动 | 修改 /system 前完整备份 |
| 数据泄露 | Root 可读取所有应用数据 | 不要把含敏感数据的脚本打包分发 |
| 安全检测 | 部分银行/支付应用检测到 Root 会拒绝运行 | 避免在支付类应用上运行 Root 命令 |
| 厂商追责 | Root 通常失去保修 | 操作前了解设备保修政策 |
Root 与 Shizuku 组合
如果设备已经 Root,仍然建议安装 Shizuku:
- Shizuku 通过 ADB 管理,比直接 su 更规范
- AutoJs6 的 Shizuku API 更友好(如
app.kill()比su -c am force-stop好写) - 部分场景 Shizuku 比直接 su 更稳定