/OpenCV4Android

OpenCV4Android android studio project

Primary LanguageC++

OpenCV4Android

Download

This project is created from opencv, with the helper script from eclipse2as.sh

Use OpenCV in your Android studio project

Add native bundle plugin to your project build.gradle
buildscript {
    //...
    dependencies {
        //...
        classpath 'com.ydq.android.gradle.build.tool:nativeBundle:1.0.4'
        //...
    }
}

Use and apply plugin in your app build.gradle

apply plugin: 'com.android.application'
apply plugin: 'com.ydq.android.gradle.native-aar.import'
Use the published version from jCenter
dependencies {
    //...
    implementation 'com.wanghong.opencv:opencv-with-header:3.1.1-dev'
    //...
}
Use this library and start develop your OpenCV app
  • Create a reference in your CMakeLists.txt if you use cmake build system
include (${ANDROID_GRADLE_NATIVE_BUNDLE_PLUGIN_MK})
target_link_libraries(native-lib ${ANDROID_GRADLE_NATIVE_MODULES})

Check the information from here if you use other build system

  • Verify in your code
#include <jni.h>
#include <string>
#include <opencv/cv.h>

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_myapp_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++" + cv::getBuildInformation();
    return env->NewStringUTF(hello.c_str());
}
  • Enjoy!

Troubleshoot

  • gnustl_static has been removed started from ndk r18, so make sure you use ndk version before r18 release.

Reference

  • Many thanks to this header bundle plugin, it's hard to config and use in newer android studio without this plugin