在 Ubuntu 18.04 上为 Android NDK18rc 和 ARMEABI-7 构建 OpenCV 3.1.0 时如何更改安装目录?

How to change Install Dir when Building OpenCV 3.1.0 on Ubuntu 18.04 for Android NDK18rc and ARMEABI-7?

我花了一个多星期的时间在 Ubuntu 18.04.

上用 NDK r18 为 Android 编译 OpenCV 3.1.0

我使用了以下命令:

cmake -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON \
  -DANDROID_NDK="/home/qamaruddin/Downloads/android-ndk-r18-linux-x86_64/android-ndk-r18/" \
  -DCMAKE_TOOLCHAIN_FILE="/home/qamaruddin/Downloads/android-ndk-r18-linux-x86_64/android-ndk-r18/build/cmake/android.toolchain.cmake" \
  -DANDROID_NATIVE_API_LEVEL=19 \
  -DANDROID_ABI="armeabi-v7a" \
  -DWITH_CUDA=OFF \
  -DWITH_MATLAB=OFF \
  -DBUILD_ANDROID_EXAMPLES=OFF \
  -DBUILD_DOCS=OFF \
  -DBUILD_PERF_TESTS=OFF \
  -DBUILD_TESTS=OFF \
  -DCMAKE_CXX_COMPILER=/usr/bin/clang++ \
  -DCMAKE_C_COMPILER=/usr/bin/clang \
  -DOPENCV_EXTRA_MODULES_PATH="/opt/opencv_contrib/modules/"  \
  -DCMAKE_INSTALL_PREFIX:PATH="/home/mig-ocv/ocv-android-310/" \
  -DEXECUTABLE_OUTPUT_PATH:PATH="/home/mig-ocv/ocv-android-310/" \
  -DCMAKE_RUNTIME_OUTPUT_DIRECTORY:PATH="/home/mig-ocv/ocv-android-310/" \
  -DCMAKE_BUILD_TYPE=Release \
  /opt/opencv

接着是 make & make install/strip。

我尝试过不同版本的 OpenCV 3,例如 3.1.0、3.4.3 和 3.4.0。我也尝试过 NDKr15c、NDKr17c 和 NDKr18c。

我尝试了以下变体:

  1. 更改 NDK 版本
  2. 更改 OpenCV 3 版本
  3. 使用位于 opencv/platforms/androind/setup.py
  4. 的 python 脚本安装
  5. 使用 CMAKE 构建
  6. 将 Ninja 用于 CMAKE,但这给出了错误:"ninja: error: loading 'build/build.global.ninja': No such file or directory"
  7. 使用 CMAKE 不使用 Ninja 进行安装

目前,使用上面发布的 CMAKE 命令构建成功,但我无法更改目标安装目录,因为它总是构建到 /usr/local/ 并且当我检查目标目录时它确实如此没有熟悉的 Android 结构的 OpenCV,即:

1-等 2- java 3- 本机 --> jni --> 包含

我想知道我的过程中有什么问题导致使用 OpenCV Contrib 从源代码为 Android 构建 OpenCV 3 如此困难。

请注意,$ANDROID_HOME 设置为我系统上的 android SDKs 目录,并且 $ANDROID_NDK 也已设置。

我在 Whosebug 和其他论坛上看到了所有重复的问题,但 none 似乎适用于我的情况。

当我尝试使用此命令使用 Ninja 进行构建时:

cmake -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON \
  -DANDROID_NDK="$HOME/Downloads/android-ndk-r17b/" \
  -DCMAKE_TOOLCHAIN_FILE="$HOME/Downloads/android-ndk-r17b/build/cmake/android.toolchain.cmake" \
  -DANDROID_NATIVE_API_LEVEL=21 \
  -DANDROID_ABI="armeabi-v7a" \
  -DWITH_CUDA=OFF \
  -DWITH_MATLAB=OFF \
  -DBUILD_ANDROID_EXAMPLES=OFF \
  -DBUILD_DOCS=OFF \
  -DBUILD_PERF_TESTS=OFF \
  -DBUILD_TESTS=OFF \
  -DCMAKE_CXX_COMPILER=/usr/bin/clang++ \
  -DCMAKE_C_COMPILER=/usr/bin/clang \
  -DOPENCV_EXTRA_MODULES_PATH="$HOME/ocv/opencv_contrib/modules/"  \
  -DCMAKE_INSTALL_PREFIX:PATH="$HOME/agusta/ocv-android-310/" \
  -DEXECUTABLE_OUTPUT_PATH:PATH="$HOME/agusta/ocv-android-310/" \
  -DCMAKE_RUNTIME_OUTPUT_DIRECTORY:PATH="$HOME/agusta/ocv-android-310/" \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_MAKE_PROGRAM=/usr/bin/ninja \
  $HOME/ocv/opencv

我得到:

CMake Deprecation Warning at CMakeLists.txt:72 (cmake_policy):


The OLD behavior for policy CMP0022 will be removed from a future version
  of CMake.

  The cmake-policies(7) manual explains that the OLD behaviors of all
  policies are deprecated and that a policy should be set to OLD only under
  specific short-term circumstances.  Projects should be ported to the NEW
  behavior and not rely on setting a policy to OLD.


CMake Deprecation Warning at CMakeLists.txt:77 (cmake_policy):
  The OLD behavior for policy CMP0026 will be removed from a future version
  of CMake.

  The cmake-policies(7) manual explains that the OLD behaviors of all
  policies are deprecated and that a policy should be set to OLD only under
  specific short-term circumstances.  Projects should be ported to the NEW
  behavior and not rely on setting a policy to OLD.


CMake Deprecation Warning at CMakeLists.txt:82 (cmake_policy):
  The OLD behavior for policy CMP0042 will be removed from a future version
  of CMake.

  The cmake-policies(7) manual explains that the OLD behaviors of all
  policies are deprecated and that a policy should be set to OLD only under
  specific short-term circumstances.  Projects should be ported to the NEW
  behavior and not rely on setting a policy to OLD.


-- Check for working CXX compiler: /home/qamaruddin/Downloads/android-ndk-r17b/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++
CMake Error: Generator: execution of make failed. Make command was: "/usr/bin/ninja" "cmTC_c4cee/fast"
-- Check for working CXX compiler: /home/qamaruddin/Downloads/android-ndk-r17b/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ -- broken
CMake Error at /usr/local/share/cmake-3.12/Modules/CMakeTestCXXCompiler.cmake:45 (message):
  The C++ compiler

    "/home/qamaruddin/Downloads/android-ndk-r17b/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: /home/qamaruddin/agusta/temp/CMakeFiles/CMakeTmp

    Run Build Command:"/usr/bin/ninja" "cmTC_c4cee/fast"
    No such file or directory
    Generator: execution of make failed. Make command was: "/usr/bin/ninja" "cmTC_c4cee/fast"




  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:93 (project)


-- Configuring incomplete, errors occurred!
See also "/home/qamaruddin/agusta/temp/CMakeFiles/CMakeOutput.log".
See also "/home/qamaruddin/agusta/temp/CMakeFiles/CMakeError.log".
You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_CXX_COMPILER= /usr/bin/clang++
CMAKE_C_COMPILER= /usr/bin/clang

-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    BUILD_ANDROID_EXAMPLES
    BUILD_DOCS
    BUILD_PERF_TESTS
    BUILD_TESTS
    EXECUTABLE_OUTPUT_PATH
    OPENCV_EXTRA_MODULES_PATH
    WITH_CUDA
    WITH_MATLAB


-- Build files have been written to: /home/qamaruddin/agusta/temp

我已经设法让它工作了,基本上,OCV 不支持 NDKr18,而且我还必须从源代码安装 ninja。我还使用 python3 而不是 python2 来 运行 opencv/platforms/android/build_sdk.py 。还有一件事是我使用 Eclipse Android ADT,Google 无缘无故地弃用了 ;(,但我发现它与笨重的 Android Studio 相比超级快。

# export ANDROID_ABI=armeabi-v7a
# export ANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.8
# export ANDROID_NDK=$HOME/Downloads/android-ndk-r17c-linux-x86_64/android-ndk-r17c/
# export ANDROID_SDK=$HOME/android-sdks/

./../opencv/platforms/android/build_sdk.py --extra_modules_path=/opt/opencv_contrib/modules --config ../opencv/platforms/android/ndk-17.config.py