2

I want to try one of the sample codes shipped with SDK, namely "tutorial-2-mixedprocessing". I followed these steps:

1) Import Project -> selected tutorial directory

2) I named it “tutorial-2-mixedprocessing1”, left 3 options ticked (“Replace jars&lib sources when possible” and “Create Gradle-style module names”)

3) Warning: “NDK integration deprecated in the current plugin” I clicked “Set "android.useDeprecatedNdk=true" in gradle.properties to continue using the current NDK integration”

4) Import module -> Selected java module which is in “C:\SDKs\OpenCV-android-sdk\sdk\java” in my case -> Left 3 checkbox ticked

5) Error:Cause: failed to find target with hash string 'android-14' in: C:\Users\c010\AppData\Local\Android\Sdk Install missing platform(s) and sync project. I changed compileSdkversion, buildToolsVersion and targetSdkVersion and synced. Error dissapeared.

6-7) Open module settings -> Dependencies Added Opencv module as module dependency. Added NDK location (“C:\SDKs\android-ndk-r12-beta2” in my case)

8) Build -> make project leads Error:(2, 33) opencv2/core/core.hpp: No such file or directory

In Eclipse I would add some include paths, but I don’t know how to do it in Android Studio. I thought editing Android.mk could help:

My Android.mk was

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

ifdef OPENCV_ANDROID_SDK
  ifneq ("","$(wildcard $(OPENCV_ANDROID_SDK)/OpenCV.mk)")
    include ${OPENCV_ANDROID_SDK}/OpenCV.mk
  else
    include ${OPENCV_ANDROID_SDK}/sdk/native/jni/OpenCV.mk
  endif
else
  include ../../sdk/native/jni/OpenCV.mk
endif

LOCAL_MODULE    := mixed_sample
LOCAL_SRC_FILES := jni_part.cpp
LOCAL_LDLIBS +=  -llog -ldl

include $(BUILD_SHARED_LIBRARY)

I changed it to plain absolute path

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include C:\SDKs\OpenCV-android-sdk\sdk\native\jni\OpenCV.mk

LOCAL_MODULE    := mixed_sample
LOCAL_SRC_FILES := jni_part.cpp
LOCAL_LDLIBS +=  -llog -ldl

include $(BUILD_SHARED_LIBRARY)

Didn’t help.

I added 'OPENCV_ANDROID_SDK := C:\SDKs\OpenCV-android-sdk\' before “ifdef ...” line, same result.

I added 'LOCAL_C_INCLUDES += C:\SDKs\OpenCV-android-sdk\sdk\native\jni\include' , same result.

I edited gradle file which looked like

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "org.opencv.samples.tutorial2"
        minSdkVersion 8
        targetSdkVersion 8

        ndk {
            moduleName "mixed_sample"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile project(':openCVLibrary310')
}

I changed ndk bracket as follows:

ndk {
    moduleName "mixed_sample"
    cFlags  += "-I${file("C:/SDKs/OpenCV-android-sdk/sdk/native/jni/include")}".toString()
}

Error:error: null-IC:\SDKs\OpenCV-android-sdk\sdk\native\jni\include: Invalid argument

It didn't accept cppFlags Error:(14, 0) Could not find property 'cppFlags' on com.android.build.gradle.internal.dsl.NdkOptions_Decorated@797a42f2.

jannarc
  • 93
  • 2
  • 8

2 Answers2

1

If you want to make NDK module you need to change your gradle to experimental. In opencv gradle files and in your gradle files.

So, firstly change your gradle module:

buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle-experimental:0.4.0'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
   }
}

And your app build.gradle should be something like this.

apply plugin: 'com.android.model.application'

model {

android {
    compileSdkVersion = 23
    buildToolsVersion = "23.0.2"
    defaultConfig.with {
        applicationId = "your package name"
        minSdkVersion.apiLevel = 15
        targetSdkVersion.apiLevel = 23
        versionCode = 1
        versionName = "1.0"
    }

}
android.ndk {
    moduleName = "viulib_interface_jni"
    cppFlags.add("-fexceptions")
    cppFlags.add("-I" + file("src/main/jni").absolutePath)
    stl = "gnustl_shared" // Which STL library to use: gnustl or stlport
    ldLibs.addAll(["android", "EGL", "GLESv2", "dl", "log", "z"])
}

android.productFlavors {
    create("arm") {
        ndk.with{
            abiFilters.add("armeabi")

            File curDir = file('./')
            curDir = file(curDir.absolutePath)
            String libsDir = curDir.absolutePath + "/src/main/jniLibs/armeabi/"

        }
    }
    create("armv7") {
        ndk.with {
            abiFilters.add("armeabi-v7a")

            File curDir = file('./')
            curDir = file(curDir.absolutePath)
            String libsDir = curDir.absolutePath + "/src/main/jniLibs/armeabi-v7a/"
        }
    }
}


}

dependencies {
compile fileTree(include: ['*.jar', '*.so'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile project('openCVLibrary310')
}

I use gradle 0.4.0 experimetal, but you can choose also gradle 0.7.0, but in this case, you need to see what are the changes in gradle 0.7.0 syntax.

I recommend you, to start creating your JNI bridge to use opencv c++ functions and after try to create build gradle files for opencv library.

Don't forget to import opencv folder as a library in file->new->import project. Hope, this code will be helpful

Cheers.

uelordi
  • 2,189
  • 3
  • 21
  • 36
  • You have a string named 'libsDir'. Where is it used? With your help and the tutorial at [here](https://codelabs.developers.google.com/codelabs/android-studio-jni/index.html?index=..%2F..%2Findex#0), it compiles, but now I see some linker errors (like 'undefined reference to `cv::FastFeatureDetector::create(int, bool, int)''). – jannarc Jun 15 '16 at 11:27
  • libsDir is a directory path where there are located 3rdparty libraries to link. If you don't need to use. Your linking problem comes because in the gradle files you are not linking opnecv c++ .so files. This issue is more extensive and difficult. You can try this link, http://stackoverflow.com/questions/34701003/android-studio-linking-opencv-static-libraries, it is not solved but may be you will have more ideas. – uelordi Jun 15 '16 at 12:23
0

add following lines in your build.gradle

sourceSets.main {

    jni.srcDirs = [] //disable automatic ndk-build call
    jniLibs.srcDir 'src/main/jniLibs'
}

buildTypes { ... }

Avinash
  • 264
  • 5
  • 15