Android加固后app检查版本更新解析错误的问题解决

本文分享了解决Android应用更新时出现的解析错误问题。通过调整安装代码,引入FileProvider和适配不同Android版本的解决方案,成功解决了从6.0到9.0版本的更新问题。

之前很奇怪的就是版本更新apk下载完后直接就调起安装apk直接解析错误,我开始以为是加固的问题,因为我已开始用的是上一个版本的乐加固,后面更新了乐加固版本发现还是不行,继续解析错误,很奇怪,我的流程是先生成正式apk,再加固,加固完在二次签名,然后把这个apk给服务器那边,之前的版本更新估计做好后都没测过,导致我这更新直接就解析错误,我看了下之前的版本安装的代码:

    private void openFile(File file) {
        // TODO Auto-generated method stub
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        intent.setAction(android.content.Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file),
                "application/vnd.android.package-archive");
        startActivity(intent);
    }

显然这没有考虑版本问题,我这6.0版本都解析错误,7.0和8.0,9.0也是,然后参考了多方资料最后改成这样:
1.java代码修改


    public void newOpenFile(File mapkFile){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
//            String[] command = {"chmod", "777", dir.getPath()+"jtzs.apk" };
//            ProcessBuilder builder = new ProcessBuilder(command);
//            try {
//                builder.start();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
            intent.setDataAndType(Uri.fromFile(mapkFile), "application/vnd.android.package-archive");
        } else {
            Uri uri = FileProvider.getUriForFile(mContext, "com.uroad.modulemain.myprovider", mapkFile);
            intent.setDataAndType(uri, "application/vnd.android.package-archive");
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
        mContext.startActivity(intent);
    }

2.(AndroidManiFest修改)
注释掉的那段代码也是参考别人的博客,然后发现没有什么软用。

 <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.uroad.modulemain.myprovider"
            android:exported="false"
            android:grantUriPermissions="true">

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

        </provider>

3.file_provider_paths.xml增加:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path name="my_download" path="."/>
</paths>

注意两点:
1.file_provider_paths里面的path="."不要改,里面就是. 之前我的是download直接报空指针android.content.pm.ProviderInfo.loadXmlMetaData错误。
2.android:authorities和java文件中的要一致。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值