我正在尝试为 Android 应用程序构建一个原生 C++ 库。这个库在内部使用了 Tensorflow C++ API(版本 1.9.0 和 1.10.0)和 OpenCV(3.3.0)。为了生成包装器,我使用的是 Swig。我正在使用 Android NDK 构建 *.so 文件(我尝试过使用不同版本的 NDK 版本 10、14、15、17)。我已经从源代码构建了 *.a (libtensorflow-core.a, nsync.a, libprotobuf.a) 文件。
defaultConfig {
minSdkVersion 21
targetSdkVersion 28
}
我有下一个文件: Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libnsync
LOCAL_SRC_FILES := $(PATH_TO_TENSORFLOW)/tensorflow/contrib/makefile/downloads/nsync/builds/armeabi-v7a.android.c++11/nsync.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := libprotobuf
LOCAL_SRC_FILES := $(PATH_TO_TENSORFLOW)/tensorflow/contrib/makefile/gen/protobuf_android/armeabi-v7a/lib/libprotobuf.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := libtensorflow
LOCAL_SRC_FILES := $(PATH_TO_TENSORFLOW)/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a
LOCAL_EXPORT_C_INCLUDES := \
$(PATH_TO_TENSORFLOW) \
$(PATH_TO_TENSORFLOW)/tensorflow/contrib/makefile/gen/host_obj \
$(PATH_TO_TENSORFLOW)/tensorflow/contrib/makefile/downloads/eigen \
$(PATH_TO_TENSORFLOW)/tensorflow/contrib/makefile/gen/protobuf_android/armeabi-v7a/include \
$(PATH_TO_TENSORFLOW)/tensorflow/core/public \
$(PATH_TO_TENSORFLOW)/tensorflow/contrib/makefile/downloads/nsync/public
LOCAL_STATIC_LIBRARIES += libprotobuf libnsync
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_ARM_NEON := true
OPENCV_CAMERA_MODULES := off
include $(OPENCV_PACKAGE_DIR)/native/jni/OpenCV.mk
LOCAL_MODULE := libOcr
LOCAL_SRC_FILES := ocr/Ocr_wrap.cxx
LOCAL_LDLIBS += -landroid -llog -ldl
LOCAL_CPPFLAGS += -std=c++11
LOCAL_STATIC_LIBRARIES += libprotobuf libtensorflow
include $(BUILD_SHARED_LIBRARY)
应用程序.mk
APP_STL := gnustl_shared
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-21 #updated from 19 to 21
APP_OPTIM := release
LOCAL_ARM_NEON := true
在构建过程中,我遇到了很多错误,最后一个是:
[armeabi-v7a] SharedLibrary : libOcr.so
[armeabi-v7a] Install : liblept.so => libs/armeabi-v7a/liblept.so
[armeabi-v7a] Install : libtess.so => libs/armeabi-v7a/libtess.so
[armeabi-v7a] Install : libgnustl_shared.so => libs/armeabi-v7a/libgnustl_shared.so
/Users/tensorflow/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a(str_util.o):str_util.cc:function tensorflow::str_util::CUnescape(tensorflow::StringPiece, std::string*, std::string*): error: undefined reference to 'std::__throw_out_of_range_fmt(char const*, ...)'
/Users/tensorflow/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a(log_memory.o):log_memory.cc:function tensorflow::LogMemory::RecordStep(long long, std::string const&): error: undefined reference to 'std::__throw_out_of_range_fmt(char const*, ...)'
/Users/tensorflow/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a(log_memory.o):log_memory.cc:function tensorflow::LogMemory::RecordTensorAllocation(std::string const&, long long, tensorflow::Tensor const&): error: undefined reference to 'std::__throw_out_of_range_fmt(char const*, ...)'
/Users/tensorflow/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a(log_memory.o):log_memory.cc:function tensorflow::LogMemory::RecordTensorDeallocation(long long, std::string const&): error: undefined reference to 'std::__throw_out_of_range_fmt(char const*, ...)'
collect2: error: ld returned 1 exit status
make: *** [/Users/app/app/src/main/obj/local/armeabi-v7a/libOcr.so] Error 1
make: Leaving directory `/Users/app/app/src/main/jni'
:sdk:buildNative FAILED
FAILURE: Build failed with an exception.
为了构建 *.so 文件,我使用 build.gradle 文件中的下一个任务:
commandLine "$ndkDir/ndk-build",
'-C', file('src/main/jni').absolutePath,
'-j', Runtime.runtime.availableProcessors(),
'all',
'NDK_LOG=1'
亲爱的社区,请帮助我解决这些问题。谢谢你。
更新:APP_PLATFORM
更改为android-21
,更好的结果,但仍然有错误