我想在我的Android项目中使用用C++编写的exiv2库。为了做到这一点,我尝试使用Android NDK交叉编译该库。
为了进行交叉编译,我遵循以下步骤:
将NDK路径添加到PATH变量中
PATH="/home/patrycja/android-packages/ndk:${PATH}" export PATH
安装用于交叉编译C/C++的标准工具链。
./make-standalone-toolchain.sh --platform=android-21 --install-dir=/tmp/my-android-toolchain --ndk-dir='/home/patrycja/android-packages/ndk/' --toolchain=arm-linux-androideabi-4.9 --system=linux-x86_64
输出:
Copying prebuilt binaries... Copying sysroot headers and libraries... Copying c++ runtime headers and libraries... Copying files to: /tmp/my-android-toolchain Cleaning up... Done.
设置一些环境变量,以便配置和构建过程使用正确的编译器。
export PATH=/tmp/my-android-toolchain/bin:$PATH export CC="arm-linux-androideabi-gcc" export CXX="arm-linux-androideabi-g++" export CFLAGS='-mthumb -O2' export CXXFLAGS='-mthumb -O2' export LDFLAGS='-Wl,--fix-cortex-a8' export LIBS='-lstdc++ -lsupc++'
构建静态库和足够的头文件
./configure --prefix=$(pwd)/build --host=arm-linux-androideabi --disable-shared --disable-xmp --disable-nls
因此,我创建了“build”类别的文件:
├── bin
│ └── exiv2
├── include
│ └── exiv2
│ ├── *.hpp
│
├── lib
│ ├── libexiv2.a
│ ├── libexiv2.la
│ └── pkgconfig
│ └── exiv2.pc
└── share
└── man
└── man1
└── exiv2.1
我将创建的静态库libexiv2.a
和include
文件夹复制到了我的Android项目中的appName/src/main/jni/prebuild
目录下。
Android.mk
文件如下:
LOCAL_PATH := $(call my-dir)
# Static library information
LOCAL_MODULE := exiv2
LOCAL_SRC_FILES := ../prebuild/libexiv2.a
LOCAL_EXPORT_C_INCLUDES := ../prebuild/include/
LOCAL_EXPORT_LDLIBS := -lz
include $(PREBUILT_STATIC_LIBRARY)
# Wrapper information
include $(CLEAR_VARS)
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../prebuild/include/
LOCAL_MODULE := helloJNI
LOCAL_SRC_FILES := helloJNI.cpp
LOCAL_STATIC_LIBRARIES := exiv2
include $(BUILD_SHARED_LIBRARY)
在我的Android封装中,我尝试使用这个库。代码如下:
#include <string.h>
#include <jni.h>
#include <exiv2/exiv2.hpp>
extern "C" {
JNIEXPORT jstring JNICALL Java_com_example_patrycja_testndi2_MyActivity_helloJNI(JNIEnv *env, jobject thiz)
{
std::ostringstream os;
std::string file("/storage/emmc/DCIM/100MEDIA/IMAG0021.jpg");
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(file);
return env->NewStringUTF("asldjaljd");
}
}
然而ndk-build
输出说找不到它。
[arm64-v8a] Compile++ : helloJNI <= helloJNI.cpp
[arm64-v8a] SharedLibrary : libhelloJNI.so
jni/../prebuild/libexiv2.a: error adding symbols: File in wrong format
collect2: error: ld returned 1 exit status
make: *** [obj/local/arm64-v8a/libhelloJNI.so] Error 1
我认为交叉编译中的参数存在问题。我尝试了几个选项,但仍有问题。
我遵循了这些指示:如何在本机 Android 代码中使用外部 C++ 库
jni/../prebuild/armeabi-v7a/libexiv2.a(pngchunk.o):pngchunk.cpp:function Exiv2::Internal::PngChunk::parseChunkContent(Exiv2::Image*, unsigned char const*, long, Exiv2::DataBuf): error: undefined reference to 'std::__throw_out_of_range_fmt(char const*, ...)'
- rocksparrow