使用 pm 命令安装 APK [英] Installing an APK using pm command

查看:64
本文介绍了使用 pm 命令安装 APK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用此代码更新 APK:

I tried updating an APK using this code:

Process process;
process = Runtime.getRuntime().exec(new String[] {"su", "-c", "pm install -r -d"+MyApk.apk});

但它不起作用.

当我将它与 adb 一起使用时效果很好:

This works well when I use it with adb like:

adb shell su -c pm install -r -d /system/app/Community-debug.apk

如果它必须请求用户许可才能像使用意图方法一样安装,它也可以正常工作.

It also works fine if it has to ask for user permission in order to install like using the intent method.

推荐答案

您的应用程序需要以系统用户身份运行才能访问此级别的命令.如果您的应用程序使用标准渠道分发,则无法以任何方式完成,例如Google Play、从 SD 卡安装或通过 ADB 安装.这些都不会让您的应用破坏安全性.

Your application would need to run as System user to access this level of commands. It cannot be done in any way if your app is distributed using standard channels, e.g. Google Play, installing from SD card, or installing by ADB. None of these would let your app breach the security.

话虽如此,一种让它工作的方法,但这种方法只适用于特权分发.您的应用必须:

Having said that, there is a way to get it working, but that way is for privileged distribution only. Your app must:

  • 在 AndroidManifest 中包含 android:sharedUserId="android.uid.system"
  • 使用用于对系统其余部分进行签名的证书进行签名
  • 预安装在以下特权位置之一:/system/app/system/priv-app

一旦您的应用满足这些要求,它就可以执行诸如 pm install 之类的命令,即使没有 su

Once your app satisfies these requirements, it can execute commands like pm install, even without the su

不用说,此类选项仅适用于销售自有品牌设备的电信提供商等大型机构.

Needless to say that such options are only available to large bodies like telecom providers who sell their own-branded devices.

这篇关于使用 pm 命令安装 APK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆