android studio调用c++ so库
·
1.实现test.cpp,test.cpp中包含对c++ so库要使用方法的jni定义,比如
libpclient.so有个头文件webrtctest.h内容如下:int test();
test.cpp中对接口test()的jni定义如下:
#include <jni.h>
#include "webrtctest.h" // 包含需要调用的头文件
extern "C" {
JNIEXPORT jint JNICALL
Java_com_example_mytest_MainActivity_test(JNIEnv *env, jobject /* this */) {
// 调用 libpclient.a 中的 test() 函数
return test();
}
} // extern "C"
2.实现cmakelists.txt,我实现的cmakelists.txt内容如下:
cmake_minimum_required(VERSION 3.18.1)
# 设置 C++ 标准为 C++20
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# 添加你的静态库目录
set(LIBS_DIR ${CMAKE_SOURCE_DIR}/jniLibs/arm64-v8a)
set(INHEAD_DIR ${CMAKE_SOURCE_DIR}/jniLibs)
add_definitions(
-D__STDC_CONSTANT_MACROS
-D__STDC_FORMAT_MACROS
-D_FORTIFY_SOURCE=2
-D_GNU_SOURCE
-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE
-DANDROID
-DHAVE_SYS_UIO_H
-DANDROID_NDK_VERSION_ROLL=r25c_1
-DCR_CLANG_REVISION=\"llvmorg-18-init-12422-g74cdb8e6-1\"
-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS
-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS
-DCR_LIBCXX_REVISION=6cd38f6d30dda7bed3df08427db0b2a4e3fa9e47
-DNDEBUG
-DNVALGRIND
-DDYNAMIC_ANNOTATIONS_ENABLED=0
-DWEBRTC_ENABLE_PROTOBUF=1
-DWEBRTC_STRICT_FIELD_TRIALS=0
-DWEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE
-DRTC_ENABLE_VP9
-DRTC_DAV1D_IN_INTERNAL_DECODER_FACTORY
-DWEBRTC_HAVE_SCTP
-DWEBRTC_USE_H264
-DWEBRTC_ENABLE_LIBEVENT
-DWEBRTC_ARCH_ARM64
-DWEBRTC_HAS_NEON
-DWEBRTC_LIBRARY_IMPL
-DWEBRTC_ENABLE_AVX2
-DWEBRTC_NON_STATIC_TRACE_EVENT_HANDLERS=0
-DWEBRTC_POSIX
-DWEBRTC_LINUX
-DWEBRTC_ANDROID
-DABSL_ALLOCATOR_NOTHROW=1
-DGOOGLE_PROTOBUF_NO_RTTI
-DGOOGLE_PROTOBUF_NO_STATIC_INITIALIZER
-DGOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL_INLINE=0
-DHAVE_PTHREAD
)
# 添加头文件路径
include_directories(
${INHEAD_DIR}
#${INHEAD_DIR}/include
#${INHEAD_DIR}/include/third_party/abseil-cpp
# ${INHEAD_DIR}/include/third_party
#${INHEAD_DIR}/include/third_party/jsoncpp/source/include
#${INHEAD_DIR}/include/third_party/jsoncpp/source/src/lib_json
#${INHEAD_DIR}/include/third_party/libyuv/include
#${INHEAD_DIR}/include/buildtools/third_party/libc++
#${INHEAD_DIR}/include/third_party/protobuf/src
)
add_library(liblog SHARED IMPORTED)
set_target_properties(liblog PROPERTIES IMPORTED_LOCATION ${LIBS_DIR}/liblog.so)
add_library(libwebrtc STATIC IMPORTED)
set_target_properties(libwebrtc PROPERTIES IMPORTED_LOCATION ${LIBS_DIR}/libwebrtc.a)
add_library(pclient STATIC IMPORTED)
set_target_properties(pclient PROPERTIES IMPORTED_LOCATION ${LIBS_DIR}/libpclient.a)
# 链接静态库到你的本地库
add_library(
testwebrtc
SHARED
test.cpp
)
# 将native-lib与静态库链接
target_link_libraries(
testwebrtc
log
libwebrtc
pclient
)
# 设置编译选项
target_compile_options(testwebrtc PRIVATE
-Wall
-fPIC
# other options
)
要生成的jni so库的名称为libtestwebrtc.so,依赖静态库libpclient.a与libwebrtc.a,主要用来进行接口测试
3.在build.gradle中添加ndk的目录与cmakelists.txt的目录,定义只编译arm64-v8a以及设置apk打包库的目录
android {
compileSdkVersion 29
buildToolsVersion "29.0.0"
defaultConfig {
applicationId "com.example.mytest"
minSdkVersion 29
targetSdkVersion 29
versionCode 1
versionName "1.0"
ndkVersion "27.0.12077973"
ndk {
abiFilters 'arm64-v8a' // arm64-v8a、armeabi-v7a、x86_64、x86 中的一个或多个
}
sourceSets.main {
jni.srcDirs = ['jniLibs'] //disable automatic ndk-build call
}
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.18.1"
// other cmake configurations
}
}
}
4.在mainactivity加载jni库,声明jni方法,并调用
companion object {
init {
System.loadLibrary("testwebrtc")
}
}
// 声明本地方法
external fun test(): Int
更多推荐
所有评论(0)