1. 程式人生 > >Android Studio 打包AAR和第三方靜態庫

Android Studio 打包AAR和第三方靜態庫

contain ise ply appcompat pla 依賴 prop 三方庫 spa

需求

現在有一個第三方庫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庫

Android Studio 打包AAR和第三方靜態庫