Specifying Android SDK Version in Cordova

November 10th 2017 Cordova Android

It all started when I stumbled upon a bug in the current version (0.1.24) of cordova-plugin-firebase. Fortunately, there's already a fix for it in the Git repository. However, there are also other changes in the master branch, because of which the build failed with the following error:

No resource found that matches the given name: attr 'android:keyboardNavigationCluster'

Obviously, one of the newly introduced or updated dependencies now requires buildToolsVersion "26.0.1" and compileSdkVersion 26. I was still at SDK 25 and corresponding build tools on my machine because of the breaking changes in SDK 26. I finally had a good reason to update. Installing newer SDK and build tools wasn't enough, though. The build kept failing with the same error. It was time to take a closer look at the build script.

In native Android applications, the version of build tools and SDK to build them with can be specified directly in the Gradle build script:

compileSdkVersion 26
buildToolsVersion "26.0.1"

In Cordova applications, the build file is automatically generated, therefore any manual changes to it will be overwritten. The question is, where do the values in the generated file come from.

Cordova documentation lists a couple of ways how to override those values, but none of them appealed to me, because they all required either a change to the build environment or a build hook. Influencing the value, which gets automatically detected and used by Cordova seemed a more attractive option to me.

Since I couldn't find any documentation on how these values are automatically detected, I started examining the generated Gradle build file. I first noticed a very helpful cdvPrintProps task, which prints out the values used by the build script. That's a quicker alternative to waiting for the build to fail (the command needs to be invoked from the platforms/android project subfolder):

PS> gradle cdvPrintProps

Ok, the correct version of build tools was already in use. A more detailed look at the generated build script revealed that Cordova automatically uses the most recent installed version of the tools.

To find out how the SDK to use is determined, I had to look at the Cordova Android platform source code. It's hardcoded there. As evident from the linked commit, I had to update the android platform to 6.3.0:

cordova platform update android@6.3.0 --save

With this change, SDK 26 was finally used for the build and the error went away.

Get notified when a new blog post is published (usually every Friday):

If you're looking for online one-on-one mentorship on a related topic, you can find me on Codementor.
If you need a team of experienced software engineers to help you with a project, contact us at Razum.
Creative Commons License