Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 661
athos Avatar asked Dec 09 '22 22:12

athos


2 Answers

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
like image 162
q5461 Avatar answered Dec 11 '22 12:12

q5461


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
like image 43
Sergey K. Avatar answered Dec 11 '22 11:12

Sergey K.