zoukankan      html  css  js  c++  java
  • android 9.x 实现应用内更新安装

    折腾了3天,总算解决了问题。

    依赖如下

    implementation 'androidx.core:core:1.0.2'
    // implementation 'com.android.support:support-compat:28.0.0'
    support包,试了几个版本总是提示一堆
    Duplicate class android.support.v4. ……
    切换到依赖 androidx,则不能忘记在 androidManifest.xml中替换 name值
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.INTENT" />
        <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
        <application
            android:theme="@style/AppTheme"
            android:networkSecurityConfig="@xml/network_security_config"
            >
            <activity android:name=".UpdateActivity" />
            <provider  android:authorities= "${applicationId}.fileprovider"
                android:name="androidx.core.content.FileProvider"
                android:exported="false"
                android:grantUriPermissions="true" >
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/filepath" />
            </provider>
        </application>

    工具类如下

        public static void installApk(Activity activity, File apkFile){
            Intent intent =new Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //Uri uri = Uri.fromFile(apkFile);
            Uri uri = null;
            //todo N FileProvider
            //todo O install permission
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
                uri = androidx.core.content.FileProvider.getUriForFile(activity,activity.getPackageName()+".fileprovider", apkFile);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            }else{
                uri = Uri.fromFile(apkFile);
            }
            intent.setDataAndType(uri, "application/vnd.android.package-archive");
            activity.startActivity(intent);
        }

    另外为了单纯一点,避免错误提示

    Duplicate class android.support.v4.app.INotificationSideChannel found in modules classes.jar (androidx.core:core:1.0.2) and classes.jar (com.android.support:support-compat:25.3.1)
    Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules classes.jar (androidx.core:core:1.0.2) and classes.jar (com.android.support:support-compat:25.3.1)
    Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules classes.jar (androidx.core:core:1.0.2) and classes.jar (com.android.support:support-compat:25.3.1)
    Duplicate class android.support.v4.os.IResultReceiver found in modules classes.jar (androidx.core:core:1.0.2) and classes.jar (com.android.support:support-compat:25.3.1)
    Duplicate class android.support.v4.os.IResultReceiver$Stub found in modules classes.jar (androidx.core:core:1.0.2) and classes.jar (com.android.support:support-compat:25.3.1)
    Duplicate class android.support.v4.os.IResultReceiver$Stub$Proxy found in modules classes.jar (androidx.core:core:1.0.2) and classes.jar (com.android.support:support-compat:25.3.1)
    Duplicate class android.support.v4.os.ResultReceiver found in modules classes.jar (androidx.core:core:1.0.2) and classes.jar (com.android.support:support-compat:25.3.1)
    Duplicate class android.support.v4.os.ResultReceiver$1 found in modules classes.jar (androidx.core:core:1.0.2) and classes.jar (com.android.support:support-compat:25.3.1)
    Duplicate class android.support.v4.os.ResultReceiver$MyResultReceiver found in modules classes.jar (androidx.core:core:1.0.2) and classes.jar (com.android.support:support-compat:25.3.1)
    Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules classes.jar (androidx.core:core:1.0.2) and classes.jar (com.android.support:support-compat:25.3.1)
    
    Go to the documentation to learn how to Fix dependency resolution errors.

    解决办法为,在gradle.properties中添加如下代码

    android.useAndroidX=true
    android.enableJetifier=true

    最后贴一下 androidManifest.xml中提到的2个xml文件,在res/xml文件夹下

    network_security_config.xml

    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <base-config cleartextTrafficPermitted="true" />
    </network-security-config>

    filepath.xml

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <root-path name="root" path="." />
        <files-path name="files" path="." />
        <external-path name="external" path="." />
        <external-cache-path name="external_cache" path="." />
        <external-files-path name="external_file" path="." />
    </paths>
  • 相关阅读:
    什么是三元表达式,遇到三元表达式,你该如何去看代码执行的结果,下面的方法简单实用!!!
    遍历某一个标签中的内容;python+selenium定位到列表整体,使用for循环获取列表文本;可用于校验列表是否存在你需要的文本内容
    Selenium3+python3--如何定位鼠标悬停才显示的元素
    selenium中get_attribute的简单使用
    css层叠样式
    前端初识
    视图,触发器,事务,存储过程,内置函数,索引
    pymysql基本操作
    多态与绑定方法
    封装与组合
  • 原文地址:https://www.cnblogs.com/htsky/p/11404946.html
Copyright © 2011-2022 走看看