How to know which API level I'm building for using ndk-build?

android, android-manifest, android-ndk, c, c++

Solution

Put this code into your `Android.mk` just after you define TARGET_PLATFORM and LOCAL_CFLAGS

ifeq ($(TARGET_PLATFORM),android-7)
    LOCAL_CFLAGS   += -DANDROID7
else
ifeq ($(TARGET_PLATFORM),android-8)
    LOCAL_CFLAGS   += -DANDROID8
else
ifeq ($(TARGET_PLATFORM),android-9)
    LOCAL_CFLAGS   += -DANDROID9
endif
endif
endif

Now you can check this defines in your C/C++ code:

#if defined( ANDROID9 )
   // do stuff for API Level 9
#endif

Problem

I'm trying to better understand how the selection of the api level works when using ndk-build. I know I can explicitly set `APP_PLATFORM` in Application.mk, and that otherwise ndk-build will target the api indicated in the manifest with `android:minSdkVersion`, but what if my application's manifest has both `android:minSdkVersion` and `android:targetSdkVersion`, and this is higher than minSdkVersion? Will ndk-build target the targetSdkVersion? And how can I check that? In case it targets the higher api level, I guess that I will be able to build using native apis only available for that level, but if I run the application on a device with lower api level it should miserably fail, so in that case I should implement some sort of api level checking, is that correct?

Original source