Android 9.0 適配指南

國內(nèi)從去年開始就有消息說,應(yīng)用上架或者更新要求TargetSdkVersion最低要為26以上,也就是最低也要適配到8.0。今年來也都逐步地開始落實(shí)。

還包括從8月份開始在Google Play上發(fā)布的應(yīng)用必須支持64位架構(gòu)??梢钥吹竭m配工作真的不能像以前一樣隨心所欲了。好在我之前也有寫過相關(guān)的適配攻略,Android適配系列:

Android 6.0 的動(dòng)態(tài)權(quán)限管理
Android 7.0脫坑指南
Android 8.0適配指北

1.Http請求失敗

在9.0中默認(rèn)情況下啟用網(wǎng)絡(luò)傳輸層安全協(xié)議 (TLS),默認(rèn)情況下已停用明文支持。也就是不允許使用http請求,要求使用https。
比如我使用的是okhttp,會(huì)報(bào)錯(cuò):

java.net.UnknownServiceException: CLEARTEXT communication to xxxx not permitted by network security policy

解決方法是需要我們添加網(wǎng)絡(luò)安全配置。首先在 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添加:

<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
    <application android:networkSecurityConfig="@xml/network_security_config">
          ...
    </application>
</manifest>

以上這是一種簡單粗暴的配置方法,要么支持http,要么不支持http。為了安全靈活,我們可以指定支持的http域名:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<!-- Android 9.0 上部分域名時(shí)使用 http -->
<domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">secure.example.com</domain>
    <domain includeSubdomains="true">cdn.example1.com</domain>
</domain-config>
</network-security-config>

當(dāng)然不止這些配置,還有抓包配置、設(shè)置自定義CA以及各種場景下靈活的配置,詳細(xì)的方法可以查看官方文檔。

2.Apache HTTP 客戶端棄用

在 Android 6.0 時(shí),就已經(jīng)取消了對(duì) Apache HTTP 客戶端的支持。從 Android 9.0 開始,默認(rèn)情況下該庫已從 bootclasspath 中移除。但是耐不住有些SDK中還在使用,比如我見到的友盟QQ分享報(bào)錯(cuò)問題。
所以要想繼續(xù)使用Apache HTTP,需要在應(yīng)用的 AndroidManifest.xml 文件中添加:

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

3.前臺(tái)服務(wù)

可以試著搜索一下你的代碼,看是否有調(diào)用startForegroundService 方法來啟動(dòng)一個(gè)前臺(tái)服務(wù)。

Intent intentService = new Intent(this, MyService.class);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    startForegroundService(intentService);
} else {
    startService(intentService);
}

9.0 要求創(chuàng)建一個(gè)前臺(tái)服務(wù)需要請求 FOREGROUND_SERVICE 權(quán)限,否則系統(tǒng)會(huì)引發(fā) SecurityException。

java.lang.RuntimeException: Unable to start service com.weilu.test.MyService@81795be with Intent { cmp=com.weilu.test/.MyService }: 
java.lang.SecurityException: Permission Denial: startForeground from pid=28631, uid=10626 requires android.permission.FOREGROUND_SERVICE
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3723)
    at android.app.ActivityThread.access$1700(ActivityThread.java:201)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1705)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:6820)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)

解決方法就是AndroidManifest.xml中添加FOREGROUND_SERVICE權(quán)限:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

4.啟動(dòng)Activity

在9.0 中,不能直接非 Activity 環(huán)境中(比如Service,Application)啟動(dòng) Activity,否則會(huì)崩潰報(bào)錯(cuò):

java.lang.RuntimeException: Unable to create service com.weilu.test.MyService: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3578)
    at android.app.ActivityThread.access$1400(ActivityThread.java:201)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1690)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:6820)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)

這類問題一般會(huì)在點(diǎn)擊推送消息跳轉(zhuǎn)頁面這類場景,解決方法就是 Intent 中添加標(biāo)志FLAG_ACTIVITY_NEW_TASK,

Intent intent = new Intent(this, TestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容