【问题标题】:How to uninstall system apps programatically after Android O?Android O之后如何以编程方式卸载系统应用程序?
【发布时间】:2019-05-05 18:27:38
【问题描述】:

我正在制作一个可以卸载系统应用程序的应用程序。在查看了 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。我遇到的问题是在请求 root 并获取 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;

这是返回文件路径的函数:

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”命令)和所有需要的权限。

【问题讨论】:

    标签: android root


    【解决方案1】:

    首先,运行:

    pm uninstall <package_name>
    

    那么,

    rm -r <applicationinfo.sourceDir>
    

    那么,

    rm -r <applicationinfo.publicSourceDir>
    

    然后重启设备,应用程序应该会被卸载。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-05
      • 1970-01-01
      • 2012-03-17
      • 1970-01-01
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多