مانند نسخههای قبلی، اندروید ۱۴ شامل تغییرات رفتاری است که ممکن است بر برنامه شما تأثیر بگذارد. تغییرات رفتاری زیر منحصراً برای برنامههایی اعمال میشود که اندروید ۱۴ (سطح API ۳۴) یا بالاتر را هدف قرار میدهند. اگر برنامه شما اندروید ۱۴ یا بالاتر را هدف قرار میدهد، باید برنامه خود را اصلاح کنید تا در صورت لزوم، از این رفتارها به درستی پشتیبانی کند.
حتماً فهرست تغییرات رفتاری که بر همه برنامههای در حال اجرا در اندروید ۱۴ تأثیر میگذارند را صرف نظر از 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 مجوز BLUETOOTH_CONNECT را هنگام فراخوانی روش BluetoothAdapter getProfileConnectionState() برای برنامههایی که Android 14 (سطح API 34) یا بالاتر را هدف قرار میدهند، اعمال میکند.
این روش قبلاً به مجوز BLUETOOTH_CONNECT نیاز داشت، اما اجرا نشد. مطمئن شوید که برنامه شما BLUETOOTH_CONNECT را در فایل AndroidManifest.xml برنامه شما همانطور که در قطعه زیر نشان داده شده است، اعلام کرده و بررسی کنید که یک کاربر قبل از تماس با 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
IllegalArgumentExceptionis thrown by thejava.util.regex.Matcherclass, 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_REFERENCEflag 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 anIllegalArgumentExceptionduring deserialization. To enable or disable this change while testing, toggle theENABLE_STRICT_VALIDATIONflag using the compatibility framework tools. - ProGuard issues: In some cases, the addition of the
java.lang.ClassValueclass 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.ClassValueclass available, then these optimizations might remove thecomputeValuemethod 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) راهاندازی Tiles
برای برنامههایی که نسخه ۱۴ و بالاتر را هدف قرار میدهند، TileService#startActivityAndCollapse(Intent) منسوخ شده است و اکنون هنگام فراخوانی یک استثنا ایجاد میکند. اگر برنامه شما فعالیت ها را از کاشی ها راه اندازی می کند، به جای آن از TileService#startActivityAndCollapse(PendingIntent) استفاده کنید.
حریم خصوصی
دسترسی جزئی به عکسها و ویدیوها
Android 14 دسترسی به عکسهای انتخاب شده را معرفی میکند، که به کاربران اجازه میدهد به برنامهها اجازه دسترسی به تصاویر و ویدیوهای خاص موجود در کتابخانه خود را بدهند، نه اینکه به همه رسانههای یک نوع خاص دسترسی داشته باشند.
این تغییر تنها در صورتی فعال میشود که برنامه شما Android 14 (سطح API 34) یا بالاتر را هدف قرار دهد. اگر هنوز از انتخابگر عکس استفاده نکردهاید، توصیه میکنیم آن را در برنامه خود پیادهسازی کنید تا تجربهای ثابت برای انتخاب تصاویر و ویدیوها ارائه دهید که همچنین حریم خصوصی کاربر را بدون نیاز به درخواست مجوز ذخیرهسازی افزایش میدهد.
اگر انتخابگر گالری خود را با استفاده از مجوزهای ذخیره سازی حفظ می کنید و نیاز دارید که کنترل کامل بر پیاده سازی خود داشته باشید، پیاده سازی خود را با استفاده از مجوز جدید READ_MEDIA_VISUAL_USER_SELECTED تطبیق دهید. اگر برنامه شما از مجوز جدید استفاده نمی کند، سیستم برنامه شما را در حالت سازگاری اجرا می کند.
تجربه کاربری
اعلانهای هدفمند تمامصفحه امن
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.
امنیت
محدودیتهای مربوط به intentهای ضمنی و در حال انتظار
برای برنامههایی که Android 14 (سطح API 34) یا بالاتر را هدف قرار میدهند، Android برنامهها را از ارسال مقاصد ضمنی به اجزای داخلی برنامه به روشهای زیر محدود میکند:
- مقاصد ضمنی فقط به اجزای صادر شده تحویل داده می شوند. برنامهها یا باید از یک قصد صریح برای تحویل به اجزای صادر نشده استفاده کنند یا مؤلفه را بهعنوان صادرشده علامتگذاری کنند.
- اگر یک برنامه یک هدف معلق قابل تغییر ایجاد کند که یک جزء یا بسته را مشخص نمی کند، سیستم یک استثنا ایجاد می کند.
این تغییرات مانع از رهگیری برنامه های مخرب در مقاصد ضمنی می شود که برای استفاده توسط اجزای داخلی برنامه در نظر گرفته شده است.
به عنوان مثال، در اینجا یک فیلتر قصد وجود دارد که می تواند در فایل مانیفست برنامه شما اعلام شود:
<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>
اگر برنامه شما سعی کند این فعالیت را با استفاده از یک هدف ضمنی راه اندازی کند، یک استثنا ActivityNotFoundException ایجاد می شود:
کاتلین
// Throws an ActivityNotFoundException exception when targeting Android 14. context.startActivity(Intent("com.example.action.APP_ACTION"))
جاوا
// Throws an ActivityNotFoundException exception when targeting Android 14. context.startActivity(new Intent("com.example.action.APP_ACTION"));
برای راه اندازی فعالیت غیرصادراتی، برنامه شما باید به جای آن از یک هدف صریح استفاده کند:
کاتلین
// This makes the intent explicit. val explicitIntent = Intent("com.example.action.APP_ACTION") explicitIntent.apply { package = context.packageName } context.startActivity(explicitIntent)
جاوا
// 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
PendingIntentusingPendingIntent#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 anActivityOptionsbundle 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_STARTSflag 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 الزامی است
For apps targeting Android 14 (API level 34) or higher, a SecurityException is
thrown by MediaProjection#createVirtualDisplay in either of the following
scenarios:
- Your app caches the
Intentthat is returned fromMediaProjectionManager#createScreenCaptureIntent, and passes it multiple times toMediaProjectionManager#getMediaProjection. - Your app invokes
MediaProjection#createVirtualDisplaymultiple times on the sameMediaProjectioninstance.
Your app must ask the user to give consent before each capture session. A single
capture session is a single invocation on
MediaProjection#createVirtualDisplay, and each MediaProjection instance must
be used only once.
Handle configuration changes
If your app needs to invoke MediaProjection#createVirtualDisplay to handle
configuration changes (such as the screen orientation or screen size changing),
you can follow these steps to update the VirtualDisplay for the existing
MediaProjection instance:
- Invoke
VirtualDisplay#resizewith the new width and height. - Provide a new
Surfacewith the new width and height toVirtualDisplay#setSurface.
Register a callback
Your app should register a callback to handle cases where the user doesn't grant
consent to continue a capture session. To do this, implement
Callback#onStop and have your app release any related resources (such as
the VirtualDisplay and Surface).
If your app doesn't register this callback,
MediaProjection#createVirtualDisplay throws an IllegalStateException
when your app invokes it.
محدودیتهای غیر 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.