1

I am trying to update my app programatically but It is always giving me error that There was a problem while parsing the package. I am also not able get the root cause of this error.

I have tried in both mode- debug and release but nothing happend. I have tried this library also but getting same error. 'https://github.com/Blankj/AndroidUtilCode'

One thing more- I am able to update the apk manually.

This is my code: -

private fun installUpdate() {
        val file = File("/storage/emulated/0/app-debug.apk")
        val uri: Uri
        if (file.exists()) {
            println("File Exists")
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val unknownSourceIntent =
                    Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).setData(
                        Uri.parse(
                            String.format(
                                "package:%s",
                                packageName
                            )
                        )
                    )
                if (!packageManager.canRequestPackageInstalls()) {
                    startActivityForResult(
                        unknownSourceIntent,
                        UNKNOWN_RESOURCE_INTENT_REQUEST_CODE
                    )
                } else {
                    val fileUri = FileProvider.getUriForFile(
                        this,
                        applicationContext.packageName + ".provider",
                        file
                    )
                    val intent = Intent(Intent.ACTION_VIEW, fileUri)
                    intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true)
                    intent.setDataAndType(fileUri, "application/vnd.android" + ".package-archive")
                    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                    startActivity(intent)
                }
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                val intent1 = Intent(Intent.ACTION_INSTALL_PACKAGE)
                uri = FileProvider.getUriForFile(
                    applicationContext,
                    BuildConfig.APPLICATION_ID + ".provider",
                    file
                )
                grantUriPermission(
                    "com.myelsadev.updateapp",
                    uri,
                    Intent.FLAG_GRANT_READ_URI_PERMISSION
                )
                grantUriPermission(
                    "com.myelsadev.updateapp",
                    uri,
                    Intent.FLAG_GRANT_WRITE_URI_PERMISSION
                )
                intent1.setDataAndType(
                    uri,
                    "application/*"
                )
                intent1.flags = Intent.FLAG_ACTIVITY_NEW_TASK;
                intent1.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent1.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                startActivity(intent1);
            } else {
                val intent = Intent(Intent.ACTION_VIEW)

                uri = Uri.fromFile(file)

                intent.setDataAndType(
                    uri,
                    "application/vnd.android.package-archive"
                )
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                startActivity(intent)
            }
        } else {
            println("File does not exits")
        }
    }
everything seems fine but don't know why I am always getting the error.
I am running it on my phone(oreo)

error screenshot is attached

Jsowa
  • 9,104
  • 5
  • 56
  • 60

0 Answers0