简体   繁体   English

android 7:使用Runtime.getRuntime()。exec在APK上安装NullPointerException

[英]android 7: NullPointerException on APK installing with Runtime.getRuntime().exec

I'm trying to install APK in android 7 (samsung and sony) using regular Runtime.getRuntime.exec() routine. 我正在尝试使用常规的Runtime.getRuntime.exec()例程在android 7(samsung和sony)中安装APK。 The installation fails with the following exception in the logcat: 安装失败,logcat中出现以下异常:

09-04 14:14:33.932 16623-16623/? D/AndroidRuntime: Calling main entry com.android.commands.pm.Pm
09-04 14:14:33.939 3695-3876/? D/PackageInstaller:  installation of android.content.pm.PackageInstaller$SessionParams@a4d2f0e for non-container user 0
09-04 14:14:33.940 16623-16623/? E/Pm: Error
                                       java.lang.NullPointerException
                                           at android.os.Parcel.readException(Parcel.java:1699)
                                           at android.os.Parcel.readException(Parcel.java:1646)
                                           at android.content.pm.IPackageInstaller$Stub$Proxy.createSession(IPackageInstaller.java:249)
                                           at com.android.commands.pm.Pm.doCreateSession(Pm.java:530)
                                           at com.android.commands.pm.Pm.runInstall(Pm.java:369)
                                           at com.android.commands.pm.Pm.run(Pm.java:142)
                                           at com.android.commands.pm.Pm.main(Pm.java:99)
                                           at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
                                           at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:301)
09-04 14:14:33.941 16623-16623/? I/art: System.exit called, status: 1
09-04 14:14:33.941 16623-16623/? I/AndroidRuntime: VM exiting with result code 1

I try to implement that so: 我试着这样做:

Process proc = Runtime.getRuntime().exec("pm install /sdcard/imo.apk");

and so: 所以:

Process proc = Runtime.getRuntime().exec(new String[]{"/system/bin/sh","-c","/system/bin/pm install /sdcard/imo.apk"});

But exception occurs in the both cases. 但在这两种情况下都会发生异常。

In androids less than 7 it works. 在小于7的机器人中它起作用。

The permissions are: 权限是:

declared permissions:
  android.permission.INTERACT_ACROSS_USERS_FULL: prot=normal
requested permissions:
  android.permission.BIND_NOTIFICATION_LISTENER_SERVICE
  android.permission.READ_EXTERNAL_STORAGE
  android.permission.WRITE_EXTERNAL_STORAGE
  android.permission.READ_LOGS
User 0: ceDataInode=393939 installed=true hidden=false suspended=false stopped=false notLaunched=false enabled=0
  gids=[1007]
  runtime permissions:
    android.permission.CLEAR_APP_USER_DATA: granted=true
    android.permission.INSTALL_PACKAGES: granted=true
    android.permission.READ_EXTERNAL_STORAGE: granted=true
    android.permission.INTERACT_ACROSS_USERS_FULL: granted=true
    android.permission.READ_LOGS: granted=true
    android.permission.CLEAR_APP_CACHE: granted=true
    android.permission.WRITE_EXTERNAL_STORAGE: granted=true

Installation does succeed thru the shell. 安装确实通过shell成功。 Thanks in advance 提前致谢

I had the same issue but I solved it by a different way. 我有同样的问题,但我用不同的方式解决了它。 My app is a launcher and it has system privileges. 我的应用程序是一个启动器,它具有系统权限。 It is my solution. 这是我的解决方案。

Add to manifest header android:sharedUserId="android.uid.system" 添加到清单头文件android:sharedUserId =“android.uid.system”

Add this permission to mainifest android:name="android.permission.INSTALL_PACKAGES" 将此权限添加到mainifest android:name =“android.permission.INSTALL_PACKAGES”

Add this code to some background thread 将此代码添加到某个后台线程

try {
        String cmd = "pm install -r -i " + LAUNCHER_PACKAGE_ID + " " + activity.getFileStreamPath(apkPath);
        Process p = Runtime.getRuntime().exec(cmd);
        p.waitFor();
    } 
    catch (Exception e) {
        Log.d(TAG, "SilentInstallActivity error: " + e);
    }

Sign your android application with system key. 使用系统密钥签署您的Android应用程序。

I found the answer. 我找到了答案。 Although I'm not sure what is the exact reason but this works. 虽然我不确定究竟是什么原因但这有效。 I referred to this blog https://shoewann0402.github.io/2017/06/27/android-n-installSilent-and-uninstallSilent/ , but it's all Chinese. 我提到了这个博客https://shoewann0402.github.io/2017/06/27/android-n-installSilent-and-uninstallSilent/ ,但这都是中国人。 In case you also understand Chinese or you use Google translation. 如果您也了解中文或使用Google翻译。

The new command should be like this: 新命令应该是这样的:

pm install -i <you installer package name> --user 0 /sdcard/com.ifeng.news2.apk

This is some new security mechanism added by Google and it requires you to install using userId 0. Also you need to specify the installer package name. 这是Google添加的一些新安全机制,它要求您使用userId 0进行安装。此外,您还需要指定安装程序包名称。

Then in your installer app's manifest file, add android.permission.INTERACT_ACROSS_USERS_FULL this permission. 然后在安装程序应用程序的清单文件中,添加android.permission.INTERACT_ACROSS_USERS_FULL此权限。 Then it should work. 然后它应该工作。 I saw there are some docs about the userId but just don't have enough time to dig into it. 我看到有一些关于userId的文档但是没有足够的时间深入研究它。 If you are interested, you can search by yourself. 如果您有兴趣,可以自己搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM