android 9.x 实现应用内更新安装
生活随笔
收集整理的這篇文章主要介紹了
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" /><applicationandroid: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-dataandroid: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 permissionif(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>?
轉載于:https://www.cnblogs.com/htsky/p/11404946.html
總結
以上是生活随笔為你收集整理的android 9.x 实现应用内更新安装的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 并发编程--线程池与进程池
- 下一篇: 微信和支付宝中的一些常用方法封装