Let's use Android 12, Migration of our Android Apps to Android12 (API 31)
This article is completely based on my recent experience dealing with Android 12, while I was trying to implement some new Android API, then in order to do so I needed to update my project to Android API 31.

Currently, My project was pointing to Android11 (API 30).
I have made a detailed youtube Video for this same where I update one of my open-source projects which was supporting Android11 (API 30).
You can check the video if you want to follow the complete migration process- link- https://youtu.be/X9IfLFLaVKI
Android12 is the latest update coming on our way and it’s already in beta as of now & “As per google docs- Android 12 Beta 4 has reached Platform Stability, a milestone that means all app-facing surfaces and behaviors are now final in Android 12.”
And by keeping these updates policies in consideration by November 2022 we will need to set your target SDK to API 31.
MIGRATION JOURNEY
Well let me take you from the same timeline When I started migrating one of my App to Android12
:)
Step1) Update SDK APIs
In this step, we need to open our app-level build.gradle
file and update the
compileSdkVersion
and targetSdkVersion
to 31.
app/build.gradle
android {
compileSdkVersion 31
defaultConfig {
minSdkVersion 23
targetSdkVersion 31
applicationId "com.example.applicationname"
versionCode 1
versionName "1.0"
resConfigs "en"
multiDexEnabled true
}
Step2) Sync and run your app.
We just need to disable offline mode in gradle
and sync our project & then try to run our app on any device or emulator.
& …

Why My App is not installing on the device, Okay it's showing some errors:
Manifest merger failed : android:exported needs to be explicitly specified for <activity>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.

Now All Android12
features and new updates are in action:)
From Android12
onwards, we need to use this tag in all of our Android
components, i.e. Activities, Services, Broadcast Receivers.
android:exported="true"
Question Where do we need to set this exported true or false?
Answer- We need to set exported as
true
if any components need to be accessible from outside of our app (either by OS or other apps).
i.e. Our App’s Launcher Activity.
<activity
android:name="com.example.sampleapp.views.activities.SplashScreenActivity"
android:theme="@style/Theme.Design.Light.NoActionBar"
android:exported="true"> <intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
</activity>
NOTE:
In the rest of the activities or other app-components we can set it to false if only our app access those, if any service is there which needs to be exposed to OS or other apps then we can set exported = true there as well.
Ok, What happens if I don't add this tag in our app components?
- APP will not run. it will keep on showing this error,
Step3) Still not able to run your app with Android 12 (API 31)?
IMPORTANT NOTE:
Here is a tricky part, if you are working on a production App and your codebase has used a few third-party libraries as well, then you will be kept on getting this error until you find what components are not exported properly.
How to detect which components still causing the crash?
- I have made a detailed youtube Video to properly scan your project and find out what components in your entire project are not properly exposed.
“In this, I update one of my projects which was supporting Android11
(API 30) and was using a few third-party libraries I spent almost a day debugging this error, and then finally I found this solution.”
- You can check the video if you want to follow the complete migration process.
Step4) PENDING INTENTS MUTABILITY
From Android12
onwards, We need to set the mutability for each use of pending intent
If we consider our app’s min support version is 23, then we can set either immutable or mutable flag to our pending intents objects.
eg.)
Let's consider a basic sample code where we use Pending Intent to use the AlarmManager service.
It will lead to a crash and will not allow the app to run.
to handle this, we need to add the IMMUTABLE OR MUTABLE flag based on our requirements like this :
If your code was not using any flag, then just add the IMMUTABLE flag and it will solve this crash.
Step5) If your app is using Alarm Manager then use this new Permission to set alarms:
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
From Android12
onwards this permission Allows applications to use exact alarm APIs. If we are setting any kind of alarm we need to add this permission inside our App manifest.
source: https://developer.android.com/about/versions/12/behavior-changes-12#exact-alarm-permission
Step6) Update WorkManager & HILT dependencies :
In my project, I was using work manager 2.6.0 & after updating it to Android12(API31) this was the one crash that i faced

and it will work fine when I updated to the recent stable version of Work Manager.
I updated the WorkManger 2.7.1 and then I didn't face this error.
A one Not very clear update is kind of use the updated HILT if you encounter any issue in previous versions of HILT.
Step7) Bluetooth permissions :
If you are using any BLE library and trying to scan and connect to any BLE devices, then to scan in Android12 we need to add new permission :
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
and dynamically request to it before performing any scan for hardware devices.
These steps are followed in this video
Full Source Code Link for the same project used here in this migration:
Thanks for staying till the end here with me, Hope I was able to share some valuable information with you!! You can also check this Migration journey where I migrate this project in the video on our Youtube channel Native Mobile Bits youtube channel
Do hit the 👏 to show some ❤️
See you next time :) till then “be you be happy + happy coding”
For more of our latest learnings on some interesting Android&iOS topics, you can connect to us on our brand new Native Mobile Bits youtube channel or LinkedIn & Github.