Hey Fellow Android Devs, as Google announced in their I/O the upcoming Android 12 release with all its new bells and whistles. For developers, they announced some changes which requires us to make changes to our existing applications to make them compatible with the upcoming Android 12 releases.
So my journey began, by changing the compileSdkVersion and targetSdkVersion to Android 12 (API level 31). Initially, Google required to change it to S but later on they released a stable sdk version.
After making the changes and compiling it, the first breaking change error came:
- Safer Component Exporting
Safer Component Exporting requires all the components in the Android Manifest which have <intent-filter> tag also specify android:exported attribute. This allows the OS to know the component is internal to the application or needs to be external.
Note: The first activity that your application declares as launcher should android:exported flag specified as true, otherwise the OS won’t be able to launch your application.
After making this change and running my application, I encountered a few crashes due to the next change which is:
2. Pending Intents Mutability
This required us to specific a mutability flag to all the pending intents in our application as PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_MUTABLE. These indicate whether the Pending Intent is mutable or not.
So I searched the codebase for my application and specified the flag for this as required. In most cases it was PendingIntent.FLAG_IMMUTABLE.
After this change, the crashing stopped. However I noticed that the Notifications used in my application which were completely custom layout based seemed broken UI wise. Hence the 3rd major change appeared.
3. Custom Notification Layout Changes
Android 12 has changed the implementation of custom notifications. It now take away full control of the notification layout from the developer and provides a partial control of the notification area.
As indicated by the images above, the area in checkered highlights the area which the developer can access and customize according to their own needs. So I had to engage with a Graphic Designer, redesign the notifications and then apply the changes across the application.
Now came the part to release the build for testing by targeting Android 12 and make sure to test it on an Android 12 compatible device. For this, I triggered the build generation script and it again showed the errors highlighted before the changes were made for Safer Component Exporting.
I double-checked the Android Manifest file and ensure that all the components with intent filters had the exported flag. After that, I decided to hop on the Google Search train to see if people had faced similar issues. A few posts on StackOverflow indicated that I check the merged manifest of my application.
Upon doing that, I found that all the components in the merged manifest with intent filters also had the exported flag. After that I was in a labyrinth, trying to figure out what could be causing this issue.
After several hours of thinking, an idea popped into my mind. I went to the build.gradle file of the main application module. I started commenting each library imported in the project and tested whether the build generation worked or not. The build.gradle file had around 30–40 implementation of external libraries and some jars. I brute-forced my way through them and found out that the junit version 1.1.2 was the culprit here. I upgraded it to the newer version junit version 1.1.3 and then released the application for initial testing.
I really hope this article helps you migrate your Android applications to Android 12. Please let me know if you need any help in the comments and I will be sure to help you out.