Android O之后如何以编程方式卸载系统应用程序?

时间:2019-05-05 18:27:38

标签: android root

我正在制作可以卸载系统应用程序的应用程序。在查看完StackOverFlow的所有答案之后,我可以说其中99%是通过ADB和我认为有用的https://stackoverflow.com/a/34399068/9953518,现在已从Android O更改了。

根据本文https://medium.com/@quaful/the-changes-of-apk-install-location-since-android-oreo-e646d1b53c4d,现在无法导航到应用程序的特定文件夹,并且我们必须使用.sourceDir。我遇到的问题是在请求根目录并获取sourceDir之后,.apk文件不会卸载,如果确实存在,则在这种情况下不会卸载或删除完整的文件。我正在使用以下代码:

//appsSelected is the array with all the package names of the system apps selected to be uninstalled
        case "uninstall":
            for (int i = 0; i < appsSelected.size(); ++i) {
                final int finalI = i;
                Thread worker = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        RootManager.getInstance().obtainPermission();
                        System.out.println("Public directory is  "+ yup(appsSelected.get(finalI)));
                        runCommand("rm -rf "+ yup(appsSelected.get(finalI)) );

                    }
                });
                worker.start();
            }

            break;

这是返回filePath的功能:

String yup(String pack){
    PackageManager m = getPackageManager();
    PackageInfo p = null;
    try {
        p = m.getPackageInfo(pack, 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return p.applicationInfo.sourceDir;
}

最后是运行命令的函数:

  public static void runCommand(String command) {
    try {
        Process chmod = Runtime.getRuntime().exec(command);

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(chmod.getInputStream()));
        int read;
        char[] buffer = new char[4096];
        StringBuffer output = new StringBuffer();
        while ((read = reader.read(buffer)) > 0) {
            output.append(buffer, 0, read);
        }
        reader.close();
        chmod.waitFor();
        String outputString = output.toString();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

我具有root权限(“ su”命令)和所有必需的权限。

1 个答案:

答案 0 :(得分:0)

首先,运行:

pm uninstall <package_name>

然后

rm -r <applicationinfo.sourceDir>

然后

rm -r <applicationinfo.publicSourceDir>

然后重新启动设备,并应卸载该应用程序。

注意:所有这些命令都应以root用户身份运行!