How to know which API level I'm building for u

2019-03-17 06:13发布

问题:

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?

回答1:

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


回答2:

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


回答3:

  • Android:minSdkVersion

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

  • Android:targetSdkversion

Specifies the API level on which the applicaion is designed to run.

  • Android:maxSdkVersion

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