4

I need to use OpenCV in Android Studio 3.1.2. I have the last version at 05/20/2018 of this IDE and the NDK (17.0.4754217).

I read this post talking about it, and says something about a problem with the NDK version 16, but is not the case.

Most of the guides are for older versions of Android (I saw guides talking about Eclipse IDE for Android development) but no one with the last version or one closest, and it is a problem, since the latest version of AS causes many errors with older projects or the way in which they are made, so they make these guides obsolete.

Maybe is a problem caused by the fact that i don't kown how to work with the NDK and CMake, so i would appreciate if someoanewho knows how or had tried to do this, would tell me how to add OpenCV to Android 3.1.2, and maybe this post can ben helpful to another users for future versions of NDK and AS.

TY so much.

2 Answers2

5

1. Make sure you have Android SDK up to date, with NDK installed

2. Download latest OpenCV SDK for Android from OpenCV.org and decompress the zip file.

3. Create a new Android Studio project

  • Check Include C++ Support image 1
  • Choose empty Activity
  • In C++ Support, you can check -fexceptions and -frtti image 2

4. Import OpenCV library module

  • New -> Import Module
  • Choose the YOUR_OPENCV_SDK/sdk/java folder image 3
  • Unckeck replace jar, unckeck replace lib, unckeck create gradle-style image 4

5. Set the OpenCV library module up to fit your SDK

app/build.gradle image 5

opencv/build.gradle image 6

Edit openCVLibrary/build.gradle to match your app/build.gradle e.g:

compileSdkVersion 27
defaultConfig {
minSdkVersion 19
targetSdkVersion 27
}

6. Add OpenCV module dependency in your app module

File -> Project structure -> Module app -> Dependencies tab -> New module dependency -> choose OpenCV library module image 7

image 16

7. Make a jni folder by right clicking on app/src/main and click Change Folder Location box after that rename folder fron jni to jniLibs

  • 1st step image 8

  • 2nd step image 9

  • 3rd step image 10

8. Copy all files from your opencv directory YOUR_OPENCV_SDK/sdk/native/libs that you have downloaded and paste them in jniLibs folder

  • 1st step image 11

  • 2nd step image 12

9. Set the app build.gradle

  • Add abiFilters

    externalNativeBuild {
    cmake {
        cppFlags "-frtti -fexceptions"
        abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
       }
    }
    

    image 13

10. Configure the CMakeLists.txt file

  • Copy these three lines and paste after the cmake_minimum_required

    include_directories(YOUR_OPENCV_SDK/sdk/native/jni/include)
    add_library( lib_opencv SHARED IMPORTED )
    set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)
    

    image 14

  • Go to the end of CMakeLists.txt and write lib_opencv to the target_link_libraries list image 15

All is done now enjoy coding with opencv... Final Image

Muhammad Yasir
  • 406
  • 3
  • 15
  • Thank you very much! I have a couple of doubts about your answer. I get lost in step 7. What do you mean when you say "click change folder box" ?? Another thing I'm a bit confused about is point 10, when you say I have to add "include_directories (YOUR_OPENCV_SDK / sdk / native / jni / include)". I suppose that for that I must move the ZIP folder of OpenCV to the project folder, am I wrong? – Álvaro Redosan Jun 03 '18 at 13:55
  • Whatever, before all, i got this error when i try to sync the gradle: Could not find method abiFilters() for arguments [x86, x86_64, armeabi-v7a, arm64-v8a] on object of type com.android.build.gradle.internal.dsl.CmakeOptions. – Álvaro Redosan Jun 03 '18 at 14:22
  • By "click change folder box" I mean to say when you create a jni folder a window appears that contain a checkbox that says** "Change Folder Location"** and you have to check that box after that change folder name from jni to jniLibs. – Muhammad Yasir Jun 04 '18 at 07:33
  • And in step 10 you don't have to move your opencv to project location. Just copy paste these three lines after the line "minimum cmake required" in your cmake. – Muhammad Yasir Jun 04 '18 at 07:39
  • Follow all steps in same sequence as described now hope you will not get any error. But if there is still any error I will be glad to help you. – Muhammad Yasir Jun 04 '18 at 07:46
  • I follow same steps on android studio 3.1.2 with ndk version 17.0.4 and I had tested two opencv versions that are 3.0 and 3.2. I got success – Muhammad Yasir Jun 04 '18 at 08:00
  • This works with NDK version 16 and OpenCV SDK for Android version 3.4.3. Didn't work with later versions though. See https://stackoverflow.com/questions/52410712/ndk-problems-after-gnustl-has-been-removed-from-the-ndk-revision-r18/52436751#52436751 – Diego Victor de Jesus Oct 01 '18 at 01:18
0

Assuming you are using Android Studio, then the initial NDK integration is much easier now then it was previously.

You can simply create a new project in Android Studio, specify that you want NDK support in it and just add a basic activity.

Adding OpenCV NDK (i.e. C++ support) to this project is still tricky - as you say most of the guides and instructions on the Android OpenCV pages are eclipse based at the moment. I went through many blogs, guides etc fro OpenCv on Android Studio with NDK and the one which worked for me most recently (this week) and which seems to be being kept up to date, is the one in this answer here:

I would encourage you to try this approach and to share any issues you have in the comments so that it is updated and kept current, as changes in Android and in OpenCV mean its important to have well supported reference instructions.

Mick
  • 24,231
  • 1
  • 54
  • 120