Android 打包aar包含第三方aar 解决方案

Android 打包aar包含第三方aar
因项目需要,打包aar包含第三方aar,如果直接对module进行打包会产生一些问题。

* What went wrong:
Direct local .aar file dependencies are not supported when building an AAR. The resulting AAR would be broken because the classes and Android resources from any local .aar file dependencies would not be packaged in the resulting AAR. Previous versions of the Android Gradle Plugin produce broken AARs in this case too (despite not throwing this error). The following direct local .aar file dependencies of the :httpLibrary project caused this error: D:\AndroidWorkSpace\mackSdk\mackSDK\httpLibrary\libs\xxxxx.aar

错误信息说的很清楚构建aar不支持本地aar文件依赖

解决方案:
fat-aar 能将依赖项合并并嵌入到生成的aar文件中。
fat-aar项目地址:https://github.com/adwiv/android-fat-aar

由于fat-aar不再维护,使用起来有诸多需要修改的地方,而不支持高版本的gradle,极其坑爹,踩坑后找到替代方案,支持高版本的gradle无需修改脚本文件
fat-aar-android:https://github.com/kezong/fat-aar-android

1.在需要打包成aar的module的build.gradle中加入如下代码

在dependencies中以如下方式依赖第三方aar

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


在这里插入图片描述

在dependencies中以如下方式依赖第三方aar

2.在项目根目录的build.gradle中添加如下代码

classpath 'com.github.kezong:fat-aar:1.3.8'

3.第三方库需要把依赖implementation改为embed,例如:

implementation('com.squareup.okhttp3:okhttp:4.11.0')
改为:
embed('com.squareup.okhttp3:okhttp:4.11.0')

4.本地的aar引入,例如:

implementation files("libs/xxx.aar")
改为:
embed(name: 'animplayer', ext: 'aar')
//需要加上这个
compileOnly fileTree(dir: 'libs', include: ['*.jar', '*.aar'])

打出来的aar lib中存在多个架构,例如x86\x86_64

我们不需要这些so库的话可以过滤掉
在需要打包的模块build中加入要过滤的so

android{
		//不需要把这些架构打进去
        packagingOptions {
            exclude 'lib/x86/*.so'
            exclude 'lib/x86_64/*.so'
        }
}

5.运行module 下task assembleRelease 打包

最终生成的aar在module下的build中

参考:https://blog.csdn.net/Mr_ziheng/article/details/131010454

猜你喜欢

转载自blog.csdn.net/yzwfeng/article/details/134599067
今日推荐