专栏名称: code小生
公众号:「code小生」,乐于分享的码农
目录
相关文章推荐
开发者全社区  ·  昔日同学中的三好学生都翻车了 ·  昨天  
开发者全社区  ·  x航最美 ·  2 天前  
开发者全社区  ·  币圈大瓜! ·  3 天前  
开发者全社区  ·  体制内的一张图 ·  3 天前  
开发者全社区  ·  计算机开始跌落神坛,想不到这么差... ·  3 天前  
51好读  ›  专栏  ›  code小生

安卓 9.0 适配方案和踩坑

code小生  · 掘金  · android  · 2019-07-04 03:03

正文

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>

第二步,在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>

三. 第三个问题就是:

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

其中特别注意是: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"); }

五. 其他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);                }

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







请到「今天看啥」查看全文


推荐文章
开发者全社区  ·  昔日同学中的三好学生都翻车了
昨天
开发者全社区  ·  x航最美
2 天前
开发者全社区  ·  币圈大瓜!
3 天前
开发者全社区  ·  体制内的一张图
3 天前
开发者全社区  ·  计算机开始跌落神坛,想不到这么差...
3 天前
中国好文章  ·  揭开“中华烟3字头”内幕
8 年前
轻松读书人  ·  做朋友,也要有分寸感
7 年前
热门电影图解  ·  没有《权游》追的日子里,就看它!
7 年前