Huawei's mobile phone prompts that parsing package fails when updating and installing in APP after Android is consolidated

Keywords: Mobile Android FileProvider xml

Recently, I found a problem. When my apk was strengthened with 360, I detected the update in the app, downloaded the new apk, and the Huawei mobile phone prompted "parsing package failed" when I installed it. Other mobile phones are OK.
Try to find out Solution
I set System.exit(0); I think it's the same as removing killProcess. The results are tested and found to be true. Here's the record

Attach the code to download apk from server to local and install:

private void startDownload(String url) {
        File dir = createApkFile();
        if (null == dir) {
            toast("No, SD Card!");
            return;
        }
        if (null == dialog) {
            downLoadDialog = new DownloadProgressDialog(this);
            downLoadDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    download.stop();
                }
            });
        }
        download.workOn(url, dir.getAbsolutePath());
        downLoadDialog.show();
    }

    private File createApkFile() {
        File newFile = null;
        try {
            File path = new File(getExternalCacheDir(), "apk");
            path.mkdirs();
            newFile = File.createTempFile("new", ".apk", path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return newFile;
    }
    
     void install(){
        File file = new File(path);//Address where the file is saved locally
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "Package name.provider", file);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        }
        startActivity(intent);
//            System.exit(0);
    }

In addition, you need to configure it in Android manifest:

 <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true"
            android:readPermission="com.company.app.fileprovider.READ"

            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"/>
        </provider>

Posted by nathus on Mon, 09 Dec 2019 21:42:38 -0800