1、编译时报错“multiple define of …",如下图是接入openssl aes时的报错信息:
- 导致出现这个问题的原因是,我在一个c++文件里面#include “md5.cpp”,同时又在CMakeLists.txt声明:
所以,编译时候就会出现重复定义的错误 - 解决方案有两个:
1)将C++文件的#include "md5.cpp"改为引用头文件#include “md5.h”
2)或者,删掉CMakeLists.txt里面声明的“ src/main/jni/md5.cpp”
add_library( # Specifies the name of the library.
sswlEncrypt
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/jni/aes256.cpp
src/main/jni/aes128cbc.cpp
src/main/jni/JNIHello.cpp
src/main/jni/rsa.cpp
src/main/jni/jsoncpp.cpp
src/main/jni/AES128.cpp
)
2、编译时报错“error: undefined reference to ‘EVP_aes_128_cbc’”,如下图是接入openssl aes的报错信息:
- 解决方案:
1)在CMakeLists.txt声明引入对应的.so文件:
# 头文件包含的目录
include_directories(
src/main/jni/openssl
src/main/jni)
# 添加依赖libcrypto.so
add_library(
libcrypto
SHARED
IMPORTED)
# 添加依赖libcrypto.so所在的位置
set_target_properties(
libcrypto
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/${CMAKE_ANDROID_ARCH_ABI}/libcrypto.so)
# 添加依赖libssl.so
add_library(
libssl
SHARED
IMPORTED)
# 添加依赖libssl.so所在的位置
set_target_properties(
libssl
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/${CMAKE_ANDROID_ARCH_ABI}/libssl.so)
# 将所有.so文件链接起来
target_link_libraries( # Specifies the target library.
sswlEncrypt
libcrypto
libssl
# Links the target library to the log library
# included in the NDK.
${log-lib})
3、编译时报错“open:Invalid argument ,clang++:error:linker command failed with exit code 1”,如下图:
- 解决方案:
1)删除该项目的build、.externalNativeBuild目录,重新编译即可
4、有时库包明明已经#include引入进来,但是调用里面的方法时候,编译器依然报错没找到对应方法,可以Build->Refresh Linked C++ Projects来刷新一下C++项目的链接即可
5、编译时报错“ninja: error: ‘…’, needed by ‘…’, missing and no known rule to make it”,如下图:
-
报错原因是:提供的libcrypto.so只有arm64-v8a架构的,但是打出libsswlEncrypt.so时候却指定要打出x86_64架构的
-
解决方案:
1)添加对应架构的libcrypto.so
2)或者abiFilters只指定libcrypto.so支持的架构,比如这个例子,就只指定abiFilters 'arm64-v8a'
6、编译时报错“ABIs [armeabi] are not supported for platform. Supported ABIs are [arm64-v8a, armeabi-v7a, x86, x86_64].”,如下图所示:
- 报错原因:由于ndk(v17)已不在支持mips、armeabi等CPU架构,只支持armeabi-v7a, arm64-v8a, x86, x86_64,一般来说Android Studio 3.3.2之后,默认ndk都是v17之后版本,所以要想打出armeabi架构的.so文件,需要用低于17版本的ndk
- 解决方案:
1)去ndk官方地址下载一个低于17版本的ndk:旧版本ndk官方下载地址
2)下载之后,解压出来,然后按步骤配置好,重新编译即可: