Android uses the pm command to install or uninstall apk

Android uses the pm command to install or uninstall apk

pm install [-l] [-r] [-t] [-i INSTALLER_PACKAGE_NAME] [-s] [-f] PATH

pm uninstall [-k] PACKAGE

The pm command can be executed in the shell through adb, and similarly, we can execute it through code.

public static void execCommand(String... command) {

Process process = null;

try {

process = new ProcessBuilder().command(command).start();

//对于命令的执行结果我们可以通过流来读取

// InputStream in = process.getInputStream();

// OutputStream out = process.getOutputStream();

// InputStream err = process.getErrorStream();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (process != null)

process.destroy();

}

}
execCommand("pm", "install", "-f", filePath);//安装apk,filePath为apk文件路径,如/mnt/sdcard/ApiDemos.apk

execCommand("pm", "uninstall", packageName);//卸载apk,packageName为包名,如com.example.android.apis

System-level app is necessary: ​​when compiling and generating apk, add android:sharedUserId="android.uid.system" under your manifest file . After compiling, it cannot be installed normally, and an Installation error: INSTALL_FAILED_SHARED_USER_INCOMPATIBLE error will appear. At this time, To re-sign the apk.

Find the platform.pk8 and platform.x509.pem files in the android source code\build\target\product\security, and find the signapk.jar package in the android compilation directory out (source code directory\build\tools\signapk), And put the compiled apk (such as PMDemo.apk) in the same directory. Before re-signing, use the rar file to open the apk file, enter the META-INF directory, and delete the two files CERT.SF and CERT.RSA and then execute the following command on the command line:

java -jar signapk.jar platform.x509.pem platform.pk8 PMDemo.apk NewPMDemo.apk

Uninstall the old apk before installation, so that the re-signed apk can be installed normally.

Reprinted in: https://my.oschina.net/eclipse88/blog/52270

Guess you like

Origin blog.csdn.net/weixin_42602900/article/details/130195835