如今有一個第三方庫libstatic_add.a和對應的頭文件static.h,要求封裝一個Module,該Module依賴這個靜態庫,要求打包的Module包含該靜態庫。android
建立Project時,記得添加"Include C++ Support",若是沒有安裝CMake,LLDB和NDK的話,記得安裝一下。app
項目建立好後,包括如下目錄和文件(部分目錄或文件是後面添加的):工具
針對app模塊的build.gradle進行以下配置: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'
}
針對app模塊的local.propertise進行以下配置:ui
## 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\\
針對app模塊下的src\main\cpp\CMakeLists.txt配置以下:spa
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所在路徑爲「app\build\outputs\aar\app-debug.aar」debug
能夠將app-debug.aar改成app-debug.zip,使用壓縮工具打開,能夠看到以下:3d
打開jni文件夾,能夠看到裏面包含了一個armeabi-v7a文件夾,該文件夾包含了libnative-lib.so庫code