以编程方式更新应用程序时解析错误

问题描述 投票:4回答:1

我的目标是将应用程序更新托管到远程服务器,并通过API检查更新。应用程序将检查定义的url以检查更新,如果响应版本代码大于当前代码,它将从服务器下载更新的apk并按代码安装。我托管了一个PHP文件,用于检查服务器的版本和apk文件。应用正在下载apk,没有任何问题。但是下载后,在安装文件时,它显示为"Parse Error : There is a problem parsing the package."

我搜索了stackoverflow和google来解决此问题,但找不到任何合适的解决方案来解决此问题并安装更新。已提供所有必需的许可。文件提供程序正常工作。下载的文件也可以手动安装,并且可以正常运行。但是唯一的问题是通过编程方式安装时出现的。

这是较低版本的gradle.bulid文件配置:

    minSdkVersion 21
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"

和gradle.bulid文件配置为更新版本:

    minSdkVersion 21
    targetSdkVersion 28
    versionCode 2
    versionName "1.1"

以下是清单:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

清单中声明的​​文件提供者:

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true"
        tools:replace="android:authorities">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

提供者文件包含:

    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <external-path path="Android/data/${applicationId}/" name="files_root" />
        <root-path name="root" path="/" />
    </paths>`

现在是编码部分

下载我写的文件

    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(updateURL));
    request.setMimeType("application/vnd.android.package-archive");
    request.setTitle("Downloading " + updateURL.substring(updateURL.lastIndexOf("/") + 1));
    request.allowScanningByMediaScanner();
    //request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    String dir = Environment.DIRECTORY_DOWNLOADS;
    String file = updateURL.substring(updateURL.lastIndexOf("/") + 1);
    request.setDestinationInExternalPublicDir(dir, file);

    downloadPathFile += "/" + file;

    Log.e("Path", downloadPathFile);

    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    manager.enqueue(request);
    registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

logcat显示路径

    E/Path: /storage/emulated/0/Download/app-debug.apk

((文件位于下载文件夹中,并且手动安装没有问题)

由下载管理器广播的下载完成意图之后,应用程序通过此接收器捕获到该信息并尝试安装更新的apk。

BroadcastReceiver onComplete = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent1) {
        Toast.makeText(context, "Download Completed...", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(Intent.ACTION_VIEW);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(MainActivity.this, context.getApplicationContext().getPackageName() + ".provider", new File(downloadPathFile));
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(new File(downloadPathFile)), "application/vnd.android.package-archive");
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

        unregisterReceiver(onComplete);
    }
};

根据Google和stackoverflow中的教程和问答,此代码是正确的,但在以编程方式安装时仍会引发解析错误。

期待获得社区的帮助以解决该问题。我总是准备共享项目的任何相关文件或摘要。

提前感谢。

编辑1:Testing device is Samsung Galaxy J8 Infinity with Android 8.0.0 (API 26)

android apk parsing-error
1个回答
0
投票

我有同样的问题。在Android 8.0.0(API 26)中,有一个选项可以检查canRequestPackageInstalls ()

这可能是您解决问题的方法。查找更多here

© www.soinside.com 2019 - 2024. All rights reserved.