1

I am trying to build a native application with OpenCV 3.0.0 by using gradle-experimental:0.6.0-beta5, and my Android Studio version is v2.1 Preview 1. I have followed the approaches provided in
Android Studio linking OpenCV static libraries
and
Building native openCV with NDK on android studio
to declare the static libraries I will use in the build.gradle file, and I have already copied the 3rd party libraries to /src/main/jniLibs/armeabi/3rdparty/:

def openCVAndroidLibDir = file(project(':app').projectDir).absolutePath + "/src/main/jniLibs"
def openCVAndroidSdkDir = "/home/jxsun/OpenCV-android-sdk/sdk/native/jni/include/opencv2"

model {
repositories {
    prebuilt(org.gradle.nativeplatform.PrebuiltLibraries) {
        libtiff {
            binaries.withType(StaticLibraryBinary) {
                staticLibraryFile = file("${openCVAndroidLibDir}/${targetPlatform.getName()}/3rdparty/liblibtiff.a")
            }
        }
        ...
        tbb {
            binaries.withType(StaticLibraryBinary) {
                staticLibraryFile = file("${openCVAndroidLibDir}/${targetPlatform.getName()}/3rdparty/libtbb.a")
            }
        }

        opencv_hal {
            headers.srcDir "${openCVAndroidSdkDir}"
            binaries.withType(StaticLibraryBinary) {
                staticLibraryFile = file("${openCVAndroidLibDir}/${targetPlatform.getName()}/libopencv_hal.a")
            }
        }
        opencv_core {
            headers.srcDir "${openCVAndroidSdkDir}"
            binaries.withType(StaticLibraryBinary) {
                staticLibraryFile = file("${openCVAndroidLibDir}/${targetPlatform.getName()}/libopencv_core.a")
            }
        }
        ...
        opencv_videostab {
            headers.srcDir "${openCVAndroidSdkDir}"
            binaries.withType(StaticLibraryBinary) {
                staticLibraryFile = file("${openCVAndroidLibDir}/${targetPlatform.getName()}/libopencv_videostab.a")
            }
        }
    }
}

android.sources {
    main {
        jni {
            dependencies {
                library "IlmImf" linkage "static"
                ...
                library "tbb" linkage "static"

                library "opencv_core" linkage "static"
                ...
                library "opencv_videostab" linkage "static"
                library "opencv_hal" linkage "static"
            }
        }
    }
}

android.ndk {
    moduleName = "test"
    cppFlags.add("-std=c++11")
    cppFlags.add("-fexceptions")
    cppFlags.add("-frtti")
    cppFlags.add("-I${file("/home/jxsun/OpenCV-android-sdk/sdk/native/jni/include")}".toString())
    cppFlags.add("-I${file("/home/jxsun/OpenCV-android-sdk/sdk/native/jni/include/opencv")}".toString())
    cppFlags.add("-I${file("/home/jxsun/OpenCV-android-sdk/sdk/native/jni/include/opencv2")}".toString())
    abiFilters.add("armeabi")
    ldLibs.addAll(["android", "EGL", "GLESv2", "dl", "log", "z"])
    stl = "gnustl_shared"
    platformVersion = 21
}

But unfortunately I still get a lot of undefined reference errors about tbb:

/home/jxsun/StudioProjects/Test/app/src/main/jniLibs/armeabi/libopencv_core.a(parallel.cpp.o):parallel.cpp:function tbb::interface6::internal::start_for<tbb::blocked_range<int>, (anonymous namespace)::ProxyLoopBody, tbb::auto_partitioner const>::~start_for(): error: undefined reference to 'vtable for tbb::task'

It seems libtbb.a still can't be resolved even I have put it in the prebuilt and dependencies blocks. How should I solve this problem?

Community
  • 1
  • 1
jxsun
  • 163
  • 2
  • 7
  • With static prebuilt libraries, there are no drawbacks using `ldLibs` *vs* `nativeplatform.PrebuiltLibraries`. At any rate, **tbb** must be the last in the list, so probably changing the order of **jni.dependencies** can make the difference – Alex Cohn Mar 29 '16 at 09:49
  • 1
    Thank you @AlexCohn. I rearranged the order of dependencies and it works now! – jxsun Mar 30 '16 at 07:38

1 Answers1

2

I have changed the order of jni.dependencies as suggested by @AlexCohn, and it works like a charm now. So I would like to share my link order here for who may face the some problem:D

android.sources {
    main {
        jni {
            dependencies {
                /*
                 * OpenCV
                 */
                library "opencv_flann" linkage "static"
                library "opencv_imgproc" linkage "static"
                library "opencv_ml" linkage "static"
                library "opencv_photo" linkage "static"
                library "opencv_video" linkage "static"
                library "opencv_imgcodecs" linkage "static"
                library "opencv_shape" linkage "static"
                library "opencv_videoio" linkage "static"
                library "opencv_highgui" linkage "static"
                library "opencv_objdetect" linkage "static"
                library "opencv_superres" linkage "static"
                library "opencv_ts" linkage "static"
                library "opencv_features2d" linkage "static"
                library "opencv_calib3d" linkage "static"
                library "opencv_stitching" linkage "static"
                library "opencv_videostab" linkage "static"
                library "opencv_core" linkage "static"
                library "opencv_hal" linkage "static"

                /*
                 * OpenCV 3rd parties
                 */
                library "IlmImf" linkage "static"
                library "libjpeg" linkage "static"
                library "libjasper" linkage "static"
                library "libpng" linkage "static"
                library "libwebp" linkage "static"
                library "libtiff" linkage "static"
                library "tbb" linkage "static"
            }
        }
    }
}
jxsun
  • 163
  • 2
  • 7
  • I believe that all 3rd party libs should go after opencv ones. At any rate, you can now "accept" your answer. – Alex Cohn Mar 30 '16 at 17:22