1

I installed Android Studio 3.1.4 and downloaded OpenCV-2.4.11-android-sdk. For first steps, I tried to run a sample that comes with the OpenCV SDK, e.g. face-detection. When I build the project in Android Studio I get the message:

Error: Your project contains C++ files but it is not using a supported native build system. Consider using CMake or ndk-build integration. For more information, go to: https://d.android.com/r/studio-ui/add-native-code.html

On this site its said NDK, CMake and LLDB must be installed. But it already is.

Any idea how to fix that?

Steffen
  • 89
  • 1
  • 1
  • 9
  • Its worth noting that OpenCV is not supported in any of the examples or documentation on Android studio - it is still clips based. Getting OpenCV to work in Studio with NDK is an ever changing problem - there are discussions here are elsewhere on this that are worth reading, e.g. https://stackoverflow.com/q/40948953/334402 , but I have never found an 'easy' answer. – Mick Sep 10 '18 at 15:17

3 Answers3

1

Since you have the C++ code, and the Makefiles in the project directory, in which case, you simply have to link Gradle to the native library:

  1. In your project pane, right-click on your module, and select Link C++ Project with Gradle.

  2. From the drop-down select either CMake or ndk-build, depending on your project

    a. If you selected CMake, specify the CMakeLists.txt script in your project

    b. If you selected ndk-build, specify the Android.mk.

PS: Android.mk is located in; app/build/intermediates/ndk/debug.

Anubhav Gupta
  • 2,492
  • 14
  • 27
  • Thank you. But how can I know if I need CMake or ndk-build? Its a sample project out of the opencv sdk. And where is the CMakeLists.txt is located? – Steffen Sep 08 '18 at 10:23
  • use ndk-build. You can refer here https://stackoverflow.com/questions/39589427/difference-between-cmake-and-ndk-build-in-android-studio-project – Anubhav Gupta Sep 08 '18 at 10:35
1

In my case, I was missing the location of the ndk in my local.properties file. Please add ndk path with android sdk in the local.properties

ndk.dir=/Path to the Sdk/Android/sdk/ndk-bundle
sdk.dir=/Path to the Ndk directory/Android/sdk
Naveed Ahmad
  • 6,627
  • 2
  • 58
  • 83
0

It does not work. Now I created a new project. Then:

  1. I clicked File->New->Import Module and added the java folder from my OpenCV-android-sdk.

  2. File->Project Structure->app->Dependencies-> + module Dependency. I added openCVLibrary2411

  3. Right click on app: New->folder->JNI Folder->change Folder location: I changed into "src/main/jniLibs/

  4. I copied the contend of "OpenCV-android-sdk\sdk\native\libs" (4 folders: armeabi, armeabi-v7a, mips, x86) into "src/main/jniLibs/"

  5. Right click on Packages: openCVLibrary2411->Link C++ Project with Gradle: Build System: ndk-build, Projekt Path: "[MyFolder]\OpencvTest7\app\build\intermediates\ndk\debug"

  6. In MainActivity.java I added after

setContentView(R.layout.activity_main);

if(OpenCVLoader.initDebug()){

        Toast.makeText(getApplicationContext(), "opencv loaded successfully.", Toast.LENGTH_SHORT).show();
    }else
    {
        Toast.makeText(getApplicationContext(), "could not load opencv .", Toast.LENGTH_SHORT).show();
    }

When I run the Project I get the Toast ""could not load opencv .".

Community
  • 1
  • 1
Steffen
  • 89
  • 1
  • 1
  • 9
  • 1
    It might be better to add this as an update to your question, rather than as an answer. – Mick Sep 10 '18 at 15:18