Build Ionic 2 Cordova Apps For Specific Platform Version

In this tutorial, we will take a look at how you can build your app for a specific version of Android and iOS.

Let me start off by saying that you need to have the platform version that you wish to build for installed on your system to be able to build for it. For example, if you wish to build for Android API 16 but you only have Android API 23 in you build tools then you won’t be able to build for API 16. The same goes for iOS.

By default, if you run the build command, your app will be built for the default target for the CLI version of Cordova. For each version of the Cordova CLI the default build target is different. As a result, it becomes necessary to know how you can target the platform version of your choice.

We will be editing the config.xml file which can be found at the root of the project. This file has a lot of configuration that Ionic and Cordova uses to build your app. All the code that will be shown in the tutorial has to be added inside the <widget> tag in config.xml. I will also attach a sample config.xml file for you to download at the end to the post.

For Android

Android has three values that you can set.

First, we can set the minSdkVersion

1

<preference name="android-minSdkVersion"value="19"/>

This will build an app that will be supported by an Android device which has to have Android API-19 KitKat (4.4) installed at the least. This means that devices running latter version of the Android will also be able to run the app. It is a good practice to have a minSdkVersion set.

An integer designating the minimum API Level required for the application to run. The Android system will prevent the user from installing the application if the system's API Level is lower than the value specified in this attribute. You should always declare this attribute.

Similarly, we can set maxSdkVersion as follows

1

<preference name="android-maxSdkVersion"value="16"/>

Contrary to minSdkVersion this sets the maxSdkVersion and it will work as you would expect. This preference is optional. You can read more on google docs about maxSdkVersion

Lastly, for Android we can also set targetSdkVersion as follows

1

<preference name="android-targetSdkVersion"value="16"/>

You can add all the three preference tags for one app, the order of the tags does not matter. Just make sure you are putting the right value of the API in the preference tag.

Conclusion

Targeting a specific build platform for Ionic and Cordova is just a matter of adding <preference> tag. I have seen various forum and articles that suggest to make changes to the AndroidManifest.xml file, but that simply will not work, as the AndroidManifest.xml file is a dynamically generated file. It will get overwritten every time you run the build command. One more thing to keep in mind is that make sure to run the build command after making changes to the config.xml file.

For Cordova and Ionic 2 Download Code:

Code Will Be Sent Via Email

Your Name (required)

Your Email (required)

Leave Me Your Questions And Feedback

Getting your feedback inspires me the most so hit me up in the comments here or on the comments section on my YouTube Channel or on twitter @uncutAcademy