如何把多个aar,jar 打包到一个aar 中(含踩坑记录)

在这几天的工作中,我需要对我司的aar和jar 再做一次封装,生成一个aar给客户使用。于是就新建了一个module,里面放置aar和jar,以及一些调用接口的封装。

(项目里的aar和jar 有点多,没做甄别,一股脑全粘进来了==)

然后进行build 时。会遇到Direct local .aar file dependencies are not supported when building an AAR......的问题。

怎么解决呢?

这时需要把module 的build.gradle中

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

改成

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

然后这样就能生成aar了。

但是把这个aar 放到写的demo 中进行运行调用时,又会报java.lang.NoClassDefFoundError的错误

这是因为打aar 时,并没有把libs 下的jar 和aar 给打进去。

这时我们可以借助fat-aar-android

具体使用的话也可以参照官方文档fat-aar-android

1.项目根build.gradle 中添加

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

2.module中build.gradle 中添加

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

3.dependencies中添加入需要被嵌入到aar 中的lib 和aar

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')
    ...
}

但是这时打包时又会报

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 就能在其它项目进行正常使用了。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值