Migrate Apps to Android12

Huma Imam
3 min readMar 9, 2022

--

In this article I’ll explain the migration of android apps to Android 12 (API 31), this article is completely based on my personal experience while I was trying to implement Android new Splash screen API,

Before continue with the implementation let’s have a look why we have to migrate our app to Android 12,

Why Android 12

With each release of Android, there are new features as well as behaviour changes introduce aimed at making Android more helpful, more secure, and more performant. In many cases your app will work exactly as expected out-of-the-box, while in other cases you might need make to update your app to adapt to the platform changes.

In android 12 there are many behavioural changes occur few of them are given below

  • Stretch overscroll effect.
  • App splash screens
  • Web intent resolution
  • Camera preview on large screens, and many more.

have a look to Android 12 behavioural changes to read more :)

How to migrate your existing app to Android 12

Step 1: change your compileSdkVersion to 31,

change your compileSdkVersion to 31 which might be 30 if you’ve recently updated your app. for that we need to set these values in build.gradle file update sdk APIs level values

android {
compileSdkVersion 31

defaultConfig {
targetSdkVersion 31
minSdkVersion 21
...
}
}

here, compileSdkVersion defines which Android SDK version will be used by gradle to compile your app.

targetSdkVersion is a property that tells the system for which Android version the app was designed and tested on.

Step 2: Sync and run your app

After updating the build.gradle, all the new Android 12 restrictions will be apply on your app

Step 3: Export Components

Exported needs to be explicitly specified with an app targeting API 31, you need to set the android:exported tag for any activity, service, or broadcast receiver that uses intent filters and need to be accessible from outside of our app (either by OS or other apps)

<activity
...
android:exported="true”>
...
</activity>
<provider
...
android:exported="false">
...
</provider>
<receiver
...
android:exported="true">
...
</receiver>
<service
...
android:exported="false”>
...
</service>
<receiver
...
android:exported="true”
/>

if you won’t add this tag on your components app will not run it will keep on showing this error on android 12 ans above devices,

if you are still getting this error despite adding this tags on your component, make sure you’ve added this in activity if you are using any third party library in your app.

In my case I was using “com.mixpanel.android:mixpanel-android” in my app, so I had to add this tag in this receiver as well in my manifest.

<receiver android:name="com.mixpanel.android.mpmetrics.MixpanelPushNotificationDismissedReceiver"  
android:exported="true">
</receiver>

Step 4: Pending intent 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.
let’s consider an example where I was using PendingIntent in my app i.e, NotificationManager

val pendingFlag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_ONE_SHOT
else PendingIntent.FLAG_ONE_SHOT

val pendingIntent = PendingIntent.getActivity(context, uniqueId /* Request code */, intent,
pendingFlag)

val notificationBuilder = NotificationCompat.Builder(context, uniqueId.toString())
.setContentTitle(title)
.setContentIntent(pendingIntent)

val notificationManager = context.getSystemService(NOTIFICATION_SERVICE) as NotificationManager

notificationManager.notify(uniqueId, notificationBuilder.build())

Step 5: Update third party dependencies version

You will have to update few third party libraries and use stable version of library to get rid from crashes due to IllegalArgumentException, in my case I had to update below dependencies

implementation 'androidx.room:room-ktx:2.4.1'
implementation 'androidx.room:room-compiler:2.4.1'

And now we are all set for the updated features as well as behaviour changes introduced in Android 12.

Thanks for reading my article. I hope you find my article valuable. Please clap and follow for my motivation towards writing more article.

--

--