1.首先将系统ROOT,源码修改方法网上搜即可。
参考:RK3588s Android 12 Framework修改记录(三)开放ROOT权限给上层应用 执行su命令 - 知乎
2.客户要求去掉root,这是可以把su包生成改为给个名字,如:yls,然后按照修改root的方法,经yls添加到到可执行的白名单中
3.如下代码做一个执行root权限API,即可运行任何命令了
public String execRootCmd( String cmd )
{
String result = "";
DataOutputStream dos = null;
DataInputStream dis = null;
try {
Process p = null;
File su = new File( "/system/xbin/su" );
if( su.exists() ) {
p = Runtime.getRuntime().exec( "su" );
} else
p = Runtime.getRuntime().exec( "yls" );
dos = new DataOutputStream( p.getOutputStream() );
dis = new DataInputStream( p.getInputStream() );
Log.i( TAG, "===cole=== " + cmd );
dos.writeBytes( cmd + "\n" );
dos.flush();
dos.writeBytes( "exit\n" );
dos.flush();
String line = null;
while ( ( line = dis.readLine() ) != null ) {
result += line + "\r\n";
// Log.i(TAG, "root result=="+result);
}
p.waitFor();
} catch ( Exception e ) {
e.printStackTrace();
} finally {
if ( dos != null ) {
try {
dos.close();
} catch ( IOException e ) {
e.printStackTrace();
}
}
if ( dis != null ) {
try {
dis.close();
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
return result;
}