美文网首页
通过Intent安装apk

通过Intent安装apk

作者: JieeOS | 来源:发表于2019-12-18 17:24 被阅读0次

    最近接收一个老项目,发现在Android Q版本上自动更新的时候出错,表现为下载完app后直接崩溃。进入文件管理器后发现安装包已经下载成功了,点击安装后能正常安装,但这是文件管理器调用系统的安装,项目中调用系统安装肯定是有问题的。
    查看gradle文件

    android {
        compileSdkVersion 23
        buildToolsVersion "29.0.2"
        defaultConfig {
            applicationId "com.*****.*****"
            minSdkVersion 14
            targetSdkVersion 23
            versionCode 2
            versionName "2.4.2"
            multiDexEnabled true
            ndk {
                //选择要添加的对应cpu类型的.so库
                abiFilters "armeabi", "armeabi-v7a"
                // 'armeabi-v8a', 'x86', 'x86_64', 'mips', 'mips64'
            }
        }
    }
    

    编译版本和目标版本都是23,项目中提示要更新到29。因为对29新特性不是特别了解,不敢贸然升级。
    再看看Manifest文件,权限有很多,这里就不贴了。查了下网上对安装apk的权限,更新app以及安装app需要的权限如下

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.REPLACE_EXISTING_PACKAGE"/>
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/><!-- 8.0必要权限  -->
    

    项目中的权限列表中没发现REPLACE_EXISTING_PACKAGE和REQUEST_INSTALL_PACKAGES这两项权限,猜想是再其他的引用中加入了这些权限。因为在AndroidQ以下的手机能安装成功。
    再往下看发现一个问题,manifest中缺少provider的节点。网上查了下关于安装apk所需的provider

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.fileprovider"  <!--定义provider后面取值需要用到,名字要一致-->
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />    <!--在xml文件夹内新建文件配置路径-->
    </provider>
    

    @xml/file_paths文件:

    <?xml version="1.0" encoding="utf-8"?>
    <paths>
        <external-path
            name="移动办公" path="/" />
    </paths>
    

    查了下关于该xml的定义:

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <!--Context.getFilesDir() 位于/data/data/安装目录-->
        <files-path name="internalPath" path="file" />
        <!--Context.getCacheDir()-->
        <cache-path name="cachePath" path="file" />
        <!--Environment.getExternalStorageDirectory()-->
        <external-path name="externalPath" path="file" />
        <!--Context.getExternalFilesDir(null)-->
        <external-files-path name="externalFPath" path="file" />
    </paths>
    

    项目中用 <external-path 定义了外置路径,最后在代码中调用安装

    public static boolean installApk(Context context, String apkPath) {
            if (context == null || apkPath.isEmpty()) {
                return false;
            }
            File apkfile = new File(apkPath);
            if (!apkfile.exists()) {
                return false;
            }
            PackageInfo pi = AppPackageUtil.getAPKFilePackageInfo(context, apkPath);
            if (pi == null) {
                apkfile.delete();
                return false;
            }
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (Build.VERSION.SDK_INT >= 24) { //7.p0以上
                Uri apkUri = FileProvider.getUriForFile(context, "com.你的包名.fileprovider", apkfile);//注意这里的fileprovider要和manifest一致
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
            } else { //7.0以下
                //intent.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");
                intent.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive");
            }
            context.startActivity(intent);
            return true;
        }
    

    项目中没有对版本进行适配,缺少24以上的适配,加入以上代码适配后,在Android 10的红米手机上测试了更新后问题解决。

    相关文章

      网友评论

          本文标题:通过Intent安装apk

          本文链接:https://www.haomeiwen.com/subject/ptyunctx.html