Android Studio Packs AAR and third-party static libraries

Source: Internet
Author: User

Demand

There is now a third-party library libstatic_add.a and the corresponding header file static.h, which requires a module to be encapsulated, which relies on this static library and requires the packaged module to contain the static library.

Scenario Create Android Studio Library Project

When you create project, remember to add "Include C + + support" and if you don't have CMAKE,LLDB and NDK installed, remember to install it.

After the project is created, include the following directories and files (some directories or files are added later):

Build.gradle Configuration

The Build.gradle for the app module is configured as follows:

Change to Libraryapply plugin: ' com.android.library '//Read the configuration attribute Add_static_lib.dir in Local.properties, which indicates libstatic_ ADD.A Static Library is located in the directory 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 targetsdkversion versioncode 1 Versionname "1.0" Testinstrumentationrunner "Android.support.test.runner.AndroidJUnitRunner"//Add the following NDK {Abifilter  S ' armeabi-v7a '//These platforms cover 99% percent of all Android devices} Externalnativebuild                        {cmake {//Add below arguments '-dandroid_platform=android-14 ', '-dandroid_toolchain=clang ', '-dandroid_arm_neon=true ', '-dandroid_stl=gnu Stl_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}}}}//Add the following sourcesets {main {jnilibs.srcdirs = [' Src/main/cpp '}}//Add the following Externalnativebuild {CMAK        e {path ' Src/main/cpp/cmakelists.txt '}}//Add the following sourcesets.main {jni.srcdirs = [] Jnilibs.srcdir ' Src/main/libs '} buildtypes {release {minifyenabled false Proguar  Dfiles 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. ' Androidtestimplementation ' com.android.support.test:runner:1.0.2 ' androidtestimplementation ' com.android.support.test.espresso:espresso-core:3.0.2 '}
Local.properties Configuration

The Local.propertise for the app module is configured as follows:

# # This file must *not* is checked into Version Control systems,# as it contains information specific to your local Co nfiguration.## location of the SDK.  This was only used by gradle.# for customization if using a Version Control System, please read the# header note. #Wed May 13:05:05 CST 2018ndk.dir=d\:\\nanri\\android\\android-ndk-r16bsdk.dir=c\:\\users\\suning\\appdata\\local\\ android\\sdk//Add the following to indicate the directory where the LIBSTATIC_ADD.A static library is located
Add_static_lib.dir=d\:\\nanri\\android\\android-studio-project\\moduleproject\\app\\src\\main\\libs\\
CMakeLists.txt Configuration

The Src\main\cpp\cmakelists.txt configuration for the app module is as follows:

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)

Exporting AAR

The exported AAR is located on the path "App\build\outputs\aar\app-debug.aar"

You can change the App-debug.aar to App-debug.zip and use the compression tool to open it, and you can see the following:

Open the Jni folder and you can see that it contains a armeabi-v7a folder containing the libnative-lib.so library

Android Studio Packs AAR and third-party static libraries

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.