错误:org.gradle.internal.UncheckedException:构建命令失败

时间:2017-08-17 06:30:55

标签: android opencv ant android-ndk android-gradle

我正在尝试扩展this project,但它是使用ANT构建的,现在我们在实践中使用Android Studio和Gradle。我想在Android Studio上移动它以进行进一步的实现。所以这是我的项目结构......

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

Gradle Console

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.

任何人请帮我设置这个项目!

1 个答案:

答案 0 :(得分:0)

经过大量搜索,我找到了this项目。非常感谢Adrien Lescourt。该演示项目描述了我们如何在Android Studio& amp; amp; amp; amp;摇篮。问题出在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文件夹添加到应用程序并复制了存储在 OpenCV-2.4.10-android-sdk / sdk / native / libs中的此文件夹中的所有 .so 文件

enter image description here

我希望此解决方案可以帮助人们使用gradle设置 OpenCV 。如果需要任何其他信息,请评论。

由于