Android 兼容(一) 使用杂项

Android api 从26 开始就做了很大的变化为了适应变化,需要将各种兼容问题整理,做笔记,方便自己查看,也方便大家解决问题

google 真的很任性,很多代码说删说改就改,没办法,只有跟着走

兼容问题一

Materal 控件之Androidx下的BottomNavigationView的使用注意事项

网上说了很多的解决问题,大体有两种思路,说的不够全

  1. java8 的问题,但是不是就解决问题的关键点

指定JAVA8 问题

compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
  1. Caused by: Error inflating class com.google.android.material.bottomnavigation.BottomNavigationView Android5.0~6.0之间的兼容性问题

不同的资源文件目录可能对应着不同的android版本号,比如上图所示drawable-v24对应的版本是android7.0以上,mipmap-anydpi-v26对应的版本是android8.0以上。

原因就是你在使用图片时候找不到BootomNavigationView对应的图片文件夹 在7.0 以下的机器直接崩溃。

解决:

  • 把drawable-24 下面或者用到的相关图片放到drawable下面

解决兼容通用思路

问题分析比较复杂项目,靠猜测,实验,验证
由于BottomNavigationView是android.material包里的控件,报错的原因首先考虑兼容性问题,下面逐一分析:

多版本运行

  • 系统版本导致兼容性分析 。依次在Android6.0、Android7.0、Android8.0虚拟机上面测试,测试结果如下:android6.0-闪退,android7.0、8.0-成功运行。得出结论:android7.0以下程序运行闪退,而android7.0以上则成功运行。

排除权限类别问题

  • 排除android7.0增加的FileProvider导致的崩溃问题

新建官方demo对比

  • 新建一个带Bottom Navigation Activity Project,观察其在android5.0和6.0的运行情况。结果发现刚新建的Project运行正常。

源代码对比

  • 通过Beyond Compare 对比两个项目之间的差异,发现新建的带BottomNavigationView 的项目,其BottomNavigationView 的menu属性中指定的icon的资源是放在drawable 目录下

对比发现报错的project 中BottomNavigationView 的menu属性中的icon属性指定的图片则放在了drawable-v24目录下(比如下图tabbar_home.png只有drawable-v24目录下才有),众所周知,drawable-v24目录是android7.0以上才能访问的资源目录,同理还有mipmap-anydpi-v26也是android8.0以上才能访问的资源目录。

兼容问题二

权限相关
整理一:内部升级功能,需要安装权限等的兼容适配

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);
}

兼容问题三

网络属性不兼容 android:usesCleartextTraffic="true"

android:usesCleartextTraffic 指示应用程序是否打算使用明文网络流量,例如明文HTTP。目标API级别为27或更低的应用程序的默认值为“ true”。面向API级别28或更高级别的应用默认为“ false”。

当属性设置为“ false”时,平台组件(例如,HTTP和FTP堆栈,DownloadManager和MediaPlayer)将拒绝应用程序使用明文流量的请求。强烈建议第三方库也采用此设置。避免明文通信的主要原因是缺乏机密性,真实性和防篡改保护;网络攻击者可以窃听所传输的数据,并且还可以对其进行修改而不会被检测到。

<application android:usesCleartextTraffic="true">
        <activity android:name=".WebviewActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar"></activity>
    </application>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容