Looking into the sources of Android (AOSP), the `installPackage` is labled deprecated and inspecting the PackageManager application of Android, it uses the `PackageInstaller` class to create a `PackageInstaller.Session` instance to perform the installation of an APK.

I am trying to do the same in my application. I am signed with the system key and I did include the `INSTALL_PACKAGES` permission in the manifest.

Here's my code:

```
    val packageName = "com.spotify.music"
    val inputStream = File(filesDir, "spotify.apk").inputStream()

    // ...

    val packageInstaller = context.packageManager.packageInstaller
    val params = PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL)
    params.setAppPackageName(packageName)

    val sessionId = packageInstaller.createSession(params)
    val session = packageInstaller.openSession(sessionId)
    val out = session.openWrite("COSU", 0, -1)
    inputStream.copyTo(out)
    session.fsync(out)
    inputStream.close()
    out.close()
    session.commit(null)

```

However, I am getting the following weird null pointer exception:

`Error while installing: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.AppOpsManager.checkPackage(int, java.lang.String)' on a null object reference`

Does anybody have a clue what's going wrong here?