Ask Your Question

Revision history [back]

Following mvuori's suggestion I checked the flags used in my compilation and could finally achieve after 4 days the result I wanted to, the step by step solution is as follows.

First, make sure you have the following software:

  • Java JDK 8+ is required, you can use other versions but be careful and check compatibility with gradle since you will need it later (e. g. currently gradle 5 has no compatibilty with java 13 and it is expected to ship with gradle 6).

  • Android SDK

  • Android NDK (I used ver 20.0.5594570)

You can download both of them via the Android Studio SDK Manager, in the latest versions of AS (3.5+) it seems the ndk packaging changed so just check where it says NDK (Side by side)

image description

  • Apache ant 1.10.7

  • MinGW Install and Make sure you have the following cheked:

image description

  • CMake The CMake GUI is really useful in windows so I higly recommend to use it (I used version 3.16.0-rc2)

  • Python I used version 3.8.0

Once you have all that installed remember to set up the environment variables for each one of those + your java configuration:

  • Android:

image description

  • Ant:

image description

  • Java:

image description

image description

  • Path:

image description

Get the Open CV sources you want to build, I advice you to set them up like:

image description

The build folder will be where the files for generation will be, so for now just create and empty folder.

You don't need to modify any file in the sources folder however, depending on you java version and the gradle version present in sources you might have to, for exampl, as I had Java 11 and fon OpenCV release 4.1.2 the gradle wrapper version is 4.6 you wil get an error when you try to get the folders to the install folder so just go to:

..\YOUR OPEN CV VERSION\source\platforms\android\gradle-wrapper\gradle\wrapper\gradle-wrapper.properties

And use a version compatible with your java set up, in my case I used 5.4.1 instead of 4.6

image description

Now, open the CMake GUI and begin set up:

image description

I recommend to check this, it will allow you to sort out the flags in a better way:

image description

Add the following entries:

  • ANDROID_NDK = The path to your NDK
  • ANDROID_SDK_ROOT = The path to your SDK
  • ANDROID_NATIVE_API_LEVEL = Whatever Min API you want, I used 17, but default is 21
  • ANT_EXECUTABLE = Your path towards the ant.bat file located in the ../ant/bin/ folder
  • ANDROID_STL = By default this value is c++_static however the default opencv android sdk ships with c++_shared so I use the same
  • ANDROID_ABI = By default it will build armeabi-v7a, so you only need yo change it to repeat this process for arm64-v8a, x86 and x86-64 (Also you can use armeabi-v7a with NEON to improve FP performance)

Don't worry about the red here.

image description

With all that done press configure, and select as follows:

image description

Click Next and grab the path to the Cmake toolchain (As of Android NDK 16 and up It is recommended to use the one within the NDK and not the one provided in the Open CV sources):

image description

Click Finish and wait a bit, you will get the following:

image description

Check each one of the sections and verify the values according to your own needs, for me I just needed to verify the values in:

CMAKE -> CMAKE_STRIP (It always points to MinGW Strip but this way compilation fails, use the one provided in the NDK ../ndk/20.0.5594570/toolchains/llvm/prebuilt/windows-x86_64/bin/YOUR-ARCHITECTURE-HERE-strip.exe)

WITH -> WITH_CAROTENE (Build fails randomly, don't know what it is for though...)

Once you check everything you need press configure again, all the red warning should be gone, and in the lower section you should see a summary of the tools that will be used to build open cv as:

General configuration for OpenCV 4.1.2 =====================================
  Version control:               unknown

  Platform:
    Timestamp:                   2019-10-29T22:50:08Z
    Host:                        Windows 10.0.18362 AMD64
    Target:                      Android 1 aarch64
    CMake:                       3.16.0-rc2
    CMake generator:             MinGW Makefiles
    CMake build tool:            C:/MinGW/bin/mingw32-make.exe
    Configuration:               Release

  CPU/HW features:
    Baseline:                    NEON FP16

  C/C++:
    Built as dynamic libs?:      NO
    C++ Compiler:                C:/Users/mauro/AppData/Local/Android/Sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe  (ver 8.0)
    C++ flags (Release):         -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security     -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments    -fvisibility=hidden -fvisibility-inlines-hidden -O2 -DNDEBUG   -DNDEBUG
    C++ flags (Debug):           -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security     -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments    -fvisibility=hidden -fvisibility-inlines-hidden -O0 -fno-limit-debug-info   -DDEBUG -D_DEBUG
    C Compiler:                  C:/Users/mauro/AppData/Local/Android/Sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe
    C flags (Release):           -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security    -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments    -fvisibility=hidden -fvisibility-inlines-hidden -O2 -DNDEBUG   -DNDEBUG
    C flags (Debug):             -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security    -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments    -fvisibility=hidden -fvisibility-inlines-hidden -O0 -fno-limit-debug-info   -DDEBUG -D_DEBUG
    Linker flags (Release):      -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack    
    Linker flags (Debug):        -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack    
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:          C:/Users/mauro/AppData/Local/Android/Sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/aarch64-linux-android/libz.a dl m log
    3rdparty dependencies:       libcpufeatures ittnotify libprotobuf libjpeg-turbo libwebp libpng libtiff libjasper IlmImf quirc

  OpenCV modules:
    To be built:                 calib3d core dnn features2d flann highgui imgcodecs imgproc java ml objdetect photo stitching ts video videoio
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 gapi js python2 python3
    Applications:                tests perf_tests
    Documentation:               NO
    Non-free algorithms:         NO

  Android NDK:                   C:/Users/mauro/AppData/Local/Android/Sdk/ndk/20.0.5594570 (ver 20.0.5594570)
    Android ABI:                 arm64-v8a
    NDK toolchain:               aarch64-linux-android-clang
    STL type:                    c++_shared
    Native API level:            21
  Android SDK:                   C:/Users/mauro/AppData/Local/Android/Sdk (tools: 26.1.1 build tools: 29.0.2)

  GUI: 

  Media I/O: 
    ZLib:                        C:/Users/mauro/AppData/Local/Android/Sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/aarch64-linux-android/libz.a (ver 1.2.7)
    JPEG:                        build-libjpeg-turbo (ver 2.0.2-62)
    WEBP:                        build (ver encoder: 0x020e)
    PNG:                         build (ver 1.6.37)
    TIFF:                        build (ver 42 - 4.0.10)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:

  Parallel framework:            pthreads

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Custom HAL:                  NO
    Protobuf:                    build (3.5.1)

  Python (for build):            C:/Users/mauro/AppData/Local/Programs/Python/Python38/python.exe

  Java:                          export all functions
    ant:                         C:/ant/bin/ant.bat (ver )
    Java wrappers:               YES
    Java tests:                  NO

  Install to:                    C:/Users/mauro/Downloads/opencv-4.1.2/build/install
-----------------------------------------------------------------

It is very important to have the Java Wrappers with a YES state, you shouldn't have problems if you have the environment variables right and python is installed in your system.

At the bottom of the screen you should see a "Configuring done" Now just press Generate and wait a bit until "Generating done" appears

image description

Go to the place you created the build folder and open a MinGW console, in my case I just had to right click and open my git console:

image description

Execute the command "mingw32-make.exe"

image description

Once it finishes use the same command with install "mingw32-make.exe install" and you will get an install folder with the same file layout as the one provided directly from opencv!

Following mvuori's suggestion I checked the flags used in my compilation and could finally achieve after 4 days the result I wanted to, the step by step solution is as follows.

First, make sure you have the following software:

  • Java JDK 8+ is required, you can use other versions but be careful and check compatibility with gradle since you will need it later (e. g. currently gradle 5 has no compatibilty with java 13 and it is expected to ship with gradle 6).

  • Android SDK

  • Android NDK (I used ver 20.0.5594570)

You can download both of them via the Android Studio SDK Manager, in the latest versions of AS (3.5+) it seems the ndk packaging changed so just check where it says NDK (Side by side)

image description

  • Apache ant 1.10.7

  • MinGW Install and Make sure you have the following cheked:

image description

  • CMake The CMake GUI is really useful in windows so I higly recommend to use it (I used version 3.16.0-rc2)

  • Python I used version 3.8.03.8.0 (Remember to check in the installer just before finishing to create the environment variables for you!)

Once you have all that installed remember to set up the environment variables for each one of those + your java configuration:

  • Android:

image description

  • Ant:

image description

  • Java:

image description

image description

  • Path:

image description

Get the Open CV sources you want to build, I advice advise you to set them up like:

image description

The build folder will be where the files for generation will be, so for now just create and empty folder.

You don't need to modify any file in the sources folder however, depending on you java version and the gradle version present in sources you might have to, for exampl, as I had Java 11 and fon OpenCV release 4.1.2 the gradle wrapper version is 4.6 you wil get an error when you try to get the folders to the install folder so just go to:

..\YOUR OPEN CV VERSION\source\platforms\android\gradle-wrapper\gradle\wrapper\gradle-wrapper.properties

And use a version compatible with your java set up, in my case I used 5.4.1 instead of 4.6

image description

Now, open the CMake GUI and begin set up:

image description

I recommend to check this, it will allow you to sort out the flags in a better way:

image description

Add the following entries:

  • ANDROID_NDK = The path to your NDK
  • ANDROID_SDK_ROOT = The path to your SDK
  • ANDROID_NATIVE_API_LEVEL = Whatever Min API you want, I used 17, but default is 21
  • ANT_EXECUTABLE = Your path towards the ant.bat file located in the ../ant/bin/ folder
  • ANDROID_STL = By default this value is c++_static however the default opencv android sdk ships with c++_shared so I use the same
  • ANDROID_ABI = By default it will build armeabi-v7a, so you only need yo change it to repeat this process for arm64-v8a, x86 and x86-64 (Also you can use armeabi-v7a with NEON to improve FP performance)

Don't worry about the red here.

image description

With all that done press configure, and select as follows:

image description

Click Next and grab the path to the Cmake toolchain (As of Android NDK 16 and up It is recommended to use the one within the NDK and not the one provided in the Open CV sources):

image description

Click Finish and wait a bit, you will get the following:

image description

Check each one of the sections and verify the values according to your own needs, for me I just needed to verify the values in:

CMAKE -> CMAKE_STRIP (It always points to MinGW Strip but this way compilation fails, use the one provided in the NDK ../ndk/20.0.5594570/toolchains/llvm/prebuilt/windows-x86_64/bin/YOUR-ARCHITECTURE-HERE-strip.exe)

WITH -> WITH_CAROTENE (Build fails randomly, don't know what it is for though...)

Once you check everything you need press configure again, all the red warning should be gone, and in the lower section you should see a summary of the tools that will be used to build open cv as:

General configuration for OpenCV 4.1.2 =====================================
  Version control:               unknown

  Platform:
    Timestamp:                   2019-10-29T22:50:08Z
    Host:                        Windows 10.0.18362 AMD64
    Target:                      Android 1 aarch64
    CMake:                       3.16.0-rc2
    CMake generator:             MinGW Makefiles
    CMake build tool:            C:/MinGW/bin/mingw32-make.exe
    Configuration:               Release

  CPU/HW features:
    Baseline:                    NEON FP16

  C/C++:
    Built as dynamic libs?:      NO
    C++ Compiler:                C:/Users/mauro/AppData/Local/Android/Sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe  (ver 8.0)
    C++ flags (Release):         -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security     -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments    -fvisibility=hidden -fvisibility-inlines-hidden -O2 -DNDEBUG   -DNDEBUG
    C++ flags (Debug):           -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security     -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments    -fvisibility=hidden -fvisibility-inlines-hidden -O0 -fno-limit-debug-info   -DDEBUG -D_DEBUG
    C Compiler:                  C:/Users/mauro/AppData/Local/Android/Sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe
    C flags (Release):           -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security    -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments    -fvisibility=hidden -fvisibility-inlines-hidden -O2 -DNDEBUG   -DNDEBUG
    C flags (Debug):             -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security    -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments    -fvisibility=hidden -fvisibility-inlines-hidden -O0 -fno-limit-debug-info   -DDEBUG -D_DEBUG
    Linker flags (Release):      -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack    
    Linker flags (Debug):        -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack    
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:          C:/Users/mauro/AppData/Local/Android/Sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/aarch64-linux-android/libz.a dl m log
    3rdparty dependencies:       libcpufeatures ittnotify libprotobuf libjpeg-turbo libwebp libpng libtiff libjasper IlmImf quirc

  OpenCV modules:
    To be built:                 calib3d core dnn features2d flann highgui imgcodecs imgproc java ml objdetect photo stitching ts video videoio
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 gapi js python2 python3
    Applications:                tests perf_tests
    Documentation:               NO
    Non-free algorithms:         NO

  Android NDK:                   C:/Users/mauro/AppData/Local/Android/Sdk/ndk/20.0.5594570 (ver 20.0.5594570)
    Android ABI:                 arm64-v8a
    NDK toolchain:               aarch64-linux-android-clang
    STL type:                    c++_shared
    Native API level:            21
  Android SDK:                   C:/Users/mauro/AppData/Local/Android/Sdk (tools: 26.1.1 build tools: 29.0.2)

  GUI: 

  Media I/O: 
    ZLib:                        C:/Users/mauro/AppData/Local/Android/Sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/aarch64-linux-android/libz.a (ver 1.2.7)
    JPEG:                        build-libjpeg-turbo (ver 2.0.2-62)
    WEBP:                        build (ver encoder: 0x020e)
    PNG:                         build (ver 1.6.37)
    TIFF:                        build (ver 42 - 4.0.10)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:

  Parallel framework:            pthreads

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Custom HAL:                  NO
    Protobuf:                    build (3.5.1)

  Python (for build):            C:/Users/mauro/AppData/Local/Programs/Python/Python38/python.exe

  Java:                          export all functions
    ant:                         C:/ant/bin/ant.bat (ver )
    Java wrappers:               YES
    Java tests:                  NO

  Install to:                    C:/Users/mauro/Downloads/opencv-4.1.2/build/install
-----------------------------------------------------------------

It is very important to have the Java Wrappers with a YES state, you shouldn't have problems if you have the environment variables right and python is installed in your system.

At the bottom of the screen you should see a "Configuring done" Now just press Generate and wait a bit until "Generating done" appears

image description

Go to the place you created the build folder and open a MinGW console, in my case I just had to right click and open my git console:

image description

Execute the command "mingw32-make.exe"

image description

Once it finishes use the same command with install "mingw32-make.exe install" and you will get an install folder with the same file layout as the one provided directly from opencv!