使用INSTALL_PACKAGES

时间:2018-05-30 23:29:36

标签: android android-source android-package-managers android-install-apk

我开发了一个使用系统权限运行的应用程序(它内置于AOSP的图像中)并具有INSTALL_PACKAGES权限。

我的目标是在没有用户互动的情况下在后台静默安装APK。我在stackoverflow上阅读了不同的方法,但是大多数方法要么过时,要么根本不起作用。

例如,如果我尝试使用PackageManager(通过反射或通过在我的应用程序中作为shell命令运行),我得到以下错误,这就是我当前卡住的地方。

11-20 19:06:10.999  4965  4965 D AndroidRuntime: >>>>>> START 

com.android.internal.os.RuntimeInit uid 10039 <<<<<<
11-20 19:06:11.199  4965  4965 D AndroidRuntime: Calling main entry com.android.commands.pm.Pm
11-20 19:06:11.268  4965  4965 E Pm      : Error
11-20 19:06:11.268  4965  4965 E Pm      : java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.AppOpsManager.checkPackage(int, java.lang.String)' on a null object reference
11-20 19:06:11.268  4965  4965 E Pm      :  at android.os.Parcel.readException(Parcel.java:2010)
11-20 19:06:11.268  4965  4965 E Pm      :  at android.os.Parcel.readException(Parcel.java:1950)
11-20 19:06:11.268  4965  4965 E Pm      :  at android.content.pm.IPackageInstaller$Stub$Proxy.createSession(IPackageInstaller.java:254)
11-20 19:06:11.268  4965  4965 E Pm      :  at com.android.commands.pm.Pm.doCreateSession(Pm.java:607)
11-20 19:06:11.268  4965  4965 E Pm      :  at com.android.commands.pm.Pm.runInstall(Pm.java:431)
11-20 19:06:11.268  4965  4965 E Pm      :  at com.android.commands.pm.Pm.run(Pm.java:150)
11-20 19:06:11.268  4965  4965 E Pm      :  at com.android.commands.pm.Pm.main(Pm.java:107)
11-20 19:06:11.268  4965  4965 E Pm      :  at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
11-20 19:06:11.268  4965  4965 E Pm      :  at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:285)
11-20 19:06:11.270  4965  4965 I app_process: System.exit called, status: 1
11-20 19:06:11.270  4965  4965 I AndroidRuntime: VM exiting with result code 1.

有人可以告诉我为什么会出现这个错误,或者其他解决方案或用INSTALL_PACKAGES静默安装APKS的“现代”方法吗?

任何帮助表示感谢。

修改

对于将此标记为nullpointer异常的副本的人 - 如何使连接成为重复?我特别询问其背后的原因,而不是关于nullpointer异常本身。您的重复建议毫无意义

编辑2:

我的代码尝试安装APK(从AOSP源的软件包安装程序应用程序复制):

public boolean install(Context context) {

    Log.d(TAG, "Start Installation");

    String packageName = "com.spotify.music";
    String apkPath = Environment.getExternalStorageDirectory().toString() + "/APKs/spotify.apk";

    PackageInstaller.Session session;
    int sessionId;
    PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL);
    params.setAppPackageName(packageName);

    try {
        sessionId = context.getPackageManager().getPackageInstaller().createSession(params);
        session = context.getPackageManager().getPackageInstaller().openSession(sessionId);
    } catch (IOException e) {
        Log.w(TAG, "Error creating/opening the installing session", e);
        return false;
    }

    try {
        File file = new File(apkPath);

        try (InputStream in = new FileInputStream(file)) {
            long sizeBytes = file.length();
            try (OutputStream out = session.openWrite("PackageInstaller", 0, sizeBytes)) {
                byte[] buffer = new byte[1024 * 1024];
                while (true) {

                    int numRead = in.read(buffer);

                    if (numRead == -1) {
                        session.fsync(out);
                        break;
                    }

                    out.write(buffer, 0, numRead);
                    if (sizeBytes > 0) {
                        float fraction = ((float) numRead / (float) sizeBytes);
                        Log.d(TAG, "Install progress: " + fraction);
                    }
                }
            }
        }

        Log.d(TAG, "Install finished");
        session.commit(PendingIntent.getBroadcast(context, sessionId, new Intent("android.intent.action.MAIN"), 0).getIntentSender());

        return true;
    } catch (IOException | SecurityException e) {
        Log.e(TAG, "Could not write package", e);

        session.close();

        return false;
    }
}

1 个答案:

答案 0 :(得分:1)

我们使用以下代码:

Uri apkUri = Uri.fromFile(apkFile);
IPackageInstallObserver observer = null;
String installerPackageName = "MyInstaller";
getPackageManager().installPackage(apkUri, observer, PackageManager.INSTALL_REPLACE_EXISTING, installerPackageName);

它已被弃用,但即使在Android 8.1上仍可使用。