我在Android应用程序中集成C++代码时遇到了一些问题。我使用Djinni库生成了我在应用程序中使用的JNI代码。我正在使用CMake来构建我的C++库,并在这个过程中链接OpenCV库(与.so文件)。我用的是NDK r19。
我有一个集成到项目中的C++类,它可以抛出一个异常std::runtime_error
,但这个异常的行为我不理解。
第一个问题:
我看了医生https://developer.android.com/ndk/guides/cmake并且使用属性CCD_ 2来启用C++异常。这样,一旦我的异常在C++中抛出,我就会得到一个SIGABRT,它会使我的应用程序崩溃。即使C++中的throw被catch(...)
块包围,也会发生这种情况。我还尝试应用标志-fexceptions
和-frtti
到cppFlags
,但没有成功。
第二个问题:
我使用了另一个属性为-DANDROID_STL
的STL,并将其设置为c++_shared
。有了这个属性,我的异常在某些体系结构上得到了正确的处理。但在体系结构armv7l
(使用System.getProperty("os.arch")
找到)上,抛出会导致SIGSEGV,这会使我的应用程序崩溃。
这里是我的build.gradle
文件的android
部分:
android {
compileSdkVersion Versions.COMPILE_SDK
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion Versions.MIN_SDK
targetSdkVersion Versions.TARGET_SDK
versionCode 1
versionName "1.0"
externalNativeBuild {
cmake {
cppFlags "-v"
arguments "-DANDROID_CPP_FEATURES=rtti exceptions", "-DANDROID_STL=c++_shared"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
sourceSets {
main.jniLibs.srcDirs += 'src/main/jniLibs'
}
}
这是我的CMakeLists.txt
文件:
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Library name
set(library_name native-lib)
# Path to djinni support code
set(support_dir src/djinni/support-lib/jni)
# Path to generated code and own c++ implementation
set(include_dirs src/djinni/generated/jni src/djinni/generated/cpp src/main/cpp)
# Djinni support code that needs to be compiled
file(
GLOB_RECURSE support_srcs
${support_dir}/*.cpp)
# Generated code and c++ implementations that needs to be compiled
file(
GLOB_RECURSE lib_srcs
src/djinni/generated/cpp/*.cpp
src/djinni/generated/jni/*.cpp
src/main/cpp/*.cpp)
# All the implementation files that make up our library
set(complete_srcs ${support_srcs} ${lib_srcs})
# Define library referring to the sources above
add_library(${library_name} SHARED ${complete_srcs})
# Add OpenCV library
set(opencv_library_name opencv)
set(opencv_dir src/main/jniLibs/${CMAKE_ANDROID_ARCH_ABI})
add_library(${opencv_library_name} SHARED IMPORTED)
set_target_properties(${opencv_library_name} PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/${opencv_dir}/libopencv_java3.so)
# We link opencv to our datamatrix library
target_link_libraries(${library_name} ${opencv_library_name})
# Define INCLUDE DIRECTORIES property for native-lib
target_include_directories(${library_name} PUBLIC ${include_dirs} ${support_dir})
以下是在我的应用程序的构建过程中构建单个C++文件的命令(使用cppFlags
中的-v
找到):
"/Users/me/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++" -cc1 -triple i686-none-linux-android21 -emit-obj -mrelax-all -mnoexecstack -disable-free -disable-llvm-verifier -discard-value-names -main-file-name usercodedecoder.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu i686 -target-feature +ssse3 -dwarf-column-info -debug-info-kind=standalone -dwarf-version=4 -debugger-tuning=gdb -target-linker-version 241.9 -v -ffunction-sections -fdata-sections -coverage-notes-file /Users/me/Documents/workspace/my_project/app/.externalNativeBuild/cmake/debug/x86/CMakeFiles/native-lib.dir/src/main/cpp/usercodedecoder.cpp.gcno -resource-dir /Users/me/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/lib64/clang/8.0.2 -dependency-file CMakeFiles/native-lib.dir/src/main/cpp/usercodedecoder.cpp.o.d -sys-header-deps -MT CMakeFiles/native-lib.dir/src/main/cpp/usercodedecoder.cpp.o -D datamatrix_native_lib_EXPORTS -I ../../../../src/djinni/generated/jni -I ../../../../src/djinni/generated/cpp -I ../../../../src/main/cpp -I ../../../../src/djinni/support-lib/jni -D ANDROID -isysroot /Users/me/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot -internal-isystem /Users/me/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/c++/v1 -internal-isystem /Users/me/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/local/include -internal-isystem /Users/me/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/lib64/clang/8.0.2/include -internal-externc-isystem /Users/me/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/i686-linux-android -internal-externc-isystem /Users/me/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/include -internal-externc-isystem /Users/me/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include -O0 -Wformat -Werror=format-security -fdeprecated-macro -fdebug-compilation-dir /Users/me/Documents/workspace/my_project/app/.externalNativeBuild/cmake/debug/x86 -ferror-limit 19 -fmessage-length 0 -stack-protector 2 -mstackrealign -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -o CMakeFiles/native-lib.dir/src/main/cpp/usercodedecoder.cpp.o -x c++ ../../../../src/main/cpp/usercodedecoder.cpp
我对这类问题做了一些研究,但找不到能帮助我消除某些架构上SIGSEGV错误的东西。
我想问题出在我的构建过程中,但我不知道该怎么解决
opencv可能是错误构建的。看见https://github.com/android-ndk/ndk/issues/889有关诊断的说明。只需对您的opencv库执行与该错误建议的qt相同的操作。