Android Studio 打包AAR和第三方静态库

Posted 且听风吟-wuchao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Studio 打包AAR和第三方静态库相关的知识,希望对你有一定的参考价值。

需求

现在有一个第三方库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和第三方静态库的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio 打包 jar 及 aar 包

Android Studio 打包 jar 及 aar 包

Android Studio 打包 jar 及 aar 包

Android Studio中打包aar并引用及打jar包

Android Studio中打包aar并引用及打jar包

Android Studio中打包aar并引用及打jar包