Android api 从26 开始就做了很大的变化为了适应变化,需要将各种兼容问题整理,做笔记,方便自己查看,也方便大家解决问题
google 真的很任性,很多代码说删说改就改,没办法,只有跟着走
Android O 对应安装进行的权限的限制
需要引入安装权限
Android P对http网络请求的约束
在Android P上,默认不允许直接使用http的请求,需要使用https
Android版本28使用http请求报错not permitted by network security policy
推荐的做法是服务器和本地应用都改用 https ,测试时为了方便使用http,上线时应该都会用https才比较安全。
解决办法:1.在 res 下新建一个 xml 目录,然后创建一个名为:network_security_config.xml 文件 ,该文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
在 AndroidManifest.xml application增加配置android:networkSecurityConfig="@xml/network_security_config"
调用Intent 安装应用程序的时候
public static void installApks(Activity activity, File apkFile) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(apkFile);
intent.setDataAndType(uri,"application/vn.android.package-archive");
activity.startActivity(intent);
//TODO N FileProvider
//TODO O INSTALL PERMISSION
}
android N:
android.os.FileUriExposedException: file:///data/user/0/com.zcwfeng.face/cache/target.apk exposed beyond app through Intent.getData()
认为分享file uri是不安全的不允许
第一种:你可能会遇到 Didn't find class "android.support.v4.content.FileProvider" on path:
你需要加上 implementation 'com.android.support:multidex:1.0.2'
并在application中加上
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(base);
}
第二种:implementation 'com.android.support:support-v4:28.0.1'或者26.x.x
这种有可能有版本不一致问题
第三种: AndroidX处理
If you have migrated to AndroidX you have to change the name in AndroidManifest.xml to androidx.core.content.FileProvider
AndroidMenifest.xml 修改:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/fileproviderpath" />
</provider>
xml 下的文件自定义文件名fileproviderpath
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<root-path name="root" path="."></root-path>
<files-path
name="file"
path="."></files-path>
<cache-path
name="cache"
path="."></cache-path>
<external-cache-path
name="external_cache"
path="."></external-cache-path>
<external-path
name="external"
path="."></external-path>
<external-files-path
name="external_file"
path="."></external-files-path>
</paths>
接下来你可能会遇到
com.google.android.packageinstaller E/InstallStart: Requesting uid 10085 needs to declare permission android.permission.REQUEST_INSTALL_PACKAGES
添加进入权限即可,我的权限是这样
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.zcwfeng.demo.provider.READ"/>
<uses-permission android:name="com.zcwfeng.demo.provider.WRITE"/>
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
代码兼容性变化修改:
public static void installApks(Activity activity, File apkFile) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
Uri uri = null;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
uri = FileProvider.getUriForFile(activity,activity.getPackageName()+".fileprovider",apkFile);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}else {
uri = Uri.fromFile(apkFile);
}
intent.setDataAndType(uri,"application/vnd.android.package-archive");
activity.startActivity(intent);
}
网友评论