Android 12 (API 31) was officially released on October 4, 2021, and the official source code was pushed to the AOSP Android Open Source project on the same day. As of the time of writing this article, Android devices sold by various domestic terminal manufacturers have gradually started to update the official version of Android 12. For Android app developers, the need for Android 12 software compatibility is imminent.

There are two main types of Android 12 compatibility: one that affects all running applications by default, and the other that only affects applications declared targetSdkVersion 31.

For Android 12 adaptation points, here are summarized in the following aspects:

  • New app launch page:

SplashScreen (affects all apps);

  • The statementandroid:exported:

Android: Exported app components need to display the statement Android: Exported apps.

  • AlarmAccurate alarm clock:

The SCHEDULE_EXACT_ALARM permission must be applied for when an application uses an Alarm.

  • Notification changes:

Notification Notification layout has been changed again (for android 12-bit target platform apps);

  • Precise location:

ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions are required to request the exact location.

  • Reception Service:

Starting foreground services from the background (android 12-bit target platform apps) will be prohibited;

  • Bluetooth Permission:

When applying for bluetooth related permissions, it is no longer necessary to apply for device location information related permissions (for android 12-bit target platform applications);

Official document description: developer. The android. Google. Cn/about/versi…

First, the application startup page

(Android startup screen SplashScreen: Affects all apps)

Starting with Android 12, the system will use the new SplashScreen for cold and warm startup of the app, which by default consists of the App ICON + The windowBackground content of the app theme.

Splashscreen-related apis affect all applications running on Android 12 devices. Application developers need to adapt SplashScreen regardless of the targetSdkVersion of your application.

If SplashScreen has not been adapted, if the developer has not adapted SplashScreen, and the application is running on Android 12 or later, during cold startup or warm startup:

  • If your app originally implemented the launch page using Android :windowBackground, it will be replaced with the default launch page style.
  • If your app uses an additional Activity as the launch page, the default launch page will pop up first, followed by the launch page you implemented (the user may experience two flashes).

SplashScreen: the display elements of the new launch screen can be completely customized by the developer. Developers are advised to completely remove the launch screen of the app before it is adapted to Android12 and adapt the new launch screen to avoid duplication of the launch screen and reduce load times.

Detailed case about the SplashScreen adaptation related API code and API instructions please refer to the article: Android 12 adapter guide – SplashScreen xiaxl.blog.csdn.net/article/det…

Second, the android: exported

Android: Exported: Affects android 12 targeted application “targetSdkVersion 31”.

Starting with Andorid 12, when your application is set to target version 31 or higher (targetSdkVersion 31), If an application component (Activity, Service, Receiver, or Provider) does not display the Declare Android: Exported attribute in the manifest, the following error message will be displayed during application development or packaging:

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.

The official description of this change is shown below:

Android: Exported = true

When an Application component needs to be started or called by a component of another Application: true allows the call; False Does not allow other applications to start or be called. For example, in an Activity, it is used to indicate whether the current Activity can be started by another Application component.

Therefore, you need to display an Android :exported attribute in Android 12, as shown in the following example:

// Launcher Activity Needs to be set to true exported<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>// No launcher Activity and no external launch requirements set to fase<activity
    android:name=".MainActivity"
    android:exported="false">
</activity>
Copy the code

Alarm Precise Alarm clock

(Alarm precise Alarm: affects the application targetSdkVersion 31 for Android 12)

Starting with Andorid 12, when your application has target version 31 or later (targetSdkVersion 31), if your application needs to use precise alarm, you need to apply for a new permission (alarm and alert permission). This permission is normal and does not need to be applied dynamically:

<! --Android S alarm permission-->
<! -- Common permission: no dynamic application is required -->
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
Copy the code

The official description of this change is shown below:

The reason for adding this permission is that, in order to save system resources, Android officials want developers to adjust their apps so that they do not need to use accurate alarm clocks as much as possible, thus reducing the frequency of alarm alarm on the operating system.

Iv. Notification Notification bar

(Notification Notification: Affects app targetSdkVersion 31 for Android 12)

Starting with Andorid 12, the system again changed the layout and style of the custom notification bar.

  • Prior to Android 12, custom notification bars could use the entire notification area and customize their own layout styles; Therefore, layout compatibility issues can occur on different devices;
  • Starting with Android12, notifications customization attempts will no longer use the full notification area for applications targeting Android12.The system provides a standard templateThis template ensures that the custom notification bar looks the same in all states.

12, the Android system provides Notification. DecoratedCustomViewStyle Notification bar style, used to display and to build the state of the fold and unfold Notification bar style.

The standard template customizes the display style of the notification bar, as shown below:

Packing status of standard notification bar:Custom -collapsed-view. JPG standard notification

The relevant code can be used as follows:

// 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())
    // Set the notification layout after the collapse
    .setCustomContentView(notificationSmallLayout)
    // Set the notification layout after the exhibition
    .setCustomBigContentView(notificationLargeLayoutExpanded)
    .build()
notificationManager.notify(1, customNotification);
Copy the code

Note: The background color of notifications may vary depending on device and system version. Therefore, it is recommended to start using Style: TextAppearance_Compat_Notification for text and Style: TextAppearance_Compat_Notification_Title for titles in custom layouts. The above styles will adapt to the color changes of the system and will not have black text with a black background or white text with a white background. Examples are as follows:

<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" />
Copy the code

Official documents related to the notification bar:

Android compatibility changes in 12: mp.weixin.qq.com/s/ek2UT0vau…

12 behavior change – custom notice: Android developer. The Android, Google. Cn/about/versi…

Developer customize the notification bar: developer. The android. Google. Cn/training/no…

5. Precise location

(Exact location: affects app targetSdkVersion 31 for Android 12)

Starting with Andorid 12, when your application sets the target version to 31 or higher (targetSdkVersion 31), if the application requests the exact location of the device, Both ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions are required. After the precise location request is sent, the user side device displays the dynamic authorization request window:

If the developer requests only the ACCESS_FINE_LOCATION permission, the following error message will pop up:

ACCESS_FINE_LOCATION must be requested with ACCESS_COARSE_LOCATION.
Copy the code

Exact location related official documentation reference:

Authorization: developer position developer. The android. Google. Cn/training/lo…

Android 12 behavior changes — roughly location: developer. The Android, Google. Cn/about/versi…

6. Reception service

(Foreground service: affects app targetSdkVersion 31 for Android 12)

Starting with Andorid 12, starting foreground services from the background is prohibited and restricted when your application has target version 31 or higher (targetSdkVersion 31).

After adjustment, the foreground service can be started in the following situations:

  • Visible activities or Windows;
  • User actions, such as notifications, widgets, and so on;
  • Specific broadcasts and callbacks;
  • A STICKY service can be restarted if it crashes or stops running due to low memory.

7. Bluetooth access

(Bluetooth permission: Affects app targetSdkVersion 31 for Android 12)

Android 12 introduced BLUETOOTH_SCAN, BLUETOOTH_ADVERTISE, and BLUETOOTH_CONNECT permissions. These permissions make it easier for Android apps to interact with Bluetooth devices without having to apply for permission on device location information.

Starting with Android 12, Google officially separated Bluetooth scanning from location privileges because it found it difficult to explain the relationship between location privileges and Bluetooth to end users in terms of privacy.

reference

The Android developer: Andoid12 developer. The Android. Google. Cn/about/versi…

Android developers: 12 officially released Android mp.weixin.qq.com/s/OiFSWEnc-…

AOSP: Android open source project source. Android. Google. Cn /

The Material You: Material. IO/blog/announ…

Material Design Components: github.com/material-co…

Androidx releases the core: developer.android.com/jetpack/and…

= THE END =

This article is published on the public account “CODING technology pavilion”. If this article is helpful to you, welcome to pay attention to my public account.