1. Introduction

The goal of this article is to create an Android AAR library with three-party dependencies that will serve as a foundation for subsequent technologies. This article uses the third-party Gradle plugin fat-aar to create a simple custom AAR library that relies on okHTTP.

2. The library to create

Create a project called DemoAAR using Android Studio. Then create the Library libCommUI in this project as File -> New Module -> Android Library.

Introduce a fat-AAR plug-in in the libCommUI module. The steps are as follows:

  1. Add a dependency to build. Gradle of DemoAAR. Note how fat-aar matches gradle Plugin and Gralde versions. One problem with the integration process is that the latest version of the plug-in is 1.3.x, but this version could not package the dependency libraries into the final AAR after it was built, for reasons unknown, so 1.2.x was used.
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com. Android. Tools. Build: gradle: 4.4.1' 
        classpath 'com. Kezong: fat - the aar: 1.2.20'}}Copy the code
  1. Add the plugin to libCommUI, build.gralde, which looks like this:
apply plugin: 'com.android.library' 
boolean buildAAR = true

if (buildAAR) {
    apply plugin: 'com.kezong.fat-aar'
} 
android {  
    defaultConfig {
       ...
    }
}

dependencies {
    ... 
    if (buildAAR) {
        embed ('com. Squareup. Okhttp3: okhttp: 3.12.1') {}}else {
        implementation 'com. Squareup. Okhttp3: okhttp: 3.12.1'}}if (buildAAR) {
    configurations.embed.transitive = true
}

Copy the code
  1. Outputs the libcommui-xxx.aar library in build/outputs/aar.

3. The test

Test the packaged library libcommui.aar. By creating a new Android project called DemoUseLibCommUI. After creating the libCommUI library: File -> New Module -> import.jar /.AAR Package, select the AAR File and click OK.

Next add a dependency on the libCommUI library to the project: right-click the project name -> Open Module Settings, as shown below. After you add libCommUI, you can invoke the functions provided by libCommUI.