Opencv: Building for android fails with NDK r19

Created on 6 May 2019  路  5Comments  路  Source: opencv/opencv

System information (version)
  • OpenCV == 4.1.0
  • Operating System / Platform => Windows 64 Bit
  • Compiler => NDK 19.2.5345600
Detailed description

I try to build OpenCV for android with NDK 19.2.5345600, some error occurs when cmake try to generate makefiles.

CMake Warning at platforms/android/android.toolchain.cmake:335 (message):
Could not determine machine name for compiler from
D:/app/android/SDK/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/windows-x86_64
Call Stack (most recent call first):
platforms/android/android.toolchain.cmake:562 (__DETECT_TOOLCHAIN_MACHINE_NAME)
platforms/android/android.toolchain.cmake:619 (__GLOB_NDK_TOOLCHAINS)
D:/app/android/SDK/cmake/3.10.2.4988404/share/cmake-3.10/Modules/CMakeDetermineSystem.cmake:94 (include)
CMakeLists.txt:135 (project)

CMake Warning at platforms/android/android.toolchain.cmake:335 (message):
Could not determine machine name for compiler from
D:/app/android/SDK/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64
Call Stack (most recent call first):
platforms/android/android.toolchain.cmake:562 (__DETECT_TOOLCHAIN_MACHINE_NAME)
platforms/android/android.toolchain.cmake:619 (__GLOB_NDK_TOOLCHAINS)
D:/app/android/SDK/cmake/3.10.2.4988404/share/cmake-3.10/Modules/CMakeDetermineSystem.cmake:94 (include)
CMakeLists.txt:135 (project)

CMake Warning at platforms/android/android.toolchain.cmake:335 (message):
Could not determine machine name for compiler from
D:/app/android/SDK/ndk-bundle/toolchains/x86-4.9/prebuilt/windows-x86_64
Call Stack (most recent call first):
platforms/android/android.toolchain.cmake:562 (__DETECT_TOOLCHAIN_MACHINE_NAME)
platforms/android/android.toolchain.cmake:619 (__GLOB_NDK_TOOLCHAINS)
D:/app/android/SDK/cmake/3.10.2.4988404/share/cmake-3.10/Modules/CMakeDetermineSystem.cmake:94 (include)
CMakeLists.txt:135 (project)

CMake Warning at platforms/android/android.toolchain.cmake:335 (message):
Could not determine machine name for compiler from
D:/app/android/SDK/ndk-bundle/toolchains/x86_64-4.9/prebuilt/windows-x86_64
Call Stack (most recent call first):
platforms/android/android.toolchain.cmake:562 (__DETECT_TOOLCHAIN_MACHINE_NAME)
platforms/android/android.toolchain.cmake:619 (__GLOB_NDK_TOOLCHAINS)
D:/app/android/SDK/cmake/3.10.2.4988404/share/cmake-3.10/Modules/CMakeDetermineSystem.cmake:94 (include)
CMakeLists.txt:135 (project)

CMake Error at platforms/android/android.toolchain.cmake:628 (message):
Could not find any working toolchain in the NDK. Probably your Android NDK
is broken.
Call Stack (most recent call first):
D:/app/android/SDK/cmake/3.10.2.4988404/share/cmake-3.10/Modules/CMakeDetermineSystem.cmake:94 (include)
CMakeLists.txt:135 (project)

CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
make.exe: * No rule to make target `install'. Stop.

Steps to reproduce

Use the this python file, and modify some paths in it.

import sys
import subprocess
import os

def main():
  source_dir = "D:/workspace/open_source/opencv"
  contrib_dir = "D:/workspace/open_source/opencv_contrib"
  build_dir = "D:/workspace/test/test_opencv_android"
  android_sdk_dir = "D:/app/android/SDK"
  android_abi = "armeabi-v7a"
  android_studio_dir = "D:/app/android/android_studio"

  cmake_path = android_sdk_dir + "/cmake/3.10.2.4988404/bin/cmake.exe"
  ndk_dir = android_sdk_dir + "/ndk-bundle"

  abi_build_dir = build_dir + "/" + android_abi + "-build"
  abi_install_dir = build_dir + "/" + android_abi + "-install"

  cmake_args = [cmake_path, "-G", "Unix Makefiles",
                "-DCMAKE_TOOLCHAIN_FILE=" + source_dir +
                "/platforms/android/android.toolchain.cmake",
                "-DCMAKE_MAKE_PROGRAM=" + ndk_dir +
                "/prebuilt/windows-x86_64/bin/make.exe",
                "-DOPENCV_EXTRA_MODULES_PATH=" + contrib_dir + "/modules",
                "-DANDROID_NDK=" + ndk_dir,
                "-DANDROID_ABI=" + android_abi,
                "-DANDROID_NATIVE_API_LEVEL=android-28",
                "-DANDROID_SDK_ROOT=" + android_sdk_dir,
                "-DBUILD_PERF_TESTS=0",
                "-DBUILD_TESTS=0",
                "-DBUILD_EXAMPLES=0",
                "-DCMAKE_INSTALL_PREFIX=" + abi_install_dir,
                source_dir]

  try:
    os.makedirs(abi_build_dir)
  except:
    pass

  run_env = os.environ.copy()
  run_env["JAVA_HOME"] = android_studio_dir + "/jre"
  run_env["ANDROID_HOME"] = android_sdk_dir

  subprocess.run(cmake_args, cwd = abi_build_dir, env = run_env)

  cmake_install_args = [cmake_path, "--build", abi_build_dir,
                      "--target", "install"]
  subprocess.run(cmake_install_args, cwd = abi_build_dir, env = run_env)

if __name__ == "__main__":
  main()

builinstall question (invalid tracker)

Most helpful comment

platforms/android/android.toolchain.cmake

This OpenCV's file should not be used after NDK 16b.
Prefer to use toolchain from NDK itself (available from NDK 15+) - check build_sdk.py for details.

All 5 comments

Build / usage questions should go to Users OpenCV Q/A forum: http://answers.opencv.org

platforms/android/android.toolchain.cmake

This OpenCV's file should not be used after NDK 16b.
Prefer to use toolchain from NDK itself (available from NDK 15+) - check build_sdk.py for details.

OpenCV 4.3.0: NDK 64bit for Windows 16b is not working, too.

For anyone wondering, alalek's comment solved the issue. This means set your CMAKE_TOOLCHAIN_FILE to NDK_HOME\build\cmake\android.toolchain.cmake

Use WSL is much easier to compile the source file in Windows. But you need to work on the Linux environment in Windows. And Edit the python file with VS Code + Docker.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

orzzzl picture orzzzl  路  3Comments

praneethsusarla picture praneethsusarla  路  3Comments

sturkmen72 picture sturkmen72  路  3Comments

opencv-pushbot picture opencv-pushbot  路  3Comments

koleckar picture koleckar  路  3Comments