Installing an APK using pm command

こ雲淡風輕ζ 提交于 2019-12-18 07:13:01

问题


I tried updating an APK using this code:

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

but it does not work.

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.


回答1:


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:

  • include android:sharedUserId="android.uid.system" in the AndroidManifest
  • be signed with the certificate which is used to sign the rest of the system
  • be pre-installed in one of the privileged locations: /system/app, /system/priv-app

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.




回答2:


Your solution will only work on rooted devices.

In order for it to run on all devices, firstly, you need a cooperation from the device manufacturer to put your app under /system/priv-app. Putting your apk there will give you system privileges enabling you to perform the install. On top of that, you need to add android.permission.INSTALL_PACKAGES to your manifest file. Finally, add these lines to your code:

Runtime.getRuntime().exec("chmod 777 " + MyApk.apk);
Process process = Runtime.getRuntime().exec("pm install -r " + MyApk.apk);


来源:https://stackoverflow.com/questions/25423519/installing-an-apk-using-pm-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!