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?
PackageInstallerService
it grabs a reference tomContext.getSystemService(AppOpsManager.class)
at system start up. But in your case it's null. You said you have the system signature. Are you building a custom ROM? Did you remove/disable app ops service? Is the system fully ready at the time you're attempting the install? What version of Android is this? – Vanzandt