在将C ++代码集成到我的Android应用程序中时遇到了一些问题。 我使用Djinni库生成了我在应用程序中使用的JNI代码。 我正在使用CMake构建我的C ++库,并且在此过程中链接了OpenCV库(带有.so文件)。 我使用NDK r19。
我在项目中集成了一个C ++类,该类可以引发一个异常std::runtime_error
,但是该异常具有我不理解的行为。
第一个问题:
我在https://developer.android.com/ndk/guides/cmake处研究了文档,并使用属性-DANDROID_CPP_FEATURES=rtti exceptions
启用了C ++异常。
这样,一旦在C ++中引发异常,我就会得到SIGABRT,这会使我的应用程序崩溃。
即使C ++中的抛出被catch(...)
块包围,也会发生这种情况。
我还尝试通过-fexceptions
应用标志-frtti
和cppFlags
,但是没有成功。
第二个问题:
我使用了另一个具有属性-DANDROID_STL
的STL,并将其放在c++_shared
中。使用此属性,可以在某些体系结构上正确处理我的异常。
但是在armv7l
架构(使用System.getProperty("os.arch")
找到)上,抛出该异常会导致SIGSEGV,这会使我的应用程序崩溃。
这是我的android
文件的build.gradle
部分:
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 ++文件的命令(在-v
中使用cppFlags
找到):
"/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错误的东西。
我想问题出在我的构建过程中,但是我不知道该怎么做才能解决它。
答案 0 :(得分:0)
opencv可能构建不正确。有关诊断的说明,请参见https://github.com/android-ndk/ndk/issues/889。只需对您的opencv库执行相同的操作,就象该错误对qt所建议的那样。