How to Handle Version Differences in Android 12 Development? Key Tips Revealed!

This article will share my development experience with you,
focusing on the challenges and solutions brought by Android version upgrades.
By analyzing the problems I encountered,
I hope to provide some valuable insights for you when facing similar issues during the development process.

If you want to see what the official changes are,
you can directly go to Android Developer

Features Affected by Android 12
New OS Camera and Microphone Permission Switches in Android 12 and Above
  • Users can enable/disable through the quick buttons in the start bar or the Camera or Microphone Access switch in settings.
  • This may affect the user experience with the camera,
    even if the camera permission is enabled,
    as long as the user disables camera access,
    it will show a black screen for the camera.
    The same applies to the microphone: Reference

  • Currently, the following code can be used to detect if the system supports it and provide a prompt to the user. There is no API to determine if it is enabled.
Deprecated ACTION_CLOSE_SYSTEM_DIALOGS in Android 12 and Above: Reference
  • However, in Android 13, it was tested that when Accessibility Permission is enabled, ACTION_CLOSE_SYSTEM_DIALOGS can still be sent. If not enabled, the broadcast will not be received.
New Default Splash Screen in Android 12 and Above Migration guide
  • A new default icon splash screen is added as shown below:
    android12_splash_icon.gif
  • After testing, there is currently no method to remove this default splash screen.
    Only the method to remove the original custom Activity that does not display the screen but still runs the original cycle.
    The official documentation also provides some specifications for the new splash screen: Reference

  • Adjustment approach:
    • Add implementation 'androidx.core:core-splashscreen:1.0.0' to build.gradle
      Note that the minimum SDK requirement is 21 and above.
      android12_splash_min_sdk.png
    • Set the new splash screen Theme windowSplashScreenBackground is the background color, which can only be set to @color.
      windowSplashScreenAnimatedIcon is the icon for the circular view in the middle, which can be set to an image, but the size must follow the official specifications.
      windowSplashScreenBrandingImage is the small background image below the icon.
      windowSplashScreenAnimationDuration is the animation duration.
      Other properties

    • Add the new SplashTheme to AndroidManifest.xml

    • Add initial code

    SplashScreen.installSplashScreen(this) must be called before super.onCreate

    splashScreen.setKeepOnScreenCondition(() -> true );
    This line ensures that the old activity screen does not display, but the activity lifecycle will still run.
    However, this approach uses the new official splash screen, so the original customized screen will not be displayed. Product owners can consider this as needed.

On Android target SDK 12 and above, if the value of android:exported is not declared in the intent-filter, it may cause installation failure.
  • As shown, when compiling and installing, the following error INSTALL_PARSE_FAILED_MANIFEST_MALFORMED is displayed: android12_exported_crash.png
  • Additionally, when using PendingIntent, corresponding flags must be added to avoid errors:
  • Solution & Demo:
    • Add FLAG_IMMUTABLE or FLAG_MUTABLE tags at each PendingIntent creation.
    • Also, add the latest work-runtime: implementation 'androidx.work:work-runtime:2.7.1'
      android12_workmanager.png
    • Notes
      • For example, in the Airdroid project, since Airdroid & httphelper repo are separate, other places also need to be corrected.
      • Implicit PendingIntent also needs modification, as libraries used in the implementation may also use PendingIntent. For instance, the current Airdroid project uses Google Analytics version 17.0.0, but errors are still prompted. (You will only know if there is a PendingIntent error when running that specific code segment) android_12_error_01.png In this case, upgrading the version will suffice: implementation 'com.google.android.gms:play-services-analytics:18.0.1'
When upgrading the target SDK to 32, setAppCacheEnabled(flag Boolean) and setAppCachePath(path String) are removed.

Since Chromium has removed this feature, the SDK no longer supports it: Reference android12_appcache.png

When upgrading the target SDK to 32, enabling foreground services while running in the background is restricted: Reference


Currently, testing on SDK 32 shows that foreground services can still run in the background.
Initially tried running StartForegroundService in the background Service, and it worked fine. This will be monitored further.

The official recommendation is to use WorkManager as a replacement for execution: Reference

To encourage saving system resources

Alarm Manager API in Android 12 requires declaring the SCHEDULE_EXACT_ALARM permission to use

Android 12 has imposed sampling rate limits on motion sensors Reference (only available on the English official website, not in Chinese)

When using registerListener() to register a sensor listener to monitor sensor events, the maximum sampling rate is limited to 200Hz
To use a higher Hz, you must add the HIGH_SAMPLING_RATE_SENSORS permission

Android 12 behavior changes for approximate/precise location

In this version, when requesting approximate/precise location permissions,
you need to request both ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION simultaneously,
to display the new location permission request dialog
If you encounter this, take note: Reference

Android 12 has separated Bluetooth permissions

Previously, when using Bluetooth-related APIs, you needed to declare
Manifest.permission.BLUETOOTH_ADMIN and Manifest.permission.ACCESS_FINE_LOCATION
to search for nearby Bluetooth devices

If you encounter using startDiscovery.. and other APIs to search for nearby devices
Using the old permissions when targeting API 32, API returns fail or some APIs directly crash
android12_ble.png

You can modify it like this:

  1. Add android:maxSdkVersion="30" to the old permissions
  2. Declare new permissions BLUETOOTH_CONNECT, BLUETOOTH_SCAN, or BLUETOOTH_ADVERTISE as needed android12_ble.png
  3. Previously, runtime permission requests only required the ACCESS_FINE_LOCATION permission, now they are separated, and you need to add the above Bluetooth permission requests at runtime

Reference API documentation and Bluetooth permission separation explanation
Here is a simple demo on how to modify

You might also enjoy