Android12 新特性及適配指南

Android 12(API 31)於2021年10月4日正式發布,正式版源程式碼也於當日被推送到AOSP Android開源項目。截止到筆者撰寫這篇文章時,中國各終端廠商的在售Android設備,已經逐步開啟了Android 12正式版本的更新。當前,對於Android應用開發者來說,Android 12 的軟體兼容適配已迫在眉睫。

對於 Android 12 的兼容適配,主要分為兩類:一類默認影響所有運行的應用另一類則只對聲明 targetSdkVersion 31 的應用產生影響。

對於Android 12 的適配點,這裡按照以下幾個方面進行了歸納:

  • 新的應用啟動頁:
    應用啟動頁 SplashScreen(影響所有應用);
  • 聲明 android:exported
    應用組件需顯示聲明 android:exported(以Android12位目標平台的應用);
  • Alarm精確鬧鐘
    應用程式使用Alarm精確鬧鐘 需申請SCHEDULE_EXACT_ALARM許可權(以Android12位目標平台的應用);
  • 通知欄變更:
    Notification通知欄布局樣式再次調整(以Android12位目標平台的應用);
  • 精確位置
    請求精確位置,需同時申請 ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION 許可權(以Android12位目標平台的應用);
  • 前台服務:
    禁止從後台啟動前台服務(以Android12位目標平台的應用);
  • 藍牙許可權:
    申請藍牙相關許可權時,不再需要申請設備位置資訊相關許可權(以Android12位目標平台的應用);

官方文檔描述://developer.android.google.cn/about/versions/12

一、應用啟動頁

(Android 啟動頁 SplashScreen:影響所有應用)

從 Android 12 開始,系統會在應用的冷啟動和暖啟動時,使用新的啟動頁 SplashScreen,該啟動頁默認由應用ICON + 應用主題的windowBackground內容構成。

SplashScreen 啟動頁運行效果

影響在 Andorid 12 設備上運行的所有應用
SplashScreen相關API的引入影響在Andorid 12設備上運行的所有應用。對於應用開發者來說,無論你的應用targetSdkVersion 版本是多少,均需要進行SplashScreen的適配工作。

若未進行 SplashScreen 的適配工作
若開發者未進行SplashScreen的適配工作,當應用運行於Android 12及以上版本的設備,在應用的冷啟動 或 溫啟動時:

  • 若你的應用原本使用 android:windowBackground 實現了啟動頁,會被默認的啟動頁樣式替換。
  • 若你的應用使用了一個額外的 Activity 作為啟動頁,則會先彈出系統默認啟動頁,再彈出你實現的啟動頁 (用戶可能會感受到兩次閃屏效果)。

SplashScreen 自定義與案例程式碼:
新的啟動頁中的顯示元素可完全由由開發者自定義,官方建議開發者:將未適配Android12前前的應用啟動頁完全移除,並適配Android12新的啟動頁,從而避免啟動頁重複、減少載入時間的問題。

關於 SplashScreen 適配相關API的詳細案例程式碼API使用說明請參考文章:
Android 12 適配指南——SplashScreen
//xiaxl.blog.csdn.net/article/details/123522277

二、android:exported

(顯示聲明 android:exported:影響以Android 12為目標平台的應用「targetSdkVersion 31」)

從 Andorid 12 開始,當您的應用程式將目標版本設置為31或更高版本(targetSdkVersion 31)時,若應用程式組件(ActivityServiceReceiverProvider)在配置清單manifest中未顯示聲明 android:exported 屬性,則在進行應用開發或打包時,將會出現如下錯誤提示:

As of Android 12, android:exported must be set; use true to make the activity available to other apps, and false otherwise. For launcher activities, this should be set to true.

對於這一點的更改,官方描述如下圖所示:
官方對於 android:exported 的詳細描述

android:exported = true的作用?

當應用程式組件,需要被另一個應用(Application)的組件啟動或調用時:true 允許調用;false 不允許其他應用啟動或調用。例如:在Activity中用來標示:當前Activity是否可以被另一個Application的組件啟動;

因此,在Android 12中需顯示聲明 android:exported 屬性,舉例如下:

// launcher Activity 需將exported設置為true
<activity
    android:name=".SplashActivity"
    android:exported="true"
    android:theme="@style/Theme.SplashScreen.Demo">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
// 非launcher Activity 又無外部啟動需求設置為fase
<activity
    android:name=".MainActivity"
    android:exported="false">
</activity>

三、Alarm精確鬧鐘

(Alarm精確鬧鐘:影響以Android 12為目標平台的應用「targetSdkVersion 31」)

從 Andorid 12 開始,當您的應用程式將目標版本設置為31或更高版本(targetSdkVersion 31)時,若您的應用程式需要使用精確鬧鐘,需申請一個新的許可權(鬧鐘和提醒許可權),該許可權為普通許可權,無需動態申請:

<!--Android S alarm permission-->
<!--普通許可權:無需動態申請-->
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

對於這一點的更改,官方描述如下圖所示:
官方對於 Alarm精確鬧鐘 的詳細描述

添加該許可權的原因是:Android官方為了節省系統資源,希望應用開發者儘可能將應用調整為不再需要使用精確鬧鐘的狀態,從而減少應用鬧鐘對作業系統的頻繁喚醒。

Android 12 精確鬧鐘許可權

四、Notification通知欄

(Notification通知欄:影響以Android 12為目標平台的應用「targetSdkVersion 31」)

從 Andorid 12 開始,系統再次更改了自定義通知欄的布局方式和樣式。

  • Android 12以前,自定義通知欄能夠使用整個通知區域並自定義自己的布局樣式;因此,在不同設備上可能出現布局兼容問題;
  • Android12開始,對於以Android 12 為目標平台的應用,通知欄的自定義試圖將不再使用完整的通知欄區域。系統會提供標準模板,此模板可確保自定義通知欄在所有狀態下效果保持一致。

Android12通知欄調整

Android 12開始,系統提供Notification.DecoratedCustomViewStyle通知欄樣式,用於展示與構建收起與展開狀態的通知欄樣式。

標準模板自定義通知欄的展示樣式,如下圖所示:
標準模板自定義通知欄的展示樣式

標準通知欄的收起狀態:
通知欄的收起狀態
custom-collapsed-view.jpg
標準通知欄的展開狀態:
標準通知欄的展開狀態

相關程式碼的使用方式如下:

// Get the layouts to use in the custom notification
val notificationSmallLayout = RemoteViews(packageName, R.layout.notification_small)
val notificationLargeLayoutExpanded = RemoteViews(packageName, R.layout.notification_large)
// Apply the layouts to the notification
val customNotification = NotificationCompat.Builder(context, channelId)
    // icon
    .setSmallIcon(R.drawable.ic_launcher)
    // style
    .setStyle(NotificationCompat.DecoratedCustomViewStyle())
    // 設置收起後通知布局
    .setCustomContentView(notificationSmallLayout)
    // 設置展後的通知布局
    .setCustomBigContentView(notificationLargeLayoutExpanded)
    .build()
notificationManager.notify(1, customNotification);

註:通知的背景顏色可能會因設備和系統版本而有所差異。因此,開始在在自定義布局中建議對文本使用Style:TextAppearance_Compat_Notification,對標題使用Style: TextAppearance_Compat_Notification_Title。以上樣式會適應系統的顏色變化,不會出現黑色文本採用黑色背景或白色文本採用白色背景的情況。
舉例如下:

<TextView
    android:id="@+id/notification_title"
    style="@style/TextAppearance.Compat.Notification.Title"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:text="notification_small" />

通知欄 相關官方文檔參考:

Android 12 中的兼容性變更:
//mp.weixin.qq.com/s/ek2UT0vauTeVQQF0K5fkSQ

Android 12 行為變更——自定義通知:
//developer.android.google.cn/about/versions/12/behavior-changes-12?hl=zh-cn

developer自定義通知欄:
//developer.android.google.cn/training/notify-user/custom-notification?hl=zh-cn#kotlin

五、精確位置

(精確位置:影響以Android 12為目標平台的應用「targetSdkVersion 31」)

從 Andorid 12 開始,當您的應用程式將目標版本設置為31或更高版本(targetSdkVersion 31)時,若應用程式請求設備的精確位置,需同時請求 ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION 許可權。
發出精確位置申請後,用戶側設備將彈出動態授權申請彈窗:

精確許可權動態授權彈窗

若開發者只請求ACCESS_FINE_LOCATION許可權,將彈出以下錯誤提示:

ACCESS_FINE_LOCATION must be requested with ACCESS_COARSE_LOCATION.

精確位置 相關官方文檔參考:

developer位置授權:
//developer.android.google.cn/training/location/permissions?hl=zh-cn#approximate-request

Android 12行為變更——大致位置:
//developer.android.google.cn/about/versions/12/behavior-changes-12?hl=zh-cn

六、前台服務

(前台服務:影響以Android 12為目標平台的應用「targetSdkVersion 31」)

從 Andorid 12 開始,當您的應用程式將目標版本設置為31或更高版本(targetSdkVersion 31)時,將禁止從後台啟動前台服務,並對啟動前台服務作了限制。

調整後,以下情況可啟動前台服務:

  • 可見的 Activity 或窗口;
  • 用戶操作,如通知、小部件等等;
  • 特定的廣播和回調;
  • STICKY 類型的服務可在崩潰或由於低記憶體而停止運行的情況下重啟;

七、藍牙許可權

(藍牙許可權:影響以Android 12為目標平台的應用「targetSdkVersion 31」)

Android 12 引入了 BLUETOOTH_SCANBLUETOOTH_ADVERTISEBLUETOOTH_CONNECT 許可權。這些許可權可讓以 Android 應用更輕鬆地與藍牙設備互動,不再需要申請設備位置資訊相關許可權

Android 12 開始,Google官方將藍牙掃描位置許可權進行了分離,因為官方發現:在隱私層面上,很難向終端用戶解釋位置許可權與藍牙的關係

參考

Android developer:Andoid12
//developer.android.google.cn/about/versions/12?hl=zh-cn

Android開發者:Android 12 正式發布
//mp.weixin.qq.com/s/OiFSWEnc-0N2z7JYWTJluw

AOSP:Android 開源項目
//source.android.google.cn/

Material You:
//material.io/blog/announcing-material-you

Material 設計組件:
//github.com/material-components/material-components-android/releases

androidx releases core:
//developer.android.com/jetpack/androidx/releases/core?hl=zh-cn

= THE END =

文章首發於公眾號」CODING技術小館「,如果文章對您有幫助,歡迎關注我的公眾號。
歡迎關注我的公眾號