Package Installer not working in Android 12

232 Views Asked by At

The code provided functions correctly on Android versions 10 and earlier. Its purpose is to update the application to a newer version.

val inputStream =
                context.applicationContext.contentResolver.openInputStream(Uri.fromFile(file))
            val length =
                DocumentFile.fromSingleUri(context.applicationContext, Uri.fromFile(file))!!
                    .length()
            val installer = context.applicationContext.packageManager.packageInstaller
            val params = SessionParams(SessionParams.MODE_FULL_INSTALL)
            val sessionId = installer.createSession(params)
            val session = installer.openSession(sessionId)
            val outputStream = session.openWrite(file.name, 0, length)
            val buf = ByteArray(1024)
            var len: Int
            while (inputStream!!.read(buf).also { len = it } > 0) {
                outputStream.write(buf, 0, len)
            }
            session.fsync(outputStream)
            outputStream.close()
            inputStream.close()

            val intent = Intent(context.applicationContext, DeviceReceivers::class.java)

            val pi = PendingIntent.getBroadcast(
                context.applicationContext,
                REQUEST_INSTALL_PACKAGE,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT // FLAG_IMMUTABLE for android 12
            )

            session.commit(pi.intentSender)
            session.close()

However, a problem arises on Android 12 (API 31), where the above code appears to be ineffective. There is one error log, and that has been given below. It's important to note that this application is a device owner application.

Failed query: java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference

stack-trace

Edit

The above issue is only happening to a specific device TB310XU (Lenovo M9) I have checked on another device TB300XU (Lenovo M8 4th Gen) and it was working as expected.

So how to debug this issue to identify what's was the problem with TB310XU device?

Any help to resolve this issue is appreciated. TY

0

There are 0 best solutions below