How to package multiple aars and jars into one aar (including stepping records)

In the past few days of work, I need to package our company's aar and jar again to generate an aar for customers to use. So a new module was created, which placed aar and jar, and some encapsulation of the calling interface.

(There are a lot of aars and jars in the project, so I didn’t screen them, so I just stuck them in ==)

Then when building. You will encounter the problem of Direct local .aar file dependencies are not supported when building an AAR....

How to solve it?

At this time, you need to put in the build.gradle of the module

implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])

changed to

compileOnly fileTree(dir: 'libs', include: ['*.jar', '*.aar'])

Then you can generate aar in this way.

But when this aar is put into the written demo to run and call, it will report java.lang.NoClassDefFoundError error

This is because the jar and aar under libs are not entered when typing aar.

At this time we can use fat-aar-android

For specific use, you can also refer to the official document fat-aar-android

1. Add in the project root build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.github.kezong:fat-aar:1.3.8'
    }
}

2. Add in build.gradle in module

apply plugin: 'com.kezong.fat-aar'

3. Add lib and aar that need to be embedded into aar in dependencies

dependencies {
    compileOnly fileTree(dir: 'libs', include: ['*.jar', '*.aar'])

    embed(name: 'sdd-1.0.0', ext: 'jar')
    embed(name: '111-1.60', ext: 'jar')
    embed(name: 'vaar..dd', ext: 'aar')
    embed(name: 'vaar..dd', ext: 'aar')
    ...
}

But at this time, when packing, it will report

A problem occurred configuring project ':androidVFServiceLib'.

> Could not resolve all dependencies for configuration ':androidVFServiceLib:embed'.

> Could not find :atoolsjar-1.0.0:.

Required by:

project :androidVFServiceLib

> Could not find :bcprov-jdk15on-1.60:.

Required by:

project :androidVFServiceLib

> Could not find :clssKernel:.

此时还需要在mudule中的build.gradle 中添加

    repositories {
        flatDir {
            dirs 'libs'
        }
    }

然后就能正常生成aar了。

但是假如libs中的aar或者jar 需要.so 文件,在生成aar时,记得也要把so 文件拷贝到存放aar和jar的module 中,

并且需要配置

        ndk {
            // Specifies the ABI configurations of your native
            // libraries Gradle should build and package with your APK.
            abiFilters 'armeabi-v7a'
        }

不然,在别处调用时,会报so 库的问题。

这样的话,生成的aar 就能在其它项目进行正常使用了。

Guess you like

Origin blog.csdn.net/qq_36162336/article/details/129378768