问题描述

最近在重构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在编译时使编译器通过不报错,在主工程拷贝一份的文件在打包后能让程序正常调用。
解决这个问题花了不少时间,记录一下,希望能帮助到遇到同类问题的同仁。

GitHub 加速计划 / de / Dependencies
42
2
下载
A rewrite of the old legacy software "depends.exe" in C# for Windows devs to troubleshoot dll load dependencies issues.
最近提交(Master分支:4 个月前 )
1997a400 - 4 年前
2f423539 - 4 年前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐