安卓 9.0 适配方案和踩坑

 
 

code小生,一个专注 Android 领域的技术平台

公众号回复 Android 加入我的安卓技术群

作者:安卓搬砖人
链接:https://www.jianshu.com/p/fd1c1c62582b
声明:本文已获
安卓搬砖人授权发表,转发等请联系原作者授权

年初的时候就已经适配了安卓9.0,但由于业务需求一直没有使用上,前段时间发布了,结果有用户反馈在安卓9.0的手机上更新下载App发生了闪退。这个时候发现9.0对权限、加密和Apache HTTP client发生了相关变化。

一. 首先我遇到的第一个错误是:

Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.protocol.BasicHttpContext" on path: DexPathList”,查找了一下原因:

非Activity-Context启动Activity,现在强制执行 FLAG_ACTIVITY_NEW_TASK 要求,Apache HTTP 客户端弃用,影响采用非标准 ClassLoader 的应用。

在项目中用到了 Apache HTTP client 的相关类,就会抛出找不到这些类的错误。这是因为官方已经在 Android P 的启动类加载器中将其移除,如果仍然需要使用 Apache HTTP client.

这种问题的解决方法有两种:

  1. 在 Manifest 文件中加入:

<uses-library      android:name="org.apache.http.legacy"      android:required="false" />
  1. 可以直接将 Apache HTTP client 的相关类打包进 APK 中。

二. 第二个错误是:

CLEARTEXT communication to life.115.com not permitted by network security policy:
原因是:Android P 限制了明文流量的网络请求,非加密的流量请求都会被系统禁止掉;

解决方案:
第一步,在资源文件新建xml目录,新建文件 network_security_config,名字可以自己命名。

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

第二步,在AndroidManifest中引用

<application    android:name=".app.SophixStubApplication"    android:allowBackup="true"    android:icon="@mipmap/jycicon"    android:label="@string/app_name"    android:networkSecurityConfig="@xml/network_security_config"    android:supportsRtl="true"    android:theme="@style/AppTheme">    <uses-library      android:name="org.apache.http.legacy"      android:required="false" /></application>

    <uses-library
      android:name="org.apache.http.legacy"
      android:required="false" />

</application>

三. 第三个问题就是:

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

问题的原因是:Context中有一个startActivity方法,Activity继承自Context,重载了startActivity方法。如果使用 Activity的startActivity方法,不会有任何限制,而如果使用Context的startActivity方法的话,就需要开启一个新 的task,遇到上面那个异常的,都是因为使用了Context的startActivity方法。

解决办法就是:
加一个flag,intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

其中这个地方我是用于下载apk的,具体的代码如下:

Intent intent2 = new Intent(Intent.ACTION_VIEW);            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {              intent2.setAction("android.intent.action.VIEW");              intent2.addCategory("android.intent.category.DEFAULT");              intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_GRANT_READ_URI_PERMISSION);              Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.jyc99.jyc.fileprovider", responseInfo.result);              intent2.setDataAndType(contentUri, "application/vnd.android.package-archive");            } else {              intent2.setAction("android.intent.action.VIEW");              intent2.addCategory("android.intent.category.DEFAULT");              intent2.setDataAndType(Uri.fromFile(responseInfo.result), "application/vnd.android.package-archive");              intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
              intent2.setAction("android.intent.action.VIEW");
              intent2.addCategory("android.intent.category.DEFAULT");
              intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_GRANT_READ_URI_PERMISSION);
              Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.jyc99.jyc.fileprovider", responseInfo.result);
              intent2.setDataAndType(contentUri, "application/vnd.android.package-archive");
            } else {
              intent2.setAction("android.intent.action.VIEW");
              intent2.addCategory("android.intent.category.DEFAULT");
              intent2.setDataAndType(Uri.fromFile(responseInfo.result), "application/vnd.android.package-archive");
              intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }

其中特别注意是:intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_GRANT_READ_URI_PERMISSION);

四. Android 9.0 加密报错

问题原因:The Crypto provider has been deleted in Android P (and was deprecated in Android N), so the code will crash.

报错代码:SecureRandom.getInstance(SHA1PRNG, "Crypto"),这个是因为Crypto provider 在Android9.0中已经被Google删除了,调用的话就会发生crash。

使用如下方法生成SecretKeySpec 对象:

private static SecretKeySpec deriveKeyInsecurely(String password) {   byte[] passwordBytes = password.getBytes(StandardCharsets.US_ASCII);  return new SecretKeySpec(InsecureSHA1PRNGKeyDerivator.deriveInsecureKey(passwordBytes, AESUtils.KEY_SIZE), "AES"); }
  byte[] passwordBytes = password.getBytes(StandardCharsets.US_ASCII);
  return new SecretKeySpec(InsecureSHA1PRNGKeyDerivator.deriveInsecureKey(passwordBytes, AESUtils.KEY_SIZE), "AES"); 
}

五. 其他Api的修改

其中错误信息是:

java.lang.IllegalArgumentException: Invalid Region.Op - only INTERSECT and DIFFERENCE are allowed

解决方案是:

if (Build.VERSION.SDK_INT >= 26) {                    canvas.clipPath(mPath);                } else {                    canvas.clipPath(mPath, Region.Op.REPLACE);                }26) {
                    canvas.clipPath(mPath);
                } else {
                    canvas.clipPath(mPath, Region.Op.REPLACE);
                }

以上就是我在适配安卓9.0系统中所遇到的坑,每次安卓新版本发布都需要充分了解其中更新的新特新,这样可以避免少踩坑,少改bug。这些适配肯定还不够完善,有补充的大神可以留言评论告知,谢谢了!

推荐阅读
Android 一起来看看 7.0 的新特性 FileProvider
Android 全面屏启动页适配的一些坑

640?wx_fmt=jpeg

扫一扫 关注我的公众号

如果你想要跟大家分享你的文章,欢迎投稿~

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值