需求

现在有一个第三方库libstatic_add.a和对应的头文件static.h,要求封装一个Module,该Module依赖这个静态库,要求打包的Module包含该静态库。

方案

创建Android Studio Library  Project

创建Project时,记得添加"Include C++ Support",如果没有安装CMake,LLDB和NDK的话,记得安装一下。

项目创建好后,包括以下目录和文件(部分目录或文件是后面添加的):

build.gradle配置

针对app模块的build.gradle进行如下配置:

//改为library
apply plugin: 'com.android.library' //读取local.properties里的配置属性add_static_lib.dir,该属性指明libstatic_add.a静态库所在目录
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def add_static_lib = properties.getProperty('add_static_lib.dir') android {
compileSdkVersion 27
defaultConfig {
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" //添加下面
ndk {
abiFilters 'armeabi-v7a'
// these platforms cover 99% percent of all Android devices
} externalNativeBuild {
cmake {
//添加下面
arguments '-DANDROID_PLATFORM=android-14',
'-DANDROID_TOOLCHAIN=clang',
'-DANDROID_ARM_NEON=TRUE',
'-DANDROID_STL=gnustl_static',
"-DPATH_TO_ADD_STATIC_LIB:STRING=${add_static_lib}"
cFlags '-O3', '-fsigned-char' // full optimization, char data type is signed
cppFlags '-fsigned-char', "-I${add_static_lib}"
}
}
}
//添加下面
sourceSets {
main {
jniLibs.srcDirs = ['src/main/cpp']
}
}
//添加下面
externalNativeBuild {
cmake {
path 'src/main/cpp/CMakeLists.txt'
}
}
//添加下面
sourceSets.main {
jni.srcDirs = []
jniLibs.srcDir 'src/main/libs'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
} dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.android.support:design:27.1.1'
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'
}

local.properties配置

针对app模块的local.propertise进行如下配置:

## This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Wed May 16 13:05:05 CST 2018
ndk.dir=D\:\\NanRi\\Android\\android-ndk-r16b
sdk.dir=C\:\\Users\\suning\\AppData\\Local\\Android\\Sdk //添加如下,指明libstatic_add.a静态库所在目录
add_static_lib.dir=D\:\\NanRi\\Android\\Android-Studio-Project\\ModuleProject\\app\\src\\main\\libs\\

CMakeLists.txt配置

针对app模块下的src\main\cpp\CMakeLists.txt配置如下:

cmake_minimum_required(VERSION 3.4.1)

set(
PATH_TO_ADD_STATIC_LIB
CACHE STRING ""
) message(${ANDROID_ABI}) file(GLOB CPP_FILES "*.cpp") add_library(
native-lib
SHARED
${CPP_FILES}
) include_directories(src/main/cpp)
include_directories(${PATH_TO_ADD_STATIC_LIB}) target_link_libraries(
native-lib
android
OpenSLES
${PATH_TO_ADD_STATIC_LIB}/libstatic_add.a
)

导出AAR

导出的AAR所在路径为“app\build\outputs\aar\app-debug.aar”

可以将app-debug.aar改为app-debug.zip,使用压缩工具打开,可以看到如下:

打开jni文件夹,可以看到里面包含了一个armeabi-v7a文件夹,该文件夹包含了libnative-lib.so库

最新文章

  1. nginx启动、关闭、重启
  2. java系列: 对不起,JavaFX——Java 8目前还不能救你(zz)
  3. crontab在一秒内刷新多次导致部分脚本不生效的问题分析
  4. IDF实验室-简单编程-特殊的日子 writeup
  5. bzoj 1500: [NOI2005]维修数列 splay
  6. Html 小插件5 百度搜索代码2
  7. jQuery常用方法集锦
  8. input的placeholder字体大小无法修改?
  9. 采药 NOIP 2005 普及组
  10. Percona Toolkit 2.2.19 is now available
  11. 深透清晰理解Java高并发概述
  12. MDK填充FLASH为0xFF
  13. luogu P1003 铺地毯
  14. Shell脚本笔记(四)条件判断
  15. ANSYS耦合
  16. Spring生态研习【四】:Springboot+mybatis(探坑记)
  17. buildroot构建项目(二)--- u-boot 2017.11 建立 2440 开发板
  18. c# ProgressBar进度条方向和美观
  19. mybatis WARN No appenders could be found for logger的解决方法
  20. Kconfig介绍

热门文章

  1. window10+linux双系统安装
  2. Qt 编程指南 4 单行编辑控件
  3. VS2013+QT5.3 中文乱码和中文路径不识别
  4. 转载 SpringMVC详解(三)------基于注解的入门实例
  5. 如何优雅地使用Sublime Text3(转)
  6. Qt中静态变量使用方法
  7. AI 判别式模型和生成式模型
  8. SVN 安装vs插件
  9. 置顶博客一览表($My~Top~List$)
  10. MIPI接口资料汇总(精)