版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kururunga/article/details/86509892
之前很奇怪的就是版本更新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文件中的要一致。