שינויים בהתנהגות: אפליקציות שמטרגטות ל-Android 14 ואילך

בדומה לגרסאות קודמות, Android 14 כולל שינויים בהתנהגות שעשויים להשפיע על האפליקציה שלכם. שינויי ההתנהגות הבאים חלים אך ורק על אפליקציות שמטרגטות ל-Android 14 (רמת API 34) ואילך. אם האפליקציה שלכם מטרגטת את Android 14 ואילך, עליכם לשנות אותה כך שתתמוך בהתנהגויות האלה בצורה תקינה, במקרים הרלוונטיים.

חשוב גם לבדוק את רשימת שינויי ההתנהגות שמשפיעים על כל האפליקציות שפועלות ב-Android 14, ללא קשר ל-targetSdkVersion של האפליקציה.

פונקציונליות עיקרית

חובה לציין סוגי שירותים שפועלים בחזית

If your app targets Android 14 (API level 34) or higher, it must specify at least one foreground service type for each foreground service within your app. You should choose a foreground service type that represents your app's use case. The system expects foreground services that have a particular type to satisfy a particular use case.

If a use case in your app isn't associated with any of these types, it's strongly recommended that you migrate your logic to use WorkManager or user-initiated data transfer jobs.

אכיפה של ההרשאה BLUETOOTH_CONNECT ב-BluetoothAdapter

对于以 Android 14(API 级别 34)或更高版本为目标平台的应用,Android 14 会在调用 BluetoothAdapter getProfileConnectionState() 方法时强制执行 BLUETOOTH_CONNECT 权限。

此方法已要求 BLUETOOTH_CONNECT 权限,但未强制执行。确保您的应用在应用的 AndroidManifest.xml 文件中声明 BLUETOOTH_CONNECT,如以下代码段所示,并在调用 getProfileConnectionState 之前检查用户是否已授予相应权限

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

עדכונים ל-OpenJDK 17

Android 14 continues the work of refreshing Android's core libraries to align with the features in the latest OpenJDK LTS releases, including both library updates and Java 17 language support for app and platform developers.

A few of these changes can affect app compatibility:

  • Changes to regular expressions: Invalid group references are now disallowed to more closely follow the semantics of OpenJDK. You might see new cases where an IllegalArgumentException is thrown by the java.util.regex.Matcher class, so make sure to test your app for areas that use regular expressions. To enable or disable this change while testing, toggle the DISALLOW_INVALID_GROUP_REFERENCE flag using the compatibility framework tools.
  • UUID handling: The java.util.UUID.fromString() method now does more strict checks when validating the input argument, so you might see an IllegalArgumentException during deserialization. To enable or disable this change while testing, toggle the ENABLE_STRICT_VALIDATION flag using the compatibility framework tools.
  • ProGuard issues: In some cases, the addition of the java.lang.ClassValue class causes an issue if you try to shrink, obfuscate, and optimize your app using ProGuard. The problem originates with a Kotlin library that changes runtime behaviour based on whether Class.forName("java.lang.ClassValue") returns a class or not. If your app was developed against an older version of the runtime without the java.lang.ClassValue class available, then these optimizations might remove the computeValue method from classes derived from java.lang.ClassValue.

JobScheduler מחזק את הקריאה החוזרת (callback) ואת התנהגות הרשת

מאז ההשקה של התכונה JobScheduler, מצפה שהאפליקציה תחזור מ- onStartJob או onStopJob בתוך כמה שניות. לפני Android 14, אם משימה מסוימת נמשכת יותר מדי זמן, היא מופסקת ונכשלה באופן שקט. אם האפליקציה שלכם מטרגטת ל-Android 14 (רמת API 34) ואילך וחרגה מהזמן שהוקצה לשרשור הראשי, האפליקציה תפעיל אירוע ANR עם הודעת השגיאה 'No response to onStartJob' או 'No response to onStopJob'.

שגיאת ה-ANR הזאת יכולה לנבוע מ-2 תרחישים: 1. יש עבודה שחוסמת את ה-thread הראשי, ומונעת את ההפעלה וההשלמה של פונקציות ה-callbacks onStartJob או onStopJob במסגרת מגבלת הזמן הצפויה. 2. המפתח מפעיל עבודה חוסמת בתוך פונקציית ה-callback onStartJob או onStopJob של JobScheduler, וכתוצאה מכך פונקציית ה-callback לא מסתיימת במסגרת מגבלת הזמן הצפויה.

כדי לטפל בבעיה מס' 1, צריך לנפות באגים ולבדוק מה חוסם את ה-thread הראשי כשמתרחש ה-ANR. אפשר לעשות זאת באמצעות ApplicationExitInfo#getTraceInputStream() כדי לקבל את הטראס של tombstone כשמתרחש ה-ANR. אם מצליחים לשחזר את שגיאת ה-ANR באופן ידני, אפשר לתעד מעקב אחר המערכת ולבדוק את המעקב באמצעות Android Studio או Perfetto כדי להבין טוב יותר מה פועל ב-thread הראשי כשמתרחשת ה-ANR. חשוב לזכור שזה יכול לקרות כשמשתמשים ב-JobScheduler API ישירות או באמצעות ספריית androidx WorkManager.

כדי לטפל בבעיה השנייה, כדאי לעבור ל-WorkManager, שמספק תמיכה באריזת כל עיבוד ב-onStartJob או ב-onStopJob בשרשור אסינכרוני.

JobScheduler גם כולל דרישה להצהיר על הרשאת ACCESS_NETWORK_STATE אם משתמשים ב-setRequiredNetworkType או אילוץ של setRequiredNetwork. אם האפליקציה לא מצהירה על ההרשאה ACCESS_NETWORK_STATE כשמגדירים את התזמון של המשימה, והיא מטרגטת את Android מגרסה 14 ואילך, תופיע הודעת השגיאה SecurityException.

Tiles launch API

For apps targeting 14 and higher, TileService#startActivityAndCollapse(Intent) is deprecated and now throws an exception when called. If your app launches activities from tiles, use TileService#startActivityAndCollapse(PendingIntent) instead.

פרטיות

גישה חלקית לתמונות ולסרטונים

Android 14 引入了“已选照片访问权限”,让用户可以向应用授予对其媒体库中特定图片和视频的访问权限,而不是授予对给定类型的所有媒体的访问权限。

只有当您的应用以 Android 14(API 级别 34)或更高版本为目标平台时,此更改才会启用。如果您尚未使用照片选择器,我们建议您在应用中实现照片选择器,以提供一致的图片和视频选择体验,同时增强用户隐私保护,而无需请求任何存储权限。

如果您使用存储权限维护自己的图库选择器,并且需要对实现保持完全控制,请调整实现以使用新的 READ_MEDIA_VISUAL_USER_SELECTED 权限。如果您的应用不使用新权限,系统会以兼容模式运行您的应用。

חוויית משתמש

התראות מאובטחות של Intent במסך מלא

With Android 11 (API level 30), it was possible for any app to use Notification.Builder.setFullScreenIntent to send full-screen intents while the phone is locked. You could auto-grant this on app install by declaring USE_FULL_SCREEN_INTENT permission in the AndroidManifest.

Full-screen intent notifications are designed for extremely high-priority notifications demanding the user's immediate attention, such as an incoming phone call or alarm clock settings configured by the user. For apps targeting Android 14 (API level 34) or higher, apps that are allowed to use this permission are limited to those that provide calling and alarms only. The Google Play Store revokes default USE_FULL_SCREEN_INTENT permissions for any apps that don't fit this profile. The deadline for these policy changes is May 31, 2024.

This permission remains enabled for apps installed on the phone before the user updates to Android 14. Users can turn this permission on and off.

You can use the new API NotificationManager.canUseFullScreenIntent to check if your app has the permission; if not, your app can use the new intent ACTION_MANAGE_APP_USE_FULL_SCREEN_INTENT to launch the settings page where users can grant the permission.

אבטחה

הגבלות על כוונות מרומזות ועל כוונות בהמתנה

For apps targeting Android 14 (API level 34) or higher, Android restricts apps from sending implicit intents to internal app components in the following ways:

  • Implicit intents are only delivered to exported components. Apps must either use an explicit intent to deliver to unexported components, or mark the component as exported.
  • If an app creates a mutable pending intent with an intent that doesn't specify a component or package, the system throws an exception.

These changes prevent malicious apps from intercepting implicit intents that are intended for use by an app's internal components.

For example, here is an intent filter that could be declared in your app's manifest file:

<activity
    android:name=".AppActivity"
    android:exported="false">
    <intent-filter>
        <action android:name="com.example.action.APP_ACTION" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

If your app tried to launch this activity using an implicit intent, an ActivityNotFoundException exception would be thrown:

Kotlin

// Throws an ActivityNotFoundException exception when targeting Android 14.
context.startActivity(Intent("com.example.action.APP_ACTION"))

Java

// Throws an ActivityNotFoundException exception when targeting Android 14.
context.startActivity(new Intent("com.example.action.APP_ACTION"));

To launch the non-exported activity, your app should use an explicit intent instead:

Kotlin

// This makes the intent explicit.
val explicitIntent =
        Intent("com.example.action.APP_ACTION")
explicitIntent.apply {
    package = context.packageName
}
context.startActivity(explicitIntent)

Java

// This makes the intent explicit.
Intent explicitIntent =
        new Intent("com.example.action.APP_ACTION")
explicitIntent.setPackage(context.getPackageName());
context.startActivity(explicitIntent);

מקלטי שידורים שנרשמו בזמן ריצה חייבים לציין את התנהגות הייצוא

Apps and services that target Android 14 (API level 34) or higher and use context-registered receivers are required to specify a flag to indicate whether or not the receiver should be exported to all other apps on the device: either RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED, respectively. This requirement helps protect apps from security vulnerabilities by leveraging the features for these receivers introduced in Android 13.

Exception for receivers that receive only system broadcasts

If your app is registering a receiver only for system broadcasts through Context#registerReceiver methods, such as Context#registerReceiver(), then it shouldn't specify a flag when registering the receiver.

טעינה בטוחה יותר של קוד דינמי

אם האפליקציה שלכם מטרגטת את Android 14 (רמת API ‏34) ואילך ומשתמשת בטעינה דינמית של קוד (DCL), כל הקבצים שנטענים באופן דינמי צריכים להיות מסומנים כקובצי קריאה בלבד. אחרת, המערכת תשליך חריגה. מומלץ להימנע משימוש בטעינה דינמית של קוד כשהדבר אפשרי, כי הפעולה הזו מגדילה מאוד את הסיכון לפריצה לאפליקציה באמצעות הזרקת קוד או פגיעה בקוד.

אם אתם חייבים לטעון קוד באופן דינמי, תוכלו להשתמש בגישה הבאה כדי להגדיר את הקובץ שנטען באופן דינמי (כמו קובץ DEX, ‏ JAR או APK) כקובץ לקריאה בלבד ברגע שהוא נפתח, לפני שתוכן כלשהו נכתב בו:

Kotlin

val jar = File("DYNAMICALLY_LOADED_FILE.jar")
val os = FileOutputStream(jar)
os.use {
    // Set the file to read-only first to prevent race conditions
    jar.setReadOnly()
    // Then write the actual file content
}
val cl = PathClassLoader(jar, parentClassLoader)

Java

File jar = new File("DYNAMICALLY_LOADED_FILE.jar");
try (FileOutputStream os = new FileOutputStream(jar)) {
    // Set the file to read-only first to prevent race conditions
    jar.setReadOnly();
    // Then write the actual file content
} catch (IOException e) { ... }
PathClassLoader cl = new PathClassLoader(jar, parentClassLoader);

טיפול בקבצים שכבר קיימים ונטענים באופן דינמי

כדי למנוע השלכת חריגות לגבי קבצים קיימים שנטענו באופן דינמי, מומלץ למחוק את הקבצים וליצור אותם מחדש לפני שמנסים לטעון אותם שוב באופן דינמי באפליקציה. כשיוצרים מחדש את הקבצים, פועלים לפי ההנחיות שלמעלה לגבי סימון הקבצים לקריאה בלבד בזמן הכתיבה. לחלופין, אפשר לתייג מחדש את הקבצים הקיימים כקבצים לקריאה בלבד, אבל במקרה כזה מומלץ מאוד לוודא קודם את תקינות הקבצים (לדוגמה, על ידי בדיקת החתימה של הקובץ מול ערך מהימן), כדי להגן על האפליקציה מפני פעולות זדוניות.

הגבלות נוספות על התחלת פעילויות מהרקע

对于以 Android 14(API 级别 34)或更高版本为目标平台的应用,系统会进一步限制允许应用在后台启动 activity 的时间:

这些更改扩大了一组现有限制条件的范围,目的是防止恶意应用滥用 API 以在后台启动干扰性活动,从而保护用户。

התקפת Path Traversal בקובץ ZIP

For apps targeting Android 14 (API level 34) or higher, Android prevents the Zip Path Traversal Vulnerability in the following way: ZipFile(String) and ZipInputStream.getNextEntry() throws a ZipException if zip file entry names contain ".." or start with "/".

Apps can opt-out from this validation by calling dalvik.system.ZipPathValidator.clearCallback().

对于以 Android 14(API 级别 34)或更高版本为目标平台的应用,在以下任一情况下,MediaProjection#createVirtualDisplay 都会抛出 SecurityException

您的应用必须在每次捕获会话之前征求用户同意。单次捕获会话是对 MediaProjection#createVirtualDisplay 的单次调用,并且每个 MediaProjection 实例只能使用一次。

处理配置变更

如果您的应用需要调用 MediaProjection#createVirtualDisplay 来处理配置更改(例如屏幕方向或屏幕大小更改),您可以按照以下步骤更新现有 MediaProjection 实例的 VirtualDisplay

  1. 使用新的宽度和高度调用 VirtualDisplay#resize
  2. VirtualDisplay#setSurface 提供新的 Surface,并为其指定新的宽度和高度。

注册回调

您的应用应注册回调,以处理用户不同意继续拍摄会话的情况。为此,请实现 Callback#onStop,并让应用释放所有相关资源(例如 VirtualDisplaySurface)。

如果您的应用未注册此回调,当您的应用调用它时,MediaProjection#createVirtualDisplay 会抛出 IllegalStateException

עדכון ההגבלות על רכיבים שאינם SDK

מערכת Android 14 כוללת רשימות מעודכנות של ממשקים מוגבלים שאינם ערכות SDK, על סמך שיתוף פעולה עם מפתחי Android והבדיקות הפנימיות האחרונות. כשהדבר אפשרי, אנחנו מוודאים שיש חלופות ציבוריות זמינות לפני שאנחנו מגבילים ממשקים שאינם ב-SDK.

אם האפליקציה שלכם לא מטרגטת ל-Android 14, יכול להיות שחלק מהשינויים האלה לא ישפיעו עליכם באופן מיידי. עם זאת, אפשר להשתמש כרגע בממשקים מסוימים שאינם SDK (בהתאם לרמת ה-API לטירגוט של האפליקציה), אבל שימוש בשדה או בשיטה שאינם SDK תמיד כרוך בסיכון גבוה לשיבוש האפליקציה.

אם אתם לא בטוחים אם באפליקציה שלכם נעשה שימוש בממשקים שאינם SDK, תוכלו לבדוק את האפליקציה כדי לברר זאת. אם האפליקציה שלכם מסתמכת על ממשקים שאינם ב-SDK, כדאי להתחיל לתכנן את המעבר לחלופות ל-SDK. עם זאת, אנחנו מבינים שלאפליקציות מסוימות יש תרחישי שימוש תקפים לשימוש בממשקים שאינם SDK. אם לא מצאתם חלופה לשימוש בממשק שאינו ב-SDK לתכונה באפליקציה, עליכם לבקש ממשק API ציבורי חדש.

如需详细了解此 Android 版本中的变更,请参阅 Android 14 中有关限制非 SDK 接口的更新。如需全面了解有关非 SDK 接口的详细信息,请参阅对非 SDK 接口的限制