2016-03-29 35 views
1

我想通過使用gradle-experimental:0.6.0-beta5構建一個OpenCV 3.0.0的本機應用程序,而我的Android Studio版本是v2.1預覽1.我已按照
Android Studio linking OpenCV static libraries

Building native openCV with NDK on android studio
提供申報我會在的build.gradle文件中使用靜態庫的方法,我已經複製第三方庫/src目錄/ main/jniLibs/armeabi/3rdparty/OpenCV4Android與Gradle實驗的鏈接錯誤:0.6.0-beta5

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 
} 

但不幸的是我仍然獲得了約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' 

看來libtbb.a仍不能得到解決,即使我已經把它的預構建和依賴塊。 我應該如何解決這個問題?

+0

對於靜態預建庫,在使用'ldLibs' * VS *'nativeplatform.PrebuiltLibraries'沒有缺點。無論如何,** tbb **必須是列表中的最後一個,因此可能更改** jni.dependencies **的順序可以產生差異 –

+1

謝謝@AlexCohn。我重新排列了依賴關係的順序,它現在可以工作了! – jxsun

回答

2

我改變了@AlexCohn建議的jni.dependencies的順序,現在它就像一個魅力一樣。所以我想在這裏對誰可能面臨的一些問題,分享我的鏈接順序: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" 
      } 
     } 
    } 
} 
+0

我相信所有的第三方庫都應該放在opencv之後。無論如何,你現在可以「接受」你的答案。 –

+0

謝謝。我已經更新了我的答案。 – jxsun