解决:Direct local .aar file dependencies are not supported when building an AAR.

问题描述

最近在重构Android旧项目时,把相关功能独立成一个个的library库,然后app module或其他模块添加依赖,这时遇到一个问题:独立的这个library中依赖了本地的第三方提供的aar文件,然后打包debug版本apk能正常运行,而打包release版本时一直报错。(但是,如果是主项目module,如app module直接依赖本地aar是不会报错的)

此时我的项目Android Studio版本及gradle版本如下:
Android Studio: Arctic Fox | 2020.3.1
Gradle:

classpath "com.android.tools.build:gradle:7.0.0"

我项目配置如下
截图1
此时编译release版本时于是出现了如下错误:
截图2
具体错误如下:

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

解决步骤

1.将library模块中依赖方式 改成compileOnly fileTree(…)

代码如下:

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

截图3

2.将主工程AAR依赖方式改成implementation fileTree(…)

代码如下(示例):

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

截图4

3.将library中libs下aar文件复制一份放入主工程的libs下

截图5
然后再重写编译,至此问题解决。


总结

这种依赖问题应该是由于环境版本变化导致的,根据错误描述说以前的这种直接依赖方式会破坏aar文件,高版本后不再支持,我们这种解决方式是让library在编译时使编译器通过不报错,在主工程拷贝一份的文件在打包后能让程序正常调用。
解决这个问题花了不少时间,记录一下,希望能帮助到遇到同类问题的同仁。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>