FindJNI: replace CMAKE_ANDROID_API by CMAKE_SYSTEM_VERSION

In commit 00c4f488f2 (FindJNI: support Android NDK, 2022-03-18,
v3.24.0-rc1~325^2) we used `CMAKE_ANDROID_API` to check the Android API
level.  However, `CMAKE_SYSTEM_VERSION` is the authoritative value.

When cross-compiling for Android, an unset `CMAKE_ANDROID_API` can
result in failure to locate JNI because the `NativeHelper` component
cannot be found. In this case, the component is falsely assumed to be
available by default (and thus required) since the comparison against an
unset `CMAKE_ANDROID_API` variable evaluates to true.  Use
`CMAKE_SYSTEM_VERSION` to determine the Android API level instead.

Issue: #23830
This commit is contained in:
Sergiu Deitsch
2022-10-17 18:45:18 +02:00
committed by Brad King
parent 06294a1945
commit c833df0924

View File

@@ -107,7 +107,7 @@ include(FindPackageHandleStandardArgs)
if(NOT JNI_FIND_COMPONENTS)
if(ANDROID)
if(CMAKE_ANDROID_API LESS 31)
if(CMAKE_SYSTEM_VERSION LESS 31)
# There are no components for Android NDK
set(JNI_FIND_COMPONENTS)
else()
@@ -125,7 +125,7 @@ else()
# On Android, if JVM was requested we need to find NativeHelper as well which
# is an implicit dependency of JVM allowing to provide uniform access to basic
# JVM/DVM functionality.
if(ANDROID AND CMAKE_ANDROID_API GREATER_EQUAL 31 AND JVM IN_LIST JNI_FIND_COMPONENTS)
if(ANDROID AND CMAKE_SYSTEM_VERSION GREATER_EQUAL 31 AND JVM IN_LIST JNI_FIND_COMPONENTS)
if(NOT NativeHelper IN_LIST JNI_FIND_COMPONENTS)
list(APPEND JNI_FIND_COMPONENTS NativeHelper)
# NativeHelper is required only if JVM was requested as such.