-
-
Save david-serrano/359b4ca3df2aeb31eca224990ba486f2 to your computer and use it in GitHub Desktop.
#!/bin/bash | |
# Script to build opencv for android for x86,x86_64, armeabi-v7a, arm64-v8a | |
# Must be run from a subdirectory that contains a directory called 'opencv' in the parent directory with the | |
# opencv sources (from git or a tarball) | |
# Final builds go to parent directory/libraries/architecture/lib_opencv_java3.so | |
export ANDROID_NDK="D:/androidNdks/android-ndk-r20" | |
export ANDROID_SDK="D:/androidSdk/" | |
export ANT_EXECUTABLE="D:/Programs/apache-ant-1.9.14/bin/ant.bat" | |
export MAKE_PROGRAM="ninja" | |
export SDK_TARGET=21 | |
export CONFIG_TYPES="Release" | |
export TOOLCHAIN_FILE="D:/androidNdks/android-ndk-r20/build/cmake/android.toolchain.cmake" | |
export BUILD_SHARED_LIBS=OFF | |
export BUILD_TOOLS_VERSION=25.0.0 | |
export ANDROID_STL="c++_static" | |
function build_opencv { | |
ABI=$1 | |
file="build_${ABI}" | |
echo "------ BUILDING 3.4.3 for $ABI -------------" | |
mkdir $file | |
pushd build_$ABI | |
cmake -G"Ninja" -DANDROID_ARM_NEON=ON -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON -DBUILD_ANDROID_PROJECTS:BOOL=OFF -DBUILD_SHARED_LIBS=$BUILD_SHARED_LIBS -DINSTALL_CREATE_DISTRIB=ON -DCMAKE_MAKE_PROGRAM=$MAKE_PROGRAM -DCMAKE_CONFIGURATION_TYPES=CONFIG_TYPES -DANDROID_SDK_TARGET=$SDK_TARGET -DANDROID_ABI=$ABI -DANT_EXECUTABLE=$ANT_EXECUTABLE -DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN_FILE -DANDROID_STL=$ANDROID_STL -DANDROID_SDK_BUILD_TOOLS_VERSION=$BUILD_TOOLS_VERSION -DANDROID_FUNCTION_LEVEL_LINKING=ON -DANDROID_FORBID_SYGWIN=ON -DANDROID_NO_EXEC_STACK=ON -DANDROID_NO_UNDEFINED=ON -DANDROID_TOOLCHAIN=clang -DWITH_CPUFEATURES=ON -DWITH_CUFFT=ON -DANDROID_GOLD_LINKER=ON -DCMAKE_C_FLAGS_RELWITHDEBINFO="-O2 -g -DNDEBUG" -DCMAKE_C_FLAGS_MINSIZEREL="-Os -DNDEBUG" -DCMAKE_CXX_FLAGS_RELWITHDEBINFO="-O2 -g -DNDEBUG" -DCMAKE_CXX_FLAGS_MINSIZEREL="-Os -DNDEBUG" -DCMAKE_CXX_FLAGS_DEBUG="-O0 -g -DDEBUG -D_DEBUG" -DCMAKE_CXX_FLAGS_RELEASE="-O3 -DNDEBUG" -DANDROID_NDK_HOST_X64=ON -DENABLE_PIC=ON -DENABLE_CCACHE=ON -DBUILD_JAVA=ON -DBUILD_opencv_java=ON -DBUILD_opencv_java_bindings_generator=ON -DBUILD_opencv_python_bindings_generator=OFF -DINSTALL_ANDROID_EXAMPLES=OFF -DANDROID_EXAMPLES_WITH_LIBS=OFF -DBUILD_EXAMPLES=OFF -DBUILD_DOCS=OFF -DWITH_OPENCL=OFF -DWITH_IPP=ON -DBUILD_ANDROID_EXAMPLES=OFF -DANDROID_RELRO=ON -DANDROID_SO_UNDEFINED=ON -DANDROID_STL_FORCE_FEATURES=ON -DBUILD_ANDROID_EXAMPLES_WITH_LIBS=OFF -DBUILD_ANDROID_PACKAGE=ON -DBUILD_DOCS=OFF -DBUILD_EXAMPLES=OFF -DBUILD_FAT_JAVA_LIB=ON -DBUILD_JASPER=OFF -DBUILD_JPEG=ON -DBUILD_OPENEXR=OFF -DBUILD_PACKAGE=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_PNG=OFF -DBUILD_WEBP=ON -DBUILD_TBB=OFF -DBUILD_TESTS=OFF -DBUILD_TIFF=OFF -DBUILD_WITH_DEBUG_INFO=OFF -DBUILD_ZLIB=OFF -DBUILD_opencv_androidcamera=OFF -DBUILD_opencv_apps=OFF -DBUILD_opencv_calib3d=OFF -DBUILD_opencv_contrib=OFF -DBUILD_opencv_videoio=OFF -DBUILD_opencv_dnn=OFF -DBUILD_opencv_core=ON -DBUILD_opencv_features2d=OFF -DBUILD_opencv_flann=OFF -DBUILD_opencv_gpu=OFF -DBUILD_opencv_highgui=OFF -DBUILD_opencv_imgproc=ON -DBUILD_opencv_imgcodecs=ON -DBUILD_opencv_legacy=OFF -DBUILD_opencv_ml=OFF -DBUILD_opencv_nonfree=OFF -DBUILD_opencv_objdetect=OFF -DBUILD_opencv_ocl=OFF -DBUILD_opencv_photo=OFF -DBUILD_opencv_stitching=OFF -DBUILD_opencv_utils=ON -DBUILD_opencv_superres=OFF -DBUILD_opencv_ts=OFF -DBUILD_opencv_video=OFF -DBUILD_opencv_videostab=OFF -DBUILD_opencv_world=OFF -DWITH_CUBLAS=OFF -DWITH_CUDA=OFF -DWITH_CUFFT=OFF -DWITH_EIGEN=OFF -DWITH_IPP=OFF -DWITH_JASPER=OFF -DWITH_JPEG=ON -DWITH_WEBP=ON -DWITH_OPENCL=OFF -DWITH_OPENEXR=OFF -DWITH_OPENMP=OFF -DWITH_PNG=OFF -DWITH_TBB=OFF -DWITH_TIFF=OFF ../../opencv | |
ninja -j5 | |
ninja install/strip | |
popd | |
} | |
function copy_library { | |
architecture=$1 | |
echo "copying ${architecture}" | |
cp -R buildall/build_$architecture/install/sdk/native/libs/$architecture/libopencv_java3.so libraries/$architecture/ | |
} | |
build_opencv x86 | |
build_opencv x86_64 | |
build_opencv armeabi-v7a | |
build_opencv arm64-v8a | |
echo "----- BUILDING COMPLETE ----" | |
cd .. | |
mkdir libraries | |
pushd libraries | |
mkdir x86 | |
mkdir x86_64 | |
mkdir armeabi-v7a | |
mkdir arm64-v8a | |
popd | |
copy_library x86 | |
copy_library x86_64 | |
copy_library armeabi-v7a | |
copy_library arm64-v8a | |
echo "----- DONE! -----" | |
############################################################################ | |
# OBSOLETE: | |
# - Mingw32 approach | |
# - OLDER NDK Example | |
# - Includes deprecated mips, mips64, armeabi | |
############################################################################ | |
#export ANDROID_NDK="D:/androidNdks/android-ndk-r15c" | |
#export ANDROID_SDK="D:/androidSdk/" | |
#export ANT_EXECUTABLE="D:/Programs/apache-ant-1.9.14/bin/ant.bat" | |
#export MAKE_PROGRAM="D:/Programs/Mingw/bin/mingw32-make.exe" | |
#export SDK_TARGET=21 | |
#export CONFIG_TYPES="Release" | |
#export TOOLCHAIN_FILE="D:/AndroidNdks/android-ndk-r15c/build/cmake/android.toolchain.cmake" | |
#export BUILD_SHARED_LIBS=ON | |
#export BUILD_FAT_JAVA_LIB=ON #-DBUILD_JAVA_FAT_LIB=$BUILD_FAT_JAVA_LIB | |
#export BUILD_TOOLS_VERSION=25.0.0 | |
#export ANDROID_STL="gnustl_static" #"gnustl_static" | |
#function build_opencv { | |
# ABI=$1 | |
# | |
# file="build_${ABI}" | |
# echo "------ BUILDING 3.4.3 for $ABI -------------" | |
# | |
# mkdir $file | |
# pushd build_$ABI | |
# | |
# | |
# cmake -G"MinGW Makefiles" -DCMAKE_MAKE_PROGRAM=$MAKE_PROGRAM -DCMAKE_CONFIGURATION_TYPES=CONFIG_TYPES -DANDROID_SDK_TARGET=$SDK_TARGET -DANDROID_ABI=$ABI -DANT_EXECUTABLE=$ANT_EXECUTABLE -DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN_FILE -DANDROID_STL=$ANDROID_STL -DANDROID_SDK_BUILD_TOOLS_VERSION=$BUILD_TOOLS_VERSION -DANDROID_FUNCTION_LEVEL_LINKING=ON -DANDROID_FORBID_SYGWIN=ON -DANDROID_NO_EXEC_STACK=ON -DANDROID_NO_UNDEFINED=ON -DANDROID_TOOLCHAIN=clang -DBUILD_SHARED_LIBS=$BUILD_SHARED_LIBS -DWITH_CPUFEATURES=ON -DWITH_CUFFT=ON -DANDROID_GOLD_LINKER=ON -DCMAKE_C_FLAGS_RELWITHDEBINFO="-O2 -g -DNDEBUG" -DCMAKE_C_FLAGS_MINSIZEREL="-Os -DNDEBUG" -DCMAKE_CXX_FLAGS_RELWITHDEBINFO="-O2 -g -DNDEBUG" -DCMAKE_CXX_FLAGS_MINSIZEREL="-Os -DNDEBUG" -DCMAKE_CXX_FLAGS_DEBUG="-O0 -g -DDEBUG -D_DEBUG" -DCMAKE_CXX_FLAGS_RELEASE="-O3 -DNDEBUG" -DANDROID_NDK_HOST_X64=ON -DENABLE_PIC=ON -DENABLE_CCACHE=ON -DBUILD_JAVA=ON -DBUILD_opencv_java=ON -DBUILD_opencv_java_bindings_generator=ON -DBUILD_opencv_python_bindings_generator=OFF -DINSTALL_ANDROID_EXAMPLES=OFF -DANDROID_EXAMPLES_WITH_LIBS=OFF -DBUILD_EXAMPLES=OFF -DBUILD_ANDROID_PROJECTS=OFF -DBUILD_DOCS=OFF -DWITH_OPENCL=OFF -DWITH_IPP=ON -DBUILD_ANDROID_EXAMPLES=OFF -DANDROID_RELRO=ON -DANDROID_SO_UNDEFINED=ON -DANDROID_STL_FORCE_FEATURES=ON -DBUILD_ANDROID_EXAMPLES_WITH_LIBS=OFF -DBUILD_ANDROID_PACKAGE=ON -DBUILD_DOCS=OFF -DBUILD_EXAMPLES=OFF -DBUILD_FAT_JAVA_LIB=ON -DBUILD_JASPER=OFF -DBUILD_JPEG=OFF -DBUILD_OPENEXR=OFF -DBUILD_PACKAGE=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_PNG=OFF -DBUILD_TBB=OFF -DBUILD_TESTS=OFF -DBUILD_TIFF=OFF -DBUILD_WITH_DEBUG_INFO=OFF -DBUILD_ZLIB=OFF -DBUILD_opencv_androidcamera=OFF -DBUILD_opencv_apps=OFF -DBUILD_opencv_calib3d=OFF -DBUILD_opencv_contrib=OFF -DBUILD_opencv_videoio=OFF -DBUILD_opencv_dnn=OFF -DBUILD_opencv_core=ON -DBUILD_opencv_features2d=OFF -DBUILD_opencv_flann=OFF -DBUILD_opencv_gpu=OFF -DBUILD_opencv_highgui=OFF -DBUILD_opencv_imgproc=ON -DBUILD_opencv_imgcodecs=ON -DBUILD_opencv_legacy=OFF -DBUILD_opencv_ml=OFF -DBUILD_opencv_nonfree=OFF -DBUILD_opencv_objdetect=OFF -DBUILD_opencv_ocl=OFF -DBUILD_opencv_photo=OFF -DBUILD_opencv_stitching=OFF -DBUILD_opencv_superres=OFF -DBUILD_opencv_ts=OFF -DBUILD_opencv_video=OFF -DBUILD_opencv_videostab=OFF -DBUILD_opencv_world=OFF -DWITH_CUBLAS=OFF -DWITH_CUDA=OFF -DWITH_CUFFT=OFF -DWITH_EIGEN=OFF -DWITH_IPP=OFF -DWITH_JASPER=OFF -DWITH_JPEG=OFF -DWITH_OPENCL=OFF -DWITH_OPENEXR=OFF -DWITH_OPENMP=OFF -DWITH_PNG=OFF -DWITH_TBB=OFF -DWITH_TIFF=OFF ../../opencv | |
# mingw32-make -j5 | |
# mingw32-make install | |
# | |
# popd | |
#} | |
#build_opencv x86 | |
#build_opencv x86_64 | |
#build_opencv armeabi-v7a | |
#build_opencv armeabi | |
#build_opencv arm64-v8a | |
#build_opencv mips | |
#build_opencv mips64 | |
# collect install directories to build/install | |
#cd .. | |
#mkdir libraries | |
#pushd libraries | |
#mkdir x86 | |
#mkdir x86_64 | |
#mkdir armeabi-v7a | |
#mkdir armeabi | |
#mkdir arm64-v8a | |
#mkdir mips | |
#mkdir mips64 | |
#popd | |
#copy_library x86 | |
#copy_library x86_64 | |
#copy_library armeabi-v7a | |
#copy_library armeabi | |
#copy_library arm64-v8a | |
#copy_library mips | |
#copy_library mips64 |
How to build OpenCV from sources for Android on Windows | |
------------------------------------------------------- | |
This is a step by step guide on how to get the source code and tools necessary for building OpenCV from scratch. | |
Downloading required packages: | |
------------------------------ | |
* Download the Android NDK: | |
ndk-r20 was used as of the time of writing. | |
* Download the Android SDK: | |
Scroll down to the section that allows you to download the SDK tools only if you don’t also want Android Studio to be installed. | |
* Download the OpenCV sources: | |
OpenCV 3.4.3 was used as of the time of writing. (https://github.com/opencv/opencv/releases/tag/3.4.3) | |
* Download Ant (https://ant.apache.org/bindownload.cgi): | |
Ant v1.9.14 was used as of the time of writing. | |
* Download Ninja (https://github.com/ninja-build/ninja/releases): | |
** Add the folder which contains ninja.exe to your PATH in Environment Variables. Eg: D:\Program\Ninja ** | |
* Download CMake (latest .msi or .zip from https://cmake.org/download/): | |
When installing, select the option to add CMake to the system PATH | |
** Check CMake is in your PATH ** | |
- If it is not, add Add the CMake/bin folder to your PATH Environment Variables. Eg: D:\Programs\CMake\bin* | |
* Download the build script (build_opencv_android.sh). | |
If you can’t run shell scripts from your command window, install Gitbash or get WSL from the Windows store. | |
Setting up the build environment: | |
--------------------------------- | |
1) Create a directory somewhere on your computer (this will be the [build directory]). | |
2) Copy the OpenCV sources into a folder called opencv. | |
3) Inside the [build directory] create a buildall folder. | |
4) Copy the build script inside the buildall folder. | |
Your folder structure should look like this: | |
[build directory] | |
|_ opencv | |
| |_ the OpenCV source files | |
| | |
|_ buildall | |
|_ build_opencv_android.sh | |
Editing the script: | |
------------------- | |
* Open the script with your editor of choice - I like Notepad++. | |
Inside you will see a few exports at the top, we are interested in the following: | |
* ANDROID_NDK | |
Edit the path in this string to point at your ndk directory. | |
* ANDROID_SDK | |
Edit the path in this string to point at your sdk directory. | |
* ANT_EXECUTABLE | |
Edit the path in this string to point at your Ant executable . | |
NB: Make sure it is pointing at the ant.bat file. | |
* TOOLCHAIN_FILE | |
Update the first part of the string path to point at your ndk directory. | |
It is worth checking the path for the cmake file is valid for your version if you’ve not downloaded android-ndk-r20. | |
Other variables that might need tweaking: | |
----------------------------------------- | |
* SDK_TARGET | |
This is the target build API. | |
* BUILD_TOOLS_VERSION | |
This is the target Build tools, may need to vary if we’re upgrading to later OpenCV releases. | |
If you don’t want to build for all supported architectures (see below), comment out the build_opencv [architecture] lines that you don’t want by prepending a #. Also comment out the mkdir [architecture] and the copy_library [architecture] to avoid confusion. | |
Executing the script: | |
--------------------- | |
* Open a command line window | |
* Navigate to the [build directory]/buildall folder. | |
* Execute ./build_opencv_android.sh | |
* Wait until the process completes (if you’re building for all architectures this might take a long while). | |
* Once the process is done you will see a log in the console saying "----- DONE! -----". | |
Obtaining the binaries: | |
----------------------- | |
* Open your [build directory]. | |
* There will be a new folder called libraries inside it. | |
* Inside this folder you will find subfolders for all the architectures that were built on the run, containing a file named libopencv_java3.so. | |
* Copy these files into the relevant architecture folders in the jniLibs directory of your Android project. | |
Troubleshooting: | |
---------------- | |
* If there are any issues building the binaries, I would recommend deleting all of the build folders, as existing remains from previous runs will cause it to break. | |
- delete all build_[architecture] folders from the buildall folder | |
- delete the top level libraries folder | |
Other notes: | |
------------ | |
The script will currently build for the following chip architectures: | |
armeabi-v7a | |
arm64-v8a | |
x86 | |
x86_64 | |
The following chip architectures are not supported by ndk-r20 | |
mips | |
mips64 | |
armeabi | |
If you want to build for these chipsets, I would recommend downloading a prior version of the android NDK (android-ndk-r15c) and updating the relevant paths as detailed in the "Editing the script" section. There is also an example in the commented out section at the bottom of the script file | |
Hello David, thank you so much.
After a big headache due to week of test finally got success with your instructions.
Grazie
@``
Thank you so much! Finally got this stuff built! OMG! I'm so happy )
is this for windows? since the file is named .sh
not .bat
and also the commands are for linux
i also see it cant be built in windows
is this for windows? since the file is named
.sh
not.bat
and also the commands are for linux i also see it cant be built in windows
I used Git bash to run this .sh file (with some modifications). You can also use Cygwin or MSYS/MinGW.
is this for windows? since the file is named
.sh
not.bat
and also the commands are for linux i also see it cant be built in windows
it was indeed built on windows, and can be run via Git Bash or any other bash terminal available on the platform
You just saved my life man! I spent whole week trying to build opencv without unneccesary libraries. No results till now! Thanks a lot!