2017-08-17 154 views
1

我試圖擴展this project,但它是使用ANT構建的,現在我們在實踐中使用Android Studio和Gradle。我想在Android Studio上進一步實現它。因此,這裏是我的項目結構...錯誤:org.gradle.internal.UncheckedException:構建命令失敗

enter image description here

應用/的build.gradle

apply plugin: 'com.android.application' 
android { 
    compileSdkVersion 25 
    buildToolsVersion "26.0.0" 
    defaultConfig { 
     applicationId "tum.andrive" 
     minSdkVersion 14 
     targetSdkVersion 25 
     versionCode 1 
     versionName "1.0" 
     ndk { 
      abiFilters "armeabi", "armeabi-v7a" 
     } 
     sourceSets { 
      main { 
       jniLibs.srcDirs = ['src/main/cpp'] 
      } 
     } 
     externalNativeBuild { 
      cmake { 
       cppFlags "-frtti -fexceptions" 
      } 
     } 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    externalNativeBuild { 
     cmake { 
      path "CMakeLists.txt" 
     } 
    } 
} 

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

的CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1) 
add_library(# Sets the name of the library. 
      native-lib 
      # Sets the library as a shared library. 
      SHARED 
      # Provides a relative path to your source file(s). 
      src/main/cpp/native-lib.cpp) 

find_library(# Sets the name of the path variable. 
       log-lib 
       # Specifies the name of the NDK library that 
       # you want CMake to locate. 
       log) 

SET(OpenCV_DIR $ENV{OPENCVSDK}/native/jni) 

message(STATUS "opencv found: ${OpenCV_LIBS}") 

include_directories(${OpenCV_DIR}/include/) 

target_link_libraries(# Specifies the target library. 
         native-lib 
         # Links the target library to the log library 
         # included in the NDK. 
         ${log-lib}) 

Android.mk

LOCAL_PATH := $(call my-dir) 

include $(CLEAR_VARS) 

#OpenCv 
OPENCV_CAMERA_MODULES:=on 
OPENCV_INSTALL_MODULES:=on 

include $(OPENCVSDK)/native/jni/OpenCV.mk 

LOCAL_MODULE := AndriveNative 
LOCAL_SRC_FILES := native-lib.cpp 
LOCAL_LDLIBS += -llog -ldl 

include $(BUILD_SHARED_LIBRARY) 

但是,得到錯誤,而建設項目,對這個一看...

留言查看

enter image description here

搖籃控制檯

D:\Android\workspace\Local\Andrive\app\.externalNativeBuild\cmake\debug\armeabi 
     --target 
     native-lib 
     jvmArgs : 

Starting process 'command 'Z:\Android\sdk\cmake\3.6.4111459\bin\cmake.exe''. Working directory: D:\Android\workspace\Local\Andrive\app Command: Z:\Android\sdk\cmake\3.6.4111459\bin\cmake.exe --build D:\Android\workspace\Local\Andrive\app\.externalNativeBuild\cmake\debug\armeabi --target native-lib 
Successfully started process 'command 'Z:\Android\sdk\cmake\3.6.4111459\bin\cmake.exe'' 
[1/1] Linking CXX shared library ..\..\..\..\build\intermediat 
es\cmake\debug\obj\armeabi\libnative-lib.so 
FAILED: cmd.exe /C "cd . && Z:\Android\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv5te-none-linux-androideabi --gcc-toolchain=Z:/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64 --sysroot=Z:/Android/sdk/ndk-bundle/sysroot -fPIC -isystem Z:/Android/sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=14 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv5te -mtune=xscale -msoft-float -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -frtti -fexceptions -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a --sysroot Z:/Android/sdk/ndk-bundle/platforms/android-14/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ..\..\..\..\build\intermediates\cmake\debug\obj\armeabi\libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -llog -lm "Z:/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi/libgnustl_static.a" "-latomic" && cd ." 
D:\Android\workspace\Local\Andrive\app\src\main\cpp/native-lib.cpp:27: error: undefined reference to 'DetectionBasedTracker::Parameters::Parameters()' 
D:\Android\workspace\Local\Andrive\app\src\main\cpp/native-lib.cpp:30: error: undefined reference to 'DetectionBasedTracker::DetectionBasedTracker(std::string const&, DetectionBasedTracker::Parameters const&)' 
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o(.ARM.extab.text.Java_tum_andrive_DetectionBasedTracker_nativeCreateObject+0xe4): error: undefined reference to 'typeinfo for cv::Exception' 
D:\Android\workspace\Local\Andrive\app\src\main\cpp/native-lib.cpp:54: error: undefined reference to 'DetectionBasedTracker::getParameters()' 
D:\Android\workspace\Local\Andrive\app\src\main\cpp/native-lib.cpp:56: error: undefined reference to 'DetectionBasedTracker::setParameters(DetectionBasedTracker::Parameters const&)' 
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o(.ARM.extab.text.Java_tum_andrive_DetectionBasedTracker_nativeSetDetectionSize+0xa4): error: undefined reference to 'typeinfo for cv::Exception' 
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o(.ARM.extab.text.Java_tum_andrive_DetectionBasedTracker_nativeDestroyObject+0xa4): error: undefined reference to 'typeinfo for cv::Exception' 
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o(.ARM.extab.text.Java_tum_andrive_DetectionBasedTracker_nativeStart+0xa4): error: undefined reference to 'typeinfo for cv::Exception' 
D:/Android/OpenCV-2.4.10-android-sdk/sdk/native/jni/include\opencv2/core/mat.hpp:188: error: undefined reference to 'cv::_OutputArray::_OutputArray(cv::Mat&)' 
D:/Android/OpenCV-2.4.10-android-sdk/sdk/native/jni/include\opencv2/core/mat.hpp:188: error: undefined reference to 'cv::Mat::copyTo(cv::_OutputArray const&) const' 
D:/Android/OpenCV-2.4.10-android-sdk/sdk/native/jni/include\opencv2/core/mat.hpp:298: error: undefined reference to 'cv::Mat::copySize(cv::Mat const&)' 
D:/Android/OpenCV-2.4.10-android-sdk/sdk/native/jni/include\opencv2/core/mat.hpp:27 
8: error: undefined reference to 'cv::fastFree(void*)' 
D:/Android/OpenCV-2.4.10-android-sdk/sdk/native/jni/include\opencv2/core/mat.hpp:367: error: undefined reference to 'cv::Mat::deallocate()' 
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation) 
ninja: build stopped: subcommand failed. 
:app:externalNativeBuildDebug FAILED 
:app:externalNativeBuildDebug (Thread[Daemon worker Thread 2,5,main]) completed. Took 8.011 secs. 
:app:buildInfoGeneratorDebug (Thread[Daemon worker Thread 2,5,main]) started. 
:app:buildInfoGeneratorDebug 
Putting task artifact state for task ':app:buildInfoGeneratorDebug' into context took 0.0 secs. 
Executing task ':app:buildInfoGeneratorDebug' (up-to-date check took 0.0 secs) due to: 
    Task has not declared any outputs. 
:app:buildInfoGeneratorDebug (Thread[Daemon worker Thread 2,5,main]) completed. Took 0.018 secs. 

FAILURE: Build failed with an exception. 

* What went wrong: 
Execution failed for task ':app:externalNativeBuildDebug'. 
> Build command failed. 
Error while executing process Z:\Android\sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {--build D:\Android\workspace\Local\Andrive\app\.externalNativeBuild\cmake\debug\armeabi --target native-lib} 
[1/1] Linking CXX shared library ..\..\..\..\build\intermediates\cmake\debug\obj\armeabi\libnative-lib.so 
FAILED: cmd.exe /C "cd . && Z:\Android\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv5te-none-linux-androideabi --gcc-toolchain=Z:/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64 --sysroot=Z:/Android/sdk/ndk-bundle/sysroot -fPIC -isystem Z:/Android/sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=14 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv5te -mtune=xscale -msoft-float -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -frtti -fexceptions -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a --sysroot Z:/Android/sdk/ndk-bundle/platforms/android-14/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ..\..\..\..\build\intermediates\cmake\debug\obj\armeabi\libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -llog -lm "Z:/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi/libgnustl_static.a" "-latomic" && cd ." 
D:\Android\workspace\Local\Andrive\app\src\main\cpp/native-lib.cpp:27: error: undefined reference to 'DetectionBasedTracker::Parameters::Parameters()' 
    D:\Android\workspace\Local\Andrive\app\src\main\cpp/native-lib.cpp:30: error: undefined reference to 'DetectionBasedTracker::DetectionBasedTracker(std::string const&, DetectionBasedTracker::Parameters const&)' 
    CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o(.ARM.extab.text.Java_tum_andrive_DetectionBasedTracker_nativeCreateObject+0xe4): error: undefined reference to 'typeinfo for cv::Exception' 
    D:\Android\workspace\Local\Andrive\app\src\main\cpp/native-lib.cpp:54: error: undefined reference to 'DetectionBasedTracker::getParameters()' 
    D:\Android\workspace\Local\Andrive\app\src\main\cpp/native-lib.cpp:56: error: undefined reference to 'DetectionBasedTracker::setParameters(DetectionBasedTracker::Parameters const&)' 
    CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o(.ARM.extab.text.Java_tum_andrive_DetectionBasedTracker_nativeSetDetectionSize+0xa4): error: undefined reference to 'typeinfo for cv::Exception' 
    CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o(.ARM.extab.text.Java_tum_andrive_DetectionBasedTracker_nativeDestroyObject+0xa4): error: undefined reference to 'typeinfo for cv::Exception' 
    CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o(.ARM.extab.text.Java_tum_andrive_DetectionBasedTracker_nativeStart+0xa4): error: undefined reference to 'typeinfo for cv::Exception' 
    D:/Android/OpenCV-2.4.10-android-sdk/sdk/native/jni/include\opencv2/core/mat.hpp:188: error: undefined reference to 'cv::_OutputArray::_OutputArray(cv::Mat&)' 
    D:/Android/OpenCV-2.4.10-android-sdk/sdk/native/jni/include\opencv2/core/mat.hpp:188: error: undefined reference to 'cv::Mat::copyTo(cv::_OutputArray const&) const' 
    D:/Android/OpenCV-2.4.10-android-sdk/sdk/native/jni/include\opencv2/core/mat.hpp:298: error: undefined reference to 'cv::Mat::copySize(cv::Mat const&)' 
    D:/Android/OpenCV-2.4.10-android-sdk/sdk/native/jni/include\opencv2/core/mat.hpp:278: error: undefined reference to 'cv::fastFree(void*)' 
    D:/Android/OpenCV-2.4.10-android-sdk/sdk/native/jni/include\opencv2/core/mat.hpp:367: error: undefined reference to 'cv::Mat::deallocate()' 
    clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation) 
    ninja: build stopped: subcommand failed. 

有人請幫我設置這個項目!

+0

您的應用程序沒有openCV作爲CMakeLists.txt中的依賴項庫。需要將它添加到target_link_libraries(...)中,併爲每個構建的ABI添加正確的路徑。 – Gerry

+0

您可否在這裏重新編寫CMakeLists.txt以供參考? – Khan

+0

您的cmake腳本不具有等同於:「include $(OPENCVSDK)/ native/jni/OpenCV。mk「,CMake build不支持混合構建系統(CMake和ndk-build在同一個項目中),所以選擇將OpenCV.mk轉換爲CMake構建腳本或者使用Android Studio」ndkbuild「插件。可以參考:https://github.com/googlesamples/android-ndk,分支master-ndkbuild [主分支僅用於CMake構建] – Gerry

回答

0

經過大量的搜索,我發現this項目。非常感謝Adrien Lescourt。這個演示項目描述了我們如何使用OpenCV與Android Studio & Gradle。問題在於jniLibs目錄和CMakeLists.txt文件。下面是正確的版本...

應用/的build.gradle

apply plugin: 'com.android.application' 
android { 
    compileSdkVersion 25 
    buildToolsVersion "26.0.0" 
    defaultConfig { 
     applicationId "tum.andrive" 
     minSdkVersion 14 
     targetSdkVersion 19 
     versionCode 1 
     versionName "1.0" 
     ndk { 
      abiFilters "armeabi-v7a" 
     } 
     sourceSets { 
      main { 
       jniLibs.srcDirs = ['src/main/jniLibs'] 
      } 
     } 
     externalNativeBuild { 
      cmake { 
       cppFlags "-frtti -fexceptions" 
       abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips', 'mips64' 
      } 
     } 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    externalNativeBuild { 
     cmake { 
      path "CMakeLists.txt" 
     } 
    } 
} 

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

的CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1) 

# OpenCV stuff 
include_directories(D:\\Android\\OpenCV-2.4.10-android-sdk\\sdk\\native\\jni\\include) 
add_library(lib_opencv SHARED IMPORTED) 
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java.so) 

add_library(native-lib    
      SHARED       
      src/main/cpp/native-lib.cpp) 

find_library(log-lib 
       log) 

target_link_libraries(native-lib      
         lib_opencv 
         ${log-lib}) 

,最後我添加jniLibs文件夾的應用程序,並複製全部.so此文件夾中的文件存儲在OpenCV-2.4.10-android-sdk/sdk/native/libs

enter image description here

我希望這個解決方案將幫助人們建立OpenCV的與gradle這個。如果需要其他信息,請發表評論。

謝謝