When using CMake to build for Android, does CMAKE_SYSTEM_VERSION correspond to the minimum api level or the target api level?
-
Did you try logging it when building? E.g. `message("CMAKE_SYSTEM_VERSION = ${CMAKE_SYSTEM_VERSION}")` – Michael Oct 16 '18 at 17:12
1 Answers
It's the version of the operating system for which CMake is to build. CMake sets it to the Android API level of the target platform. Usually, we set CMAKE_ANDROID_API
instead of manipulating CMAKE_SYSTEM_VERSION
directly.
Unfortunately, the NDK toolchain file, which is used by the Android gradle plugin we all use with Android Studio, uses an entirely different set of variables, and sets this to 1
to "Inhibit all of CMake's own NDK handling code". It expects ANDROID_PLATFORM
instead.
At any rate, your question about minimum vs. target API level is very important. Unlike Android SDK, the NDK platform support is not backwards compatible. With Java, the best practice is to set the target API as high as possible (and also compile using the latest available SDK), and carefully use the APIs that may be unavailable on older devices.
With C++, we must work differently. Even the latest NDK r18 has 'platforms' going back to android-16
, so that you could build your C++ code to run on this older system, too. In terms of Android SDK, this corresponds to minSdkVersion
.

- 56,089
- 9
- 113
- 307
-
1Too bad the NDK toolchain file sets `CMAKE_SYSTEM_VERSION` to 1... `-DCMAKE_SYSTEM_VERSION=23` (for minSdk 23) is even passed to CMake by Android Studio, so I thought it would be available. Took me a good bit of time to find this post, so thank you! – derpda Aug 30 '22 at 10:06