Ask Your Question

how to build custom opencv for android (and test application using c++ client code as well as java ui)

asked 2016-09-29 03:39:27 -0500

Peter_p8 gravatar image


How can I build opencv myself on android such that I can write other native c++ application code that interacts with it?

Im using Android studio 2.2 with ndk installed. Most examples/documentation seems to be geared toward eclipse and the older .mk build system rather tham Android Studio and gradle.

More details:

I have a windows c++ application thst uses opencv. I want to port this application to android. So I need to port not only my application (c++) code but also opencv to android. My application code can be split into 2 parts: User interface part and the lower level part that interacts directly with opencv. The part of my application code that interacts with opencv I want to leave in c++, and just create a user interface in java. So I want an android (java) ui, that "talks" to my other c++ native code, which in turn "talks" to the opencv library code. (This way I can presumably port more easily later to iOS).

Is there an exmaple anywhere of this (ideally using Android Studio, and gradle)?

I have downloaded the opencv android sdk (linked from

to create an application with Android Studio solely in java that talks to opencv. I followed the advice here:

To do this I had to copy the precompiled libraries that came with the sdk (sdk/native/libs/libopencv_core.a etc) to my android project dir. One probblem I have with this is that the precompiled libraries are not available for my android test device (which requires "arm64-v8a") . So Id like to be able to compile them myself and specify that it should also build for arm64-v8a, and not just: "armeabi" "armeabi-v7a" "mips" "x86". (The opencv sdk 3.1.0 has "arm64-v8a" "armeabi" "armeabi-v7a" "mips" "mips64" "x86" "x86_64") but I need to use earlier versions of opencv too. the other problem is that it wasnt quite clear how to write other c++ code that interacts with opencv here (only java).

Id be very grateful for any help.

Thanks in Advance.

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2016-10-11 04:40:29 -0500

Peter_p8 gravatar image

updated 2016-10-14 07:17:33 -0500

Here are instructions on how to achieve this taken from here:

The detailed instructions here are apply to a development machine running Linux, but the procedure should be very similar on Windows/MacOS(X)

 Clone the main OpenCV repository from GitHub:
git clone {path_to_local_clone_of_opencv_repo}/opencv_github
Change to the opencv platforms dir
cd {path_to_local_clone_of_opencv_repo}/opencv_github/platforms
Add android’s cmake to your PATH environment variable:
export PATH={path_to_local_android_installation}/android/sdk/cmake/3.6.3155560/bin/:$PATH
Note: You may need to modify the cmake version number depending on your android installation
Point ANDROID_NDK to the install location:
export ANDROID_NDK={path_to_local_android_installation}/android/sdk/ndk-bundle/
export ANDROID_NDK={path_to_local_crystax_installation}/crystax/crystax-ndk-10.3.2
Configure the build
sh ./scripts/ .
This creates a build directory and runs a CMake command to populate the directory with a particular configuration of OpenCV.
(i) You can edit this script if you want a different configuration. eg “cmake -DBUILD_SHARED_LIBS=ON….” if you want shared libs, instead of the default dynamic libs.
(ii) Call one of the other scripts instead if you want to build for a different cpu architecture (abi) eg to build instead for x86:
Start the build
cd build_android_arm
make -j8
Note: The –j8 flag specifies that the make command will use 8 threads. You could specify a different number if you have reason for doing so.

You should now have the freshly built OpenCV libraries in {path_to_local_clone_of_opencv_repo}/opencv_github/platforms//build_android_arm/lib/armeabi-v7a/

Note: After the first build cmake caches options in the following location: {path_to_local_clone_of_opencv_repo}/opencv_github/platforms/build_android_arm/CMakeCache.txt You can peruse and edit these (and then repeat the previous 2 steps), if you want to perform any customizations of the build.

edit flag offensive delete link more


sorry to say so, but a link-only answer is a bad thing.

berak gravatar imageberak ( 2016-10-11 04:44:28 -0500 )edit

much better now @Peter_p8 Now even if the page will be closed everyone can find a Answer in your Question

Vintez gravatar imageVintez ( 2016-10-14 07:25:08 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-09-29 03:36:51 -0500

Seen: 2,708 times

Last updated: Oct 14 '16