年初的时候就已经适配了安卓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-configcleartextTrafficPermitted="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 = newIntent(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 对象:

privatestaticSecretKeySpec deriveKeyInsecurely(String password){
byte[] passwordBytes = password.getBytes(StandardCharsets.US_ASCII);
returnnewSecretKeySpec(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。这些适配肯定还不够完善,有补充的大神可以留言评论告知,谢谢了!