國內(nèi)從去年開始就有消息說乍赫,應(yīng)用上架或者更新要求TargetSdkVersion最低要為26以上坦康,也就是最低也要適配到8.0惨奕。今年來也都逐步地開始落實(shí)揖赴。
還包括從8月份開始在Google Play上發(fā)布的應(yīng)用必須支持64位架構(gòu)馆匿。可以看到適配工作真的不能像以前一樣隨心所欲了储笑。好在我之前也有寫過相關(guān)的適配攻略甜熔,Android適配系列:
Android 6.0 的動(dòng)態(tài)權(quán)限管理
Android 7.0脫坑指南
Android 8.0適配指北
1.Http請(qǐng)求失敗
在9.0中默認(rèn)情況下啟用網(wǎng)絡(luò)傳輸層安全協(xié)議 (TLS),默認(rèn)情況下已停用明文支持突倍。也就是不允許使用http請(qǐng)求,要求使用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>
以上這是一種簡(jiǎn)單粗暴的配置方法秕磷,要么支持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以及各種場(chǎng)景下靈活的配置易桃,詳細(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ù)需要請(qǐng)求 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)頁面這類場(chǎng)景晰绎,解決方法就是 Intent 中添加標(biāo)志FLAG_ACTIVITY_NEW_TASK,
Intent intent = new Intent(this, TestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);