Android studio packaging-aar nested reference to local aar packaging

Android studio module packaging is generally packaged in the form of aar. If the module refers to a local aar, the referenced aar will not be packaged when it is packaged. This design idea is better and can avoid the problem of package conflicts. For example, the app refers to two aars (1.aar and 2.aar), both 1.aar and 2.aar depend on the third-party 3.aar, if both 1.aar and 2.aar package 3.aar into it When App again refers to 1.aar and 2.aar, there will be a reference conflict because of 3.aar.

In actual development, third-party libraries will be referenced, but you do n’t want the reference to know what third-party libraries are used, so you need to type the third-party libraries into your own aar, this article will Introduction, how to deal with this situation.

1. Create a project

Take a look at the directory structure of the project.
Insert picture description here
There are three modules basiclib, exlib, and pkglib
basiclib in the above figure : The modules referenced by exlib (exlib will refer to the aar packaged by basiclib). Basiclib's internal implementation is very simple, only one sentence is printed.
Insert picture description here
exlib : Modules that need to be provided externally (will eventually be packaged in the form of aar), call the interface of the basiclib module.
Insert picture description here
pkglib : responsible for packaging, packaging all the dependent packages of exlib.

2. Pack and nest aar

pkglib is mainly responsible for packaging, then look at the implementation of pkglib's build.gradle

apply plugin: 'java'
version = 1.0
buildscript {

    repositories {
        google()
        mavenCentral()
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'
    }
}

repositories {
    google()
    mavenCentral()
    jcenter()
    maven {
        url "https://maven.google.com"
    }
}

dependencies {
    implementation project(':exlib') //此处填写需要打包的Android Library Project name
}

//把所有依赖的 jar 包拷贝至 build/outputs/aar/libs
task syncAllJars() {
    // 使用了绝对路径,需要根据实际情况调整
    // 将指定路径下的所有 jar 包,打包至被打包的路径下
    def dir = new File('D:\\hosh\\android\\hosh\\MqttPhone\\exlib\\libs')
    files(dir.listFiles()).each { file ->
        if (file.name.endsWith('.jar')) {
            copy {
                into buildDir.getPath() + "/outputs/aar/libs"
                from file.absolutePath
            }
        }
    }
}

// 把所有依赖的 aar 包中包含的 classes.jar
// 都拷贝到 build/outputs/aar/libs下,并重命名以不被覆盖
task syncAllAars(dependsOn:':exlib:assemble') {
    def jarName
    def aarPath
    def destDir = buildDir.getPath()+"/outputs/aar"
    // 使用了绝对路径,需要根据实际情况调整
    def dir = new File('D:\\hosh\\android\\hosh\\MqttPhone\\exlib\\libs')
    files(dir.listFiles()).each { file ->
        aarPath = file.absolutePath

        if (file.name.endsWith('.aar')) {
            jarName = "libs/" + file.name.replace(".aar",".jar")
            copy {
                from zipTree(aarPath)
                into destDir
                include "**/*"
                rename 'classes.jar', jarName
            }
        }
    }
}

// 将多个 jar 包打包成一个 classes.jar
task makeJar(type: Jar) {
    archiveName = 'classes.jar'
    def dir = new File(buildDir.getPath()+"/outputs/aar/libs")
    files(dir.listFiles()).each { file ->

        if (file.name.endsWith('.jar')) {
            from (project.zipTree(file.absolutePath))
        }
    }
    destinationDirectory = file(buildDir.getPath()+"/outputs/aar")
}

task fataar(dependsOn:[syncAllAars, syncAllJars, makeJar]) {
}

//生成最终 aar 包,libs 目录需要被排除
task genAar(dependsOn:[fataar], type: Zip) {
    def destDir = buildDir.getPath()+"/outputs/aar"
    baseName "wholeSDK"
    extension "aar"
    version '1.1'
    destinationDirectory = file('libs/')
    from destDir
    exclude "libs"
}

There are prerequisites for dependencies. First, you need to package the basiclib module, generate basiclib-debug.aar and copy it to the libs directory under the exlib module, and package the exlib module to generate exlib-debug.aar and copy it to the libs directory under the exlib module .
Insert picture description here
The packaging process of pkglib is mainly to process the libs directory of the exlib module.

The last execution gradlew pkglib: genAar to
Insert picture description here

3. Test nested aar

After executing the gradlew pkglib: genAar command, the following files will be generated. The files
Insert picture description here
generated in the intermediate process are in the build / outputs directory, and the wholeSDK-1.1.aar is finally needed in the libs directory .

In an empty project, test wholeSDK-1.1.aar .
Insert picture description here
Insert picture description here
I saw the print log in basiclib, indicating that the packaging was successful.

Published 34 original articles · Like 34 · Visitors 60,000+

Guess you like

Origin blog.csdn.net/qq_19154605/article/details/105532443