欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

AndroidStudio管理多个c++子项目编译出多个so库

时间:2023-05-31
一、需求

AndroidStudio 合作开发的时候,管理多个库,编译多个so库的情况,其中jniLibs.armeabi-v7a是编译出的库文件目录,例如

 二、管理三个子项目的CMakeLists

# 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)#set(CMAKE_CXX_FLAGS "-Wno-error=format-security -Wno-error=pointer-sign")set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/jniLibs/${ANDROID_ABI})#设置头文件搜索路径(和此txt同个路径的头文件无需设置),可选#INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/common)#指定用到的系统库或者NDK库或者第三方库的搜索路径,可选。#link_DIRECTORIES(/usr/local/lib)#添加子目录,将自动找到子目录中的CMakeLists.txtADD_SUBDIRECTORY(${PROJECT_SOURCE_DIR}/jsoncpp)ADD_SUBDIRECTORY(${PROJECT_SOURCE_DIR}/websocketclient)ADD_SUBDIRECTORY(${PROJECT_SOURCE_DIR}/janusclientsdk)

三、janusclientsdk、jsoncpp、websocketclient的CMakeLists

CMakeLists内容:

#继承上一层的CMakeLists.txt的变量,也可以在这里重新赋值#C 的编译选项是 CMAKE_C_FLAGS# 指定编译参数,可选#SET(CMAKE_CXX_FLAGS "-Wno-error=format-security -Wno-error=pointer-sign")#生成动态库名称、类型、资源文件add_library(janusclientsdk SHARED janusclientsdk.cpp)#依赖库find_library(log-lib log)#target_link_libraries(janusclientsdk ${log-lib})

 四、build.gradle中添加配置
添加externalNativeBuild配置,参考如下:

apply plugin: 'com.android.application'def LIBWEBRTC_HOME_PATH = "I:/webrtc/android/webrtc_m84_20201001/webrtc_android/src/"//def LIBWEBRTC_HOME_PATH = "I:/webrtc/android/androidnativeapi/app/webrtc/"android { compileSdkVersion 28 defaultConfig { applicationId "org.webrtc.examples.androidnativeapi" minSdkVersion 21 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" externalNativeBuild { cmake { arguments "-DLIBWEBRTC_HOME_PATH=" + LIBWEBRTC_HOME_PATH, "-DANDROID_STL=c++_static" } } ndk { abiFilters "armeabi-v7a" } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } externalNativeBuild { cmake { path "src/main/cpp/CMakeLists.txt" } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } sourceSets { main { // 1、配置在根目录libs下可以加载第三方so库, (最好不要创建jniLibs, 在众多的开源库中可能会引起冲突,还没发现) // 2、运行时会自动将libs目录下的so库拷贝到指定目录 // 3、如果自己创建的so不需要重新编译,可以将(app/build/intermediates/transforms)生成的so拷贝到这个目录 jniLibs.srcDirs = ['libs'] // 如果是单个文件夹 可以直接这样如下配置 // jniLibs.srcDir 'libs' } } buildToolsVersion '28.0.2'//新增}repositories { flatDir{ dirs'libs' }}dependencies { implementation fileTree(dir: "libs", include: ["*.aar"]) //implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.android.support:appcompat-v7:28.0.0-rc02' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' //implementation(name: 'libwebrtc', ext: 'aar') //implementation 'org.webrtc:google-webrtc:1.0.+'}

五、结果

 

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。