Android 13(API 33)
On 2022年8月15日
the official release (the release time is earlier than in previous years), the official release source code was also pushed to the AOSP Android open source project on the same day.
As of the writing of this article, some domestic application software developers have gradually received new version adaptation requirements from mobile phone manufacturers (Huami OV, etc.). Currently, for Android application developers, the software compatibility and adaptation of Android 13 has to be put on the work schedule.
In order to fit the title of this article, this article combines the official Android Developer documentation to elaborate on two aspects Android13适配点
.Android13新特性
Adaptation point:
- Subdivide media permissions:
subdivideREAD_EXTERNAL_STORAGE
into IAMGES, VIDEO, AUDIO permissions
(若设置 targetSdk>=33 则此项必需适配!
) - WebView adjustment:
obsoletesetAppCacheEnabled
andsetForceDark
method;
(若设置 targetSdk>=33 则此项必需适配!
) - Static broadcast registration: when
registering静态广播
, you need to set对其他应用的可见性
(若设置 targetSdk>=33 则此项必需适配!
) - Notification permission:
New runtime notification permission:POST_NOTIFICATIONS
(若设置 targetSdk>=33 则此项必需适配!
) - Wi-Fi:
AddNEARBY_WIFI_DEVICES
runtime permissions
(若设置 targetSdk>=33 则此项必需适配!
) - Body sensor background permissions:
addBODY_SENSORS_BACKGROUND
runtime permissions
(若设置 targetSdk>=33 则此项必需适配!
) - Clipboard content hidden:
新增内容隐藏API
(根据业务需求,选择性适配!
) - Restrictions for non-SDK interfaces
(若设置 targetSdk>=33 则此项必需适配!
)
New features:
- Foreground Service Manager:
Added to the system前台服务管理器
(系统新特性无需适配!
)
1. Segmentation of media rights
Starting from Android 13, 以Android13(API 33+)为目标平台的应用
, system 新增
runtime permissions READ_MEDIA_IAMGES
, READ_MEDIA_VIDEO
, READ_MEDIA_AUDIO
替代
original READ_EXTERNAL_STORAGE
permissions.
permissions | Permission Description |
---|---|
READ_MEDIA_IAMGES | Image permissions |
READ_MEDIA_VIDEO | video rights |
READ_MEDIA_AUDIO | audio permissions |
When the app is upgraded to targetSdk>=33
:
- Application of authorized
READ_EXTERNAL_STORAGE
permissions: The system will automatically grant corresponding fine-grained permissions. - Permissions are still requested without authorization
READ_EXTERNAL_STORAGE
: the pro-test system will not grant any permissions.
Subdivided media permission dynamic application pop-up window style:
- If you request
READ_MEDIA_IMAGES
andREAD_MEDIA_VIDEO
permission at the same time, the system will prompt the following permission pop-up window: - If
READ_MEDIA_AUDIO
permission is requested, the system will prompt the following pop-up window:
Official Reference:
Android Developer Segmentation Media Permissions:
Behavior Changes: Apps Targeting Android 13 or Later | Android Developers | Android Developers
Two, WebView
Starting from Android 13, 以Android13(API 33+)为目标平台的应用
WebView has the following methods and API adjustments:
WebSettings.setAppCacheEnabled()
method废弃
.WebSettings.setForceDark()
method废弃
.
2.1 setAppCacheEnabled Obsolete
WebView 95+ version is no longer supported setAppCacheEnabled
Chrome version 85+ no longer supports AppCache
2.2 setForceDark obsolete
Starting from Android 13 以Android13(API 33+)为目标平台的应用
, the system will automatically set the light or dark theme style of WebView according to the theme attribute isLightTheme of the application (the system will set prefers-color-scheme according to isLightTheme). At the same time, the API methods related to setting the theme style of the developer's Webview WebSettings.setForceDark()
will be discarded.
Based on above changes:
- If developers still need to customize the theme color of Webview, they can use the :
WebSettings.setAlgorithmicDarkeningAllowed()
orWebSettingsCompat.setAlgorithmicDarkeningAllowed()
method. - If the developer still uses
WebSettings.setForceDark()
the system, an error will be displayed:
W/cr_WebSettings: setForceDark() is a no-op in an app with targetSdkVersion>=T
Official Reference:
Android Developer Static Broadcast Registration Official Introduction:
Behavior Changes: Apps Targeting Android 13 or Later | Android Developers | Android Developers
3. Static broadcast registration
Starting from Android 13, when 以Android13(API 33+)为目标平台的应用
registering 静态广播
, you need to set 对其他应用的可见性
:
若对其他应用可见
, set when broadcasting registration:Context.RECEIVER_EXPORTED
若仅应用内使用
, set when broadcasting registration:Context.RECEIVER_NOT_EXPORTED
private void registerTestReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction("com.xiaxl.test.action");
// api >= 33
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
// 跨应用间使用
MainActivity.this.registerReceiver(mTestReceiver, filter, Context.RECEIVER_EXPORTED);
// 应用内使用
//MainActivity.this.registerReceiver(mTestReceiver, filter, Context.RECEIVER_EXPORTED);
}
// api <= 32
else {
MainActivity.this.registerReceiver(mTestReceiver, filter);
}
}
Official Reference:
Android Developer Static Broadcast Registration Official Introduction:
Features and API Overview | Android Developers | Android Developers
4. Notification authority
Android 13 introduces a new runtime notification permission: POST_NOTIFICATIONS
. POST_NOTIFICATIONS
The permission level is defined as dangerous
the developer needs to use the permission 动态申请
, waiting for the user to actively authorize:
- For
Android13(API 33+)为目标平台的应用
:
When displaying the Android notification bar, on the one hand, it needs to be declared in the AndroidManifestandroid.permission.POST_NOTIFICATION
, and on the other hand, the code needs to dynamically apply for the permission of the notification bar.
<!-- AndroidManifest权限声明 -->
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xiaxl.test">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
</manifest>
// Java代码动态申请POST_NOTIFICATIONS权限
if (Build.VERSION.SDK_INT >= 33) {
int checkPermission =
ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.POST_NOTIFICATIONS);
if (checkPermission != PackageManager.PERMISSION_GRANTED) {
//动态申请
ActivityCompat.requestPermissions(MainActivity.this, new String[]{
Manifest.permission.POST_NOTIFICATIONS}, PERMISSION_REQUEST_CODE);
} else {
//showRecordNotification();
}
} else {
//showRecordNotification();
}
POST_NOTIFICATIONS 动态授权申请弹窗
as shown below:
- For the target platform
Android12(API 32-)为目标平台的应用
: when running on Android 13 and above devices, when .API 32-
应用第一次显示通知时,系统会自动弹出以下提示框,要求用户动态授权
Official Reference:
Android Developer Notification Runtime Permissions Official Introduction:
Notification Runtime Permissions | Android Developers | Android Developers
5. Wi-Fi permissions
Starting from Android 13, the Android system has added the NEARBY_WIFI_DEVICES permission , which distinguishes the original ACCESS_FINE_LOCATION
permission from the original permission Wi-Fi能力使用
(to avoid the need to request the user's location permission when the developer used the Wi-Fi capability earlier, which caused ambiguity for the user).
Starting from Android 13, as long as developers 不通过Wi-Fi推导用户的物理位置
no longer need to request ACCESS_FINE_LOCATION
permissions, the official summarizes the NEARBY_WIFI_DEVICES
following usage scenarios for new permissions:
Official reference:
Android Developer NEARBY_WIFI_DEVICES:
Manifest.permission | Android Developers
Six, clipboard content hidden
Starting from Android 13 (API 33), a new item has been added to the Android clipboard 新API
:
Starting from Android 13 (API 33), users can choose to use the API PersistableBundle#(ClipDescription.EXTRA_IS_SENSITIVE, true)
to hide sensitive information about user accounts and passwords to be copied to the clipboard.
Examples of related API usage are as follows:
private void addData2Clipboard() {
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clipData = ClipData.newPlainText("111111", "我是密码");
ClipDescription description = clipData.getDescription();
// 隐私内容:剪切板加密
PersistableBundle persistableBundle = new PersistableBundle();
if (Build.VERSION.SDK_INT >= 33) {
persistableBundle.putBoolean(ClipDescription.EXTRA_IS_SENSITIVE, true);
} else {
persistableBundle.putBoolean("android.content.extra.IS_SENSITIVE", true);
}
description.setExtras(persistableBundle);
// 剪切板添加加密内容
clipboardManager.setPrimaryClip(clipData);
}
不使用新API
Compared with 使用新API
hiding sensitive information, the comparison before and after the clipboard is as follows:
Seven, body sensor background permissions
Starting from Android 13, 以Android13(API 33+)为目标平台的应用
when 后台
accessing 身体传感器
(such as heart rate, body temperature and blood oxygen saturation), in addition to requesting existing BODY_SENSORS
permissions, you also need to request BODY_SENSORS_BACKGROUND
permissions.
Official reference:
Android Developer BODY_SENSORS_BACKGROUND:
Manifest.permission | Android Developers
8. Non-SDK interface restrictions
Starting from Android 9 (API level 28), the official has gradually implemented restrictions on the non-SDK interfaces used by applications.
These restrictions come into play if your app passes references 非 SDK 接口
or tries 使用反射或 JNI 来获取句柄
. The official explanation given is for 提升用户体验、降低应用崩溃风险
.
8.1. Non-SDK interface detection tool
The official gives a detection tool, download address: veridex
https://android.googlesource.com/platform/prebuilts/runtime/+archive/master/appcompat.tar.gz
How to use veridex:
appcompat.sh --dex-file=apk.apk
8.2、blacklist、greylist、greylist-max-o、greylist-max-p含义
In the above screenshot, the meanings of blacklist, greylist, greylist-max-o, and greylist-max-p are as follows:
- blacklist Blacklist: Prohibited non-SDK interfaces, crash directly at runtime (so must be resolved)
- greylist greylist: the non-SDK interface that can still be used in the current version, but may become a restricted non-SDK interface in the next version
- greylist-max-o: A non-SDK interface that can be used in targetSDK<=O, but is prohibited in targetSDK>=P
- greylist-max-p: non-SDK interfaces that can be used in targetSDK<=P, but are prohibited in targetSDK>=Q
Official reference:
Android Developer non-SDK interface restrictions:
Restrictions for non-SDK interfaces | Android Developers | Android Developers
9. Foreground service manager
Starting from Android 13 (API 33), a new item has been added to the Android notification bar 新特性
: the user can choose to stop the operation of " " through " "
in the notification bar .前台服务管理器
正在运行的前台服务
reference
Android Developer: Andoid13
Android 13 | Android Developers | Android Developers
AOSP: Android 13 Release Notes
Android 13 Release Notes | Android Open Source Project | Android Open Source Project
GoogleSource:Android13_r3
https://android.googlesource.com/platform/build/+/refs/tags/android-13.0.0_r4