Ask Your Question

OpenCV C++ in Android Studio

asked 2016-09-29 15:57:05 -0600

Reeven gravatar image

updated 2016-09-30 13:31:38 -0600

Context: I'm using OpenCV for a Unity project. I already made a simple .dll plugin to wrap the C++ code I use to fit my needs on Editor / PC. Now I need a .so or .jar plugin to do the same for Android compatibility. For the life of me, I don't understand all the JNI / NDK stuff. I must not use OpenCV Java or OpenCV4Android, as these two have some bugs / missing features that I'm actually using: in particular, they're missing BOWs and the ability to load SVMs.

All current tutorials I have googled either fall back to OpenCV4Android or assume familiarity with the JNI / NDK, familiarity which I have zero of.

Question: How do I include and use C++ OpenCV in Android Studio? Explain it to me like I'm five.

EDIT: Still stuck.

get builded Libraries

Which builded libraries? Built how? Found where? Certainly not the .lib or .dll from my Windows version? Can I just dump the OpenCV headers and class files into the project's cpp folder and go from there?

edit retag flag offensive close merge delete


Could you explain, why you dont want to use JNI/NDK? For every of my C++/Android Projects it works extremely fine, and you should be able to include the opencv .so into the, sure it is a bit reading work until its sure what the does, but the Android Developer Tutorial makes it quite easy to understand.

Vintez gravatar imageVintez ( 2016-09-30 01:53:18 -0600 )edit

Oh, I want to use JNI / NDK. I know that's the part I need to understand to have working C++ in Android. But it is also the part I'm absolutely not familiar with. It's OpenCV Java and OpenCV4Android that are a no go, JNI / NDK are fine.

Reeven gravatar imageReeven ( 2016-09-30 07:16:26 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-09-30 08:07:27 -0600

Vintez gravatar image

updated 2016-09-30 08:07:47 -0600

Before trying to include c++ OpenCV to an Android Studio Project I highly recommend you to make yourself familiar with the NDK and JNI. Look up Android NDK for that. To see a Code example for that you can also look up the Samples Android Studio gives to you.

What you need to do:

Install the Android NDK, if you haven't done this already. You can find it through SDK Manager -> Android SDK -> SDK Tools. Download the Hello-JNI Sample after that. You can get it through the "Import an Android code sample" In the "Welcome to Android Studio" Window.

The Hello-JNI Project, shows you the basic usage of the NDK/JNI Combination for native development.

After you understand that, look up the Building Guide from the Android NDK page. There you will learn how to change and update the and which are necessary to add a Library or .cpp/.c/.hpp/.h files to your Application.

After that, you should be able to add OpenCV to your Android Application through: - get builded Libraries - change the that it adds the needed Libraries - add the Libraries through `static{ System.loadLibrary("librarynamespace defined in the")}" - Build your Application - Run and have fun!

Also I would suggest you, to add a .cpp File into your Android Studio Project, where you make every JNICALL. Android Studio does not (until now) Support Cpp Debugging and Syntax check but when you are familiar with c++ it wont make any problem.

So far that should be everything you need to know about that. AFAIK the OpenCV .so should be addable just like ervery other librariy through the

edit flag offensive delete link more


"get builded Libraries -"

Can you please explain how to build these yourself for android (rather than use the precompiled ones from OpenCV4Android SDK )?

If I do:

cmake -DCMAKE_TOOLCHAIN_FILE=../android.toolchain.cmake ..

I get "OpenCV-2.4.9-android-sdk/sdk/native/jni" does not appear to contain CMakeLists.txt"

Peter_p8 gravatar imagePeter_p8 ( 2016-09-30 09:16:15 -0600 )edit

Maybe something like this?

Reeven gravatar imageReeven ( 2016-09-30 10:15:24 -0600 )edit

I had another Tutorial where I builded my Libraries, but this one looks fine @Reeven try it out and say how it worked for you, I try to find my Tutorial which I used.

Vintez gravatar imageVintez ( 2016-10-04 02:21:48 -0600 )edit

@Reeven: Hi bro. If I want to write a function in native code. This function uses some function of opencv. Does it help me? Tks.

hoang anh tuan gravatar imagehoang anh tuan ( 2016-12-20 03:58:53 -0600 )edit

Question Tools

1 follower


Asked: 2016-09-29 15:57:05 -0600

Seen: 3,875 times

Last updated: Sep 30 '16