Native Mobile Bits
Published in

Native Mobile Bits

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

Native Mobile Bits (Android12 migration)

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.

& …

head exploded by crashes :(
head exploding by crashes

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.

actual crash report

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.
Native Mobile Bits (Android12 migration)

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.

Android11 way

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 :

Android12 with pending intents updations

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 Android12onwards 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

handling new permissions and new code updates for alarms and bluetooth

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.

--

--

--

We share our learnings and experiences in Android & iOS in easy way. We are native development language lovers 🥰 be it KOTLIN or SWIFT. We are going to share some useful coding techniques on both Android + iOS using pure native language

Recommended from Medium

CoolDown Systems

Simplify ViewModel using Extensions

Android: How to create a custom alert dialog (using ViewModel)

An alert dialog, which shows two options, a text and a headline. On top there is a red circle with a white warning icon.

8.1 Combining Triangle Strips and Triangle Fans

images/Objects/Puck.png

Create your first unit test

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sachin Rajput

Sachin Rajput

Works on Android at Biofourmis (“Coder for profession😎 || love explaining things in a simple way”) Follow on Native Mobile Bits YouTube

More from Medium

Configuring Scheduled Notifications in Android

Why Jetpack Compose for Android Development?

Custom Notification with Work manager for Android

5 Ways of Creating Android LiveData