Skip to content

Android: Avoid searching API level directories matching architecture bitness

Sergiu Deitsch requested to merge sergiud/cmake:android-path-suffixes into master

When cross-compiling for Android, the library path suffixes /<number>/ refer to API level specific platform libraries instead of architecture bitness. Disable path suffix use under NDK to avoid incorrect inclusion of API level specific libraries below the targeted API level.

Following !7801 (merged), FindJNI now correctly identifies the NativeHelper component.

API level 30 which does not provide NativeHelper:

$ cmake -S . -B build-android-30 --debug-find-var=JAVA_NativeHelper_LIBRARY -DCMAKE_SYSTEM_NAME=Android -DCMAKE_ANDROID_API=30 -DCMAKE_ANDROID_ARCH_ABI=x86
Running with debug output on for the variable(s) JAVA_NativeHelper_LIBRARY.
-- Android: Targeting API '30' with architecture 'x86', ABI 'x86', and processor 'i686'
-- Android: Selected unified Clang toolchain
-- The C compiler identification is Clang 14.0.6
-- The CXX compiler identification is Clang 14.0.6
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Debug Log at /home/sergiu/Projects/cmake/Modules/FindJNI.cmake:498 (find_library):
  find_library called with the following settings:

    VAR: JAVA_NativeHelper_LIBRARY
    NAMES: "nativehelper"
    Documentation: Android nativehelper library
    Framework
      Only Search Frameworks: 0
      Search Frameworks Last: 0
      Search Frameworks First: 0
    AppBundle
      Only Search AppBundle: 0
      Search AppBundle Last: 0
      Search AppBundle First: 0
    CMAKE_FIND_USE_CMAKE_PATH: 1
    CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: 1
    CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: 1
    CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: 1
    CMAKE_FIND_USE_INSTALL_PREFIX: 1

  find_library considered the following locations:

    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/home/sergiu/Projects/skia/lib/i686-linux-android/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/home/sergiu/Projects/skia/lib/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/home/sergiu/Projects/skia/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/opt/intel/oneapi/compiler/2022.1.0/linux/compiler/lib/i686-linux-android/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/opt/intel/oneapi/compiler/2022.1.0/linux/compiler/lib/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/opt/intel/oneapi/compiler/2022.1.0/linux/compiler/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/home/sergiu/.local/bin/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/home/sergiu/Projects/scripts/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/home/sergiu/.gem/ruby/3.0.0/bin/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/local/bin/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/bin/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/local/sbin/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/opt/cuda/bin/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/opt/cuda/nsight_compute/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/opt/cuda/nsight_systems/bin/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/jvm/default/bin/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/bin/site_perl/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/bin/vendor_perl/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/bin/core_perl/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/i686-linux-android/30/lib/i686-linux-android/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/i686-linux-android/30/lib/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/i686-linux-android/30/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/local/lib/i686-linux-android/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/local/lib/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/local/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/i686-linux-android/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/lib/i686-linux-android/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/lib/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/home/sergiu/lib/i686-linux-android/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/home/sergiu/lib/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/home/sergiu/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/X11R6/lib/i686-linux-android/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/X11R6/lib/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/X11R6/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/pkg/lib/i686-linux-android/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/pkg/lib/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/pkg/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/opt/lib/i686-linux-android/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/opt/lib/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/opt/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/X11/(lib)nativehelper(\.so|\.a)

  The item was not found.

Call Stack (most recent call first):
  CMakeLists.txt:4 (find_package)


-- Found JNI: /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include (found version "25.1.8937393")  
-- Configuring done
-- Generating done
-- Build files have been written to: /home/sergiu/Projects/test-jni/build-android-30

API level 31 (first to provide NativeHelper):

cmake -S . -B build-android-31 --debug-find-var=JAVA_NativeHelper_LIBRARY -DCMAKE_SYSTEM_NAME=Android -DCMAKE_ANDROID_API=31 -DCMAKE_ANDROID_ARCH_ABI=x86
Running with debug output on for the variable(s) JAVA_NativeHelper_LIBRARY.
-- Android: Targeting API '31' with architecture 'x86', ABI 'x86', and processor 'i686'
-- Android: Selected unified Clang toolchain
-- The C compiler identification is Clang 14.0.6
-- The CXX compiler identification is Clang 14.0.6
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Debug Log at /home/sergiu/Projects/cmake/Modules/FindJNI.cmake:498 (find_library):
  find_library called with the following settings:

    VAR: JAVA_NativeHelper_LIBRARY
    NAMES: "nativehelper"
    Documentation: Android nativehelper library
    Framework
      Only Search Frameworks: 0
      Search Frameworks Last: 0
      Search Frameworks First: 0
    AppBundle
      Only Search AppBundle: 0
      Search AppBundle Last: 0
      Search AppBundle First: 0
    CMAKE_FIND_USE_CMAKE_PATH: 1
    CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: 1
    CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: 1
    CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: 1
    CMAKE_FIND_USE_INSTALL_PREFIX: 1

  find_library considered the following locations:

    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/home/sergiu/Projects/skia/lib/i686-linux-android/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/home/sergiu/Projects/skia/lib/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/home/sergiu/Projects/skia/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/opt/intel/oneapi/compiler/2022.1.0/linux/compiler/lib/i686-linux-android/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/opt/intel/oneapi/compiler/2022.1.0/linux/compiler/lib/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/opt/intel/oneapi/compiler/2022.1.0/linux/compiler/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/home/sergiu/.local/bin/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/home/sergiu/Projects/scripts/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/home/sergiu/.gem/ruby/3.0.0/bin/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/local/bin/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/bin/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/local/sbin/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/opt/cuda/bin/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/opt/cuda/nsight_compute/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/opt/cuda/nsight_systems/bin/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/jvm/default/bin/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/bin/site_perl/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/bin/vendor_perl/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/bin/core_perl/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/i686-linux-android/31/lib/i686-linux-android/(lib)nativehelper(\.so|\.a)
    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/i686-linux-android/31/lib/(lib)nativehelper(\.so|\.a)

  The item was found at

    /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/i686-linux-android/31/libnativehelper.so

Call Stack (most recent call first):
  CMakeLists.txt:4 (find_package)


-- Found JNI: /home/sergiu/Projects/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include (found version "25.1.8937393") found components: NativeHelper 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/sergiu/Projects/test-jni/build-android-31

Fixes: #23830 (closed)
Backport: release

Edited by Brad King

Merge request reports