4

I am integrating opencv library with my project. I've successfully configured NDK using experimental gradle plugin. But getting error with Opencv. Here is the error showing in my cpp file.

enter image description here

My build.gradle is:

apply plugin: 'com.android.model.application'
model {
     android {
       compileSdkVersion = 23
       buildToolsVersion = "23.0.0"

       defaultConfig.with {
        applicationId = "com.legalplex.dharani.android"
        minSdkVersion.apiLevel = 14
        targetSdkVersion.apiLevel = 23

        buildConfigFields.with {
            create() {
                type = "int"
                name = "VALUE"
                value = "1"
            }
        }
    }
  }
  android.ndk {
    moduleName = "document_scanner"
     cppFlags += "-fno-rtti"
     cppFlags += "-fno-exceptions"
     ldLibs    = ["android", "log"]

    stl ="gnustl_shared"
   }
   android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles += file('proguard-rules.pro')
        }
   }
   android.productFlavors {
    // for detailed abiFilter descriptions, refer to "Supported ABIs" @
    // https://developer.android.com/ndk/guides/abis.html#sa
    create("arm") {
        ndk.abiFilters += "armeabi"
    }
    create("arm7") {
        ndk.abiFilters += "armeabi-v7a"
    }
    create("arm8") {
        ndk.abiFilters += "arm64-v8a"
    }
    create("x86") {
        ndk.abiFilters += "x86"
    }
    create("x86-64") {
        ndk.abiFilters += "x86_64"
    }
    create("mips") {
        ndk.abiFilters += "mips"
    }
    create("mips-64") {
        ndk.abiFilters += "mips64"
    }
    // To include all cpu architectures, leaves abiFilters empty
    create("all")
    }
    }
    dependencies {
        compile 'com.android.support:support-v4:19.1.0'
       compile project(':openCVLibrary')
    }

Why it is showing error at opencv includes even after adding module dependency in my gradle file. Please help me out. How to configure my build.gradle file to activate our own Android.mk. Here is my Android.mk file:

  LOCAL_PATH := $(call my-dir)
  include $(CLEAR_VARS)
  include OpenCV-2.4.10-android-sdk\sdk\native\jni\OpenCV.mk
  OPENCV_INSTALL_MODULES := on
  LOCAL_MODULE := document_scanner
  LOCAL_SRC_FILES := jni_part.cpp
  LOCAL_C_INCLUDES := OpenCV-2.4.10-android-sdk\sdk\native\jni\include
  OPENCV_LIB_TYPE:=STATIC
  LOCAL_LDLIBS += -llog
  include $(BUILD_SHARED_LIBRARY)
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
sravanalakshmi.sunkara
  • 1,161
  • 2
  • 13
  • 35
  • did you ever get this figured out? I'm working on a similar project right now and I have also run into this issue. – bstar55 Apr 26 '16 at 00:34
  • No I have not found solution. But as of i know Android Studio has experimental NDK Support. May be that is the reason it is not able to identity third party libraries like OpenCV. – sravanalakshmi.sunkara Apr 26 '16 at 09:31

1 Answers1

-1

You should a) enable ndk-build, and b) disable ndk plugin. Code below is tuned for Mac, on Windows you need ndk-build.cmd:

task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
    def ndkDir = android.ndkDirectory
    commandLine "$ndkDir/ndk-build"
}
buildNative.onlyIf {
    def ndkDir = android.ndkDirectory
    file("$ndkDir/ndk-build").exists()
}

task cleanNative(type: Exec, description: 'Clean JNI object files') {
    def ndkDir = android.ndkDirectory
    commandLine "$ndkDir/ndk-build", 'clean'
}
cleanNative.onlyIf {
    def ndkDir = android.ndkDirectory
    file("$ndkDir/ndk-build").exists()
}

clean.dependsOn 'cleanNative'

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn buildNative
}

defaultConfig.ndk {
    moduleName "pexeso-android-mobile"
}

tasks.all {
    task -> if (task.name.contains('compileDebugNdk') || task.name.contains('compileReleaseNdk')) task.enabled = false
}

UPDATE similar approach works with the experimental plugin, see define LOCAL_SRC_FILES in ndk{} DSL.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • where should i place this? It is showing Error: No signature of method: org.gradle.model.dsl.internal.NonTransformedModelDslBacking.dependsOn() is applicable for argument types: (java.lang.String) values: [cleanNative] Possible solutions: respondsTo(java.lang.String) Open File – sravanalakshmi.sunkara Aug 28 '15 at 10:01
  • I forgot to mention that I use `'com.android.tools.build:gradle:1.2.3'`, not the *experimental* plugin. The new DSL not complete yet, see *http://ph0b.com/new-android-studio-ndk-support* – Alex Cohn Aug 28 '15 at 19:31