مانند نسخه های قبلی، Android 14 شامل تغییرات رفتاری است که ممکن است بر برنامه شما تأثیر بگذارد. تغییرات رفتاری زیر منحصراً برای برنامههایی اعمال میشود که Android 14 (سطح API 34) یا بالاتر را هدف قرار میدهند. اگر برنامه شما اندروید 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 در آداپتور بلوتوث
Android 14 enforces the BLUETOOTH_CONNECT
permission when calling the
BluetoothAdapter
getProfileConnectionState()
method for apps targeting
Android 14 (API level 34) or higher.
This method already required the BLUETOOTH_CONNECT
permission, but it was not
enforced. Make sure your app declares BLUETOOTH_CONNECT
in your app's
AndroidManifest.xml
file as shown in the following snippet and check that
a user has granted the permission before calling
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 thejava.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 theDISALLOW_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 anIllegalArgumentException
during deserialization. To enable or disable this change while testing, toggle theENABLE_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 whetherClass.forName("java.lang.ClassValue")
returns a class or not. If your app was developed against an older version of the runtime without thejava.lang.ClassValue
class available, then these optimizations might remove thecomputeValue
method from classes derived fromjava.lang.ClassValue
.
JobScheduler رفتار برگشت به تماس و شبکه را تقویت می کند
از زمان معرفی، JobScheduler انتظار دارد برنامه شما در عرض چند ثانیه از onStartJob
یا onStopJob
بازگردد. قبل از اندروید 14، اگر یک کار بیش از حد طولانی شود، کار متوقف شده و بیصدا از کار میافتد. اگر برنامه شما Android 14 (سطح API 34) یا بالاتر را هدف قرار دهد و از زمان اعطا شده در رشته اصلی فراتر رود، برنامه یک ANR با پیام خطای «بدون پاسخ به onStartJob
» یا «بدون پاسخ به onStopJob
» راهاندازی میکند.
این ANR ممکن است نتیجه 2 سناریو باشد: 1. کاری وجود دارد که رشته اصلی را مسدود می کند، مانع از اجرا و تکمیل تماس های onStartJob
یا onStopJob
در محدوده زمانی مورد انتظار می شود. 2. برنامهنویس در حال اجرای کار مسدودسازی در پاسخ به تماس JobScheduler onStartJob
یا onStopJob
است و از تکمیل تماس در محدوده زمانی مورد انتظار جلوگیری میکند.
برای آدرس شماره 1، باید مواردی را که در زمان وقوع ANR، رشته اصلی را مسدود میکند، بیشتر اشکال زدایی کنید، میتوانید این کار را با استفاده از ApplicationExitInfo#getTraceInputStream()
انجام دهید تا ردیابی سنگ قبر را هنگام وقوع ANR دریافت کنید. اگر میتوانید ANR را به صورت دستی بازتولید کنید، میتوانید یک ردیابی سیستم را ضبط کنید و با استفاده از Android Studio یا Perfetto ردیابی را بررسی کنید تا بهتر بفهمید که چه چیزی در رشته اصلی در هنگام وقوع ANR اجرا میشود. توجه داشته باشید که این اتفاق میتواند هنگام استفاده مستقیم از JobScheduler API یا استفاده از کتابخانه Androidx WorkManager رخ دهد.
برای پرداختن به شماره 2، مهاجرت به WorkManager را در نظر بگیرید، که از بسته بندی هر پردازش در onStartJob
یا onStopJob
در یک رشته ناهمزمان پشتیبانی می کند.
JobScheduler
همچنین الزامی را برای اعلام مجوز ACCESS_NETWORK_STATE
در صورت استفاده از محدودیت setRequiredNetworkType
یا setRequiredNetwork
معرفی می کند. اگر برنامه شما مجوز ACCESS_NETWORK_STATE
را هنگام زمانبندی کار اعلام نکند و اندروید 14 یا بالاتر را هدف قرار دهد، منجر به یک SecurityException
میشود.
از زمان معرفی، JobScheduler انتظار دارد برنامه شما در عرض چند ثانیه از onStartJob
یا onStopJob
بازگردد. قبل از اندروید 14، اگر یک کار بیش از حد طولانی شود، کار متوقف شده و بیصدا از کار میافتد. اگر برنامه شما Android 14 (سطح API 34) یا بالاتر را هدف قرار دهد و از زمان اعطا شده در رشته اصلی فراتر رود، برنامه یک ANR با پیام خطای «بدون پاسخ به onStartJob
» یا «بدون پاسخ به onStopJob
» راهاندازی میکند.
این ANR ممکن است نتیجه 2 سناریو باشد: 1. کاری وجود دارد که رشته اصلی را مسدود می کند، مانع از اجرا و تکمیل تماس های onStartJob
یا onStopJob
در محدوده زمانی مورد انتظار می شود. 2. برنامهنویس در حال اجرای کار مسدودسازی در پاسخ به تماس JobScheduler onStartJob
یا onStopJob
است و از تکمیل تماس در محدوده زمانی مورد انتظار جلوگیری میکند.
برای آدرس شماره 1، باید مواردی را که در زمان وقوع ANR، رشته اصلی را مسدود میکند، بیشتر اشکال زدایی کنید، میتوانید این کار را با استفاده از ApplicationExitInfo#getTraceInputStream()
انجام دهید تا ردیابی سنگ قبر را هنگام وقوع ANR دریافت کنید. اگر میتوانید ANR را به صورت دستی بازتولید کنید، میتوانید یک ردیابی سیستم را ضبط کنید و با استفاده از Android Studio یا Perfetto ردیابی را بررسی کنید تا بهتر بفهمید که چه چیزی در رشته اصلی در هنگام وقوع ANR اجرا میشود. توجه داشته باشید که این اتفاق میتواند هنگام استفاده مستقیم از JobScheduler API یا استفاده از کتابخانه Androidx WorkManager رخ دهد.
برای پرداختن به شماره 2، مهاجرت به WorkManager را در نظر بگیرید، که از بسته بندی هر پردازش در onStartJob
یا onStopJob
در یک رشته ناهمزمان پشتیبانی می کند.
JobScheduler
همچنین الزامی را برای اعلام مجوز ACCESS_NETWORK_STATE
در صورت استفاده از محدودیت setRequiredNetworkType
یا setRequiredNetwork
معرفی می کند. اگر برنامه شما مجوز ACCESS_NETWORK_STATE
را هنگام زمانبندی کار اعلام نکند و اندروید 14 یا بالاتر را هدف قرار دهد، منجر به یک SecurityException
میشود.
از زمان معرفی، JobScheduler انتظار دارد برنامه شما در عرض چند ثانیه از onStartJob
یا onStopJob
بازگردد. قبل از اندروید 14، اگر یک کار بیش از حد طولانی شود، کار متوقف شده و بیصدا از کار میافتد. اگر برنامه شما Android 14 (سطح API 34) یا بالاتر را هدف قرار دهد و از زمان اعطا شده در رشته اصلی فراتر رود، برنامه یک ANR با پیام خطای «بدون پاسخ به onStartJob
» یا «بدون پاسخ به onStopJob
» راهاندازی میکند.
این ANR ممکن است نتیجه 2 سناریو باشد: 1. کاری وجود دارد که رشته اصلی را مسدود می کند، مانع از اجرا و تکمیل تماس های onStartJob
یا onStopJob
در محدوده زمانی مورد انتظار می شود. 2. برنامهنویس در حال اجرای کار مسدودسازی در پاسخ به تماس JobScheduler onStartJob
یا onStopJob
است و از تکمیل تماس در محدوده زمانی مورد انتظار جلوگیری میکند.
برای آدرس شماره 1، باید مواردی را که در زمان وقوع ANR، رشته اصلی را مسدود میکند، بیشتر اشکال زدایی کنید، میتوانید این کار را با استفاده از ApplicationExitInfo#getTraceInputStream()
انجام دهید تا ردیابی سنگ قبر را هنگام وقوع ANR دریافت کنید. اگر میتوانید ANR را به صورت دستی بازتولید کنید، میتوانید یک ردیابی سیستم را ضبط کنید و با استفاده از Android Studio یا Perfetto ردیابی را بررسی کنید تا بهتر بفهمید که چه چیزی در رشته اصلی در هنگام وقوع ANR اجرا میشود. توجه داشته باشید که این اتفاق میتواند هنگام استفاده مستقیم از JobScheduler API یا استفاده از کتابخانه Androidx WorkManager رخ دهد.
برای پرداختن به شماره 2، مهاجرت به WorkManager را در نظر بگیرید، که از بسته بندی هر پردازش در onStartJob
یا onStopJob
در یک رشته ناهمزمان پشتیبانی می کند.
JobScheduler
همچنین الزامی را برای اعلام مجوز ACCESS_NETWORK_STATE
در صورت استفاده از محدودیت setRequiredNetworkType
یا setRequiredNetwork
معرفی می کند. اگر برنامه شما مجوز ACCESS_NETWORK_STATE
را هنگام زمانبندی کار اعلام نکند و اندروید 14 یا بالاتر را هدف قرار دهد، منجر به یک SecurityException
میشود.
از زمان معرفی، JobScheduler انتظار دارد برنامه شما در عرض چند ثانیه از onStartJob
یا onStopJob
بازگردد. قبل از اندروید 14، اگر یک کار بیش از حد طولانی شود، کار متوقف شده و بیصدا از کار میافتد. اگر برنامه شما Android 14 (سطح API 34) یا بالاتر را هدف قرار دهد و از زمان اعطا شده در رشته اصلی فراتر رود، برنامه یک ANR با پیام خطای «بدون پاسخ به onStartJob
» یا «بدون پاسخ به onStopJob
» راهاندازی میکند.
این ANR ممکن است نتیجه 2 سناریو باشد: 1. کاری وجود دارد که رشته اصلی را مسدود می کند، مانع از اجرا و تکمیل تماس های onStartJob
یا onStopJob
در محدوده زمانی مورد انتظار می شود. 2. برنامهنویس در حال اجرای کار مسدودسازی در پاسخ به تماس JobScheduler onStartJob
یا onStopJob
است و از تکمیل تماس در محدوده زمانی مورد انتظار جلوگیری میکند.
برای آدرس شماره 1، باید مواردی را که در زمان وقوع ANR، رشته اصلی را مسدود میکند، بیشتر اشکال زدایی کنید، میتوانید این کار را با استفاده از ApplicationExitInfo#getTraceInputStream()
انجام دهید تا ردیابی سنگ قبر را هنگام وقوع ANR دریافت کنید. اگر میتوانید ANR را به صورت دستی بازتولید کنید، میتوانید یک ردیابی سیستم را ضبط کنید و با استفاده از Android Studio یا Perfetto ردیابی را بررسی کنید تا بهتر بفهمید که چه چیزی در رشته اصلی در هنگام وقوع ANR اجرا میشود. توجه داشته باشید که این اتفاق میتواند هنگام استفاده مستقیم از JobScheduler API یا استفاده از کتابخانه Androidx WorkManager رخ دهد.
برای پرداختن به شماره 2، مهاجرت به WorkManager را در نظر بگیرید، که از بسته بندی هر پردازش در onStartJob
یا onStopJob
در یک رشته ناهمزمان پشتیبانی می کند.
JobScheduler
همچنین الزامی را برای اعلام مجوز ACCESS_NETWORK_STATE
در صورت استفاده از محدودیت setRequiredNetworkType
یا setRequiredNetwork
معرفی می کند. اگر برنامه شما مجوز ACCESS_NETWORK_STATE
را هنگام زمانبندی کار اعلام نکند و اندروید 14 یا بالاتر را هدف قرار دهد، منجر به یک SecurityException
میشود.
کاشیها API را راهاندازی میکنند
برای برنامههایی که نسخه ۱۴ و بالاتر را هدف قرار میدهند، TileService#startActivityAndCollapse(Intent)
منسوخ شده است و اکنون هنگام فراخوانی یک استثنا ایجاد میکند. اگر برنامه شما فعالیت ها را از کاشی ها راه اندازی می کند، به جای آن از TileService#startActivityAndCollapse(PendingIntent)
استفاده کنید.
حریم خصوصی
دسترسی جزئی به عکس ها و فیلم ها
Android 14 introduces Selected Photos Access, which allows users to grant apps access to specific images and videos in their library, rather than granting access to all media of a given type.
This change is only enabled if your app targets Android 14 (API level 34) or higher. If you don't use the photo picker yet, we recommend implementing it in your app to provide a consistent experience for selecting images and videos that also enhances user privacy without having to request any storage permissions.
If you maintain your own gallery picker using storage permissions and need to
maintain full control over your implementation, adapt your implementation
to use the new READ_MEDIA_VISUAL_USER_SELECTED
permission. If your app
doesn't use the new permission, the system runs your app in a compatibility
mode.
تجربه کاربری
اعلانهای 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);
گیرنده های پخش ثبت شده در زمان اجرا باید رفتار صادراتی را مشخص کنند
برنامهها و سرویسهایی که Android 14 (سطح API 34) یا بالاتر را هدف قرار میدهند و از گیرندههای ثبت شده در زمینه استفاده میکنند، باید پرچمی را مشخص کنند تا نشان دهد گیرنده باید به همه برنامههای دیگر دستگاه صادر شود یا نه: به ترتیب RECEIVER_EXPORTED
یا RECEIVER_NOT_EXPORTED
. . این نیاز به محافظت از برنامهها در برابر آسیبپذیریهای امنیتی با استفاده از ویژگیهای این گیرندههای معرفیشده در Android 13 کمک میکند.
استثنا برای گیرنده هایی که فقط پخش سیستم را دریافت می کنند
اگر برنامه شما فقط برای پخشهای سیستمی از طریق روشهای Context#registerReceiver
، مانند Context#registerReceiver()
یک گیرنده را ثبت میکند، پس نباید هنگام ثبت گیرنده پرچمی را مشخص کند.
بارگیری کد پویا ایمن تر
If your app targets Android 14 (API level 34) or higher and uses Dynamic Code Loading (DCL), all dynamically-loaded files must be marked as read-only. Otherwise, the system throws an exception. We recommend that apps avoid dynamically loading code whenever possible, as doing so greatly increases the risk that an app can be compromised by code injection or code tampering.
If you must dynamically load code, use the following approach to set the dynamically-loaded file (such as a DEX, JAR, or APK file) as read-only as soon as the file is opened and before any content is written:
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);
Handle dynamically-loaded files that already exist
To prevent exceptions from being thrown for existing dynamically-loaded files, we recommend deleting and recreating the files before you try to dynamically load them again in your app. As you recreate the files, follow the preceding guidance for marking the files read-only at write time. Alternatively, you can re-label the existing files as read-only, but in this case, we strongly recommend that you verify the integrity of the files first (for example, by checking the file's signature against a trusted value), to help protect your app from malicious actions.
محدودیت های اضافی برای شروع فعالیت ها از پس زمینه
For apps targeting Android 14 (API level 34) or higher, the system further restricts when apps are allowed to start activities from the background:
- When an app sends a
PendingIntent
usingPendingIntent#send()
or similar methods, the app must opt in if it wants to grant its own background activity launch privileges to start the pending intent. To opt in, the app should pass anActivityOptions
bundle withsetPendingIntentBackgroundActivityStartMode(MODE_BACKGROUND_ACTIVITY_START_ALLOWED)
. - When a visible app binds a service of another app that's in the background
using the
bindService()
method, the visible app must now opt in if it wants to grant its own background activity launch privileges to the bound service. To opt in, the app should include theBIND_ALLOW_ACTIVITY_STARTS
flag when calling thebindService()
method.
These changes expand the existing set of restrictions to protect users by preventing malicious apps from abusing APIs to start disruptive activities from the background.
پیمایش مسیر زیپ
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()
.
رضایت کاربر برای هر جلسه ضبط MediaProjection لازم است
برای برنامههایی که Android 14 (سطح API 34) یا بالاتر را هدف قرار میدهند، یک SecurityException
توسط MediaProjection#createVirtualDisplay
در یکی از سناریوهای زیر ایجاد میشود:
- برنامه شما
Intent
که ازMediaProjectionManager#createScreenCaptureIntent
برگردانده میشود در حافظه پنهان ذخیره میکند و چندین بار آن را بهMediaProjectionManager#getMediaProjection
ارسال میکند. - برنامه شما چندین بار
MediaProjection#createVirtualDisplay
را در یک نمونهMediaProjection
فراخوانی می کند.
برنامه شما باید قبل از هر جلسه عکسبرداری از کاربر بخواهد رضایت بدهد. یک جلسه ضبط یک فراخوانی واحد در MediaProjection#createVirtualDisplay
است و هر نمونه MediaProjection
باید فقط یک بار استفاده شود.
کنترل تغییرات پیکربندی
اگر برنامه شما نیاز به فراخوانی MediaProjection#createVirtualDisplay
برای مدیریت تغییرات پیکربندی دارد (مانند تغییر جهت صفحه یا اندازه صفحه)، میتوانید این مراحل را برای بهروزرسانی VirtualDisplay
برای نمونه MediaProjection
موجود دنبال کنید:
-
VirtualDisplay#resize
کنید. - یک
Surface
جدید با عرض و ارتفاع جدیدVirtualDisplay#setSurface
ارائه دهید.
پاسخ تماس را ثبت کنید
برنامه شما باید برای رسیدگی به مواردی که کاربر برای ادامه جلسه ضبط رضایت نمی دهد، یک تماس پاسخ ثبت کند. برای انجام این کار، Callback#onStop
را پیاده سازی کنید و از برنامه خود بخواهید منابع مرتبط (مانند VirtualDisplay
و Surface
) را منتشر کند.
اگر برنامه شما این پاسخ تماس را ثبت نکند، MediaProjection#createVirtualDisplay
هنگامی که برنامه شما آن را فراخوانی می کند، یک IllegalStateException
ایجاد می کند.
محدودیتهای غیر SDK بهروزرسانی شد
Android 14 includes updated lists of restricted non-SDK interfaces based on collaboration with Android developers and the latest internal testing. Whenever possible, we make sure that public alternatives are available before we restrict non-SDK interfaces.
If your app does not target Android 14, some of these changes might not immediately affect you. However, while you can currently use some non-SDK interfaces (depending on your app's target API level), using any non-SDK method or field always carries a high risk of breaking your app.
If you are unsure if your app uses non-SDK interfaces, you can test your app to find out. If your app relies on non-SDK interfaces, you should begin planning a migration to SDK alternatives. Nevertheless, we understand that some apps have valid use cases for using non-SDK interfaces. If you cannot find an alternative to using a non-SDK interface for a feature in your app, you should request a new public API.
To learn more about the changes in this release of Android, see Updates to non-SDK interface restrictions in Android 14. To learn more about non-SDK interfaces generally, see Restrictions on non-SDK interfaces.