How do I know which API level I'm creating to use ndk-build?

I am trying to better understand how api level selection works when using ndk-build.

I know that I can explicitly set APP_PLATFORM in Application.mk, and otherwise ndk-build will target the api specified in the manifest using android:minSdkVersion , but what if my application manifest has like android:minSdkVersion , so and android:targetSdkVersion , and is it higher than minSdkVersion?

Will ndk-build target targetSdkVersion ? And how can I check this?

In case it targets a higher level of api, I believe that I can build using the native apis available only for this level, but if I ran the application on a device with a lower level of api, it would have to fail. so in this case I have to do some api level check, is this correct?

+3
source share
3 answers

Put this code in your Android.mk immediately after defining 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 in C / C ++ code:

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

Use __ANDROID_API__ defined in $NDK/platforms/android-<level>/<abi>/usr/include/android/api-level.h

 #if __ANDROID_API__ >= 21 // building with Android NDK Native API level 21 or higher posix_fadvise64(fd, ...); #else // building with Android NDK Native API level 20 or lower syscall(__NR_arm_fadvise64_64, fd, ...); #endif 
+5
source
  • Android:minSdkVersion

The minimum version of the Android platform on which the application will run.

  • Android:targetSdkversion

Defines the API level at which the application is intended to run .

  • Android:maxSdkVersion

The maximum version of the Android platform on which the application is designed to run.

+1
source

All Articles