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

标签 android apk parsing-error

我的目标是将应用更新托管到远程服务器并通过 API 检查更新。应用程序将检查定义的 url 以检查更新,如果响应版本代码大于当前代码,它将从服务器下载更新的 apk 并通过代码安装它。我托管了一个 PHP 文件,用于检查服务器的版本和 apk 文件。应用程序正在下载 apk 没有任何问题。但是下载后,安装文件时,显示"Parse Error : There is a problem parsing the package."
我搜索了 stackoverflow 和谷歌来解决这个问题,但找不到任何合适的解决方案来解决这个问题并安装更新。已提供所有必需的许可。文件提供程序正常工作。下载的文件也是手动安装的,并且已经运行没有任何问题。但是以编程方式安装它时会出现唯一的问题。

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

    minSdkVersion 21
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"

和更新版本的 gradle.bulid 文件配置:
    minSdkVersion 21
    targetSdkVersion 28
    versionCode 2
    versionName "1.1"

以下是 list 中的:
<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"/>

list 中声明的​​文件提供程序:
    <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

(文件在下载文件夹中,手动安装没有问题)

下载完成下载管理器广播的 Intent 后,应用程序通过此接收器捕获该 Intent 并尝试安装更新的 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 中的教程和问答,这段代码是正确的,但在以编程方式安装时仍然抛出解析错误。

期待得到社区的帮助来解决这个问题。我随时准备分享项目的任何相关文件或 fragment 。

提前致谢。

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

最佳答案

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

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

关于android - 以编程方式更新应用程序时解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55649520/

相关文章:

android - 相同的代码库、不同的 APK、不同的资源文件和不同的 Web 服务 URL

android - 在获取电子邮件附件名称时获取权限错误 java.lang.SecurityException : Permission Denial on 3. x Android 设备

android - 在共享首选项中保存大型数组列表

android - 典型的 ZIP 文件和 APK 文件之间有区别吗?

java - pm 命令给出段错误

c++ - 奇怪的编译器错误

java - Android fragment 与回收器 View 通信

android - 使用 Adob​​e Reader 在 Android 中打开 PDF

javascript - MathJax 不呈现 "less-than sign"

reactjs - create-react-app 中 EXTEND_ESLINT=true 时 typescript 解析错误