How to set Android target API level for NativeScript project

smartphone, notebook, social media-1701086.jpg

Yesterday, I pumped into a problem when trying to put a new release on Goole Play Console. Turn out that Google start to require app bundle to be built with API level 30 or later to be released on Play Store. Here is my quick guide of how to set Android target API level for existed NativeScript project.

Start from August 2021, Google start to enforcing all new release needed to target API level 30 (Android 11) or above. The reason for this change is to bring significant security and performance improvements as well as enhance the user experience of Android overall. More details on this can be found at bellow link:

https://developer.android.com/distribute/best-practices/develop/target-sdk

By default, NativeScript is target API level 29 (Android 10). If you try to upload your app bundle after August 2021, Play Console will start complaining about API level:

Your app currently targets API level 29 and must target at least API level 30 to ensure it is built on the latest APIs optimized for security and performance. Change your app’s target API level to at least 30.

Google Play Console
How to set Android target API level

Your app currently targets API level 29 and must target at least API level 30 to ensure it is built on the latest APIs optimized for security and performance. Change your app’s target API level to at least 30.

Okay then, how to set Android target API level? Check the solutions bellow.

Solutions

Target SDK (API leavel) can be easily changed by set in your App_Resources/Android/app.gradle . By adding targetSdkVersion 30 to android > defaultConfig section, it will tell Gradle to build your project in desired configs. Final app.gradle content should be look like this (may different if you have customized configs):

android {
  defaultConfig {
    minSdkVersion 22
    generatedDensities = []
    targetSdkVersion 30
  }
  aaptOptions {
    additionalParameters "--no-version-vectors"
  }
}

After set above configs, build your app bundle again with

ns build android ... --aab

Your new bundle now should target API level 30 and ready to be upload to Play Store.

Side note

Gradle is a build tools for Android projects, normally we can set this target sdk in Android Studio’s interface. However, in case of NativeScript, because it have its own gradle build script so we need to set our own configs in App_Resources app.gradle, this file later will be merged together with NativeScript’s gradle script somehow. I’m not sure where exactly this configs is read or overwritten.

Maybe I will write more deep dive article about this topic later 🤔. For now, let’s get back to work, I have an app to be deployed.

Leave a Reply