错误信息:
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 :moudleA project caused this error:/..../..../test.aar
翻译:
构建aar时不支持直接的本地.aar文件依赖关系。生成的AAR将被破坏,因为来自任何本地.AAR文件依赖项的类和Android资源都不会打包在生成的AAR中。以前版本的Android Gradle插件在这种情况下也会产生损坏的AAR(尽管没有抛出此错误)。moudleA 项目的以下直接本地.aar文件依赖项导致了此错误:test.aar
场景:
1.moduleA模块依赖test.aar,如果其他模块或者APP不需要依赖test.aar,修改依赖方式即可:implementation或者api换成compileOnly
2.Android项目app依赖moduleA模块,moudleA依赖test.aar,并且APP也要同时依赖test.aar,本想直接使用api,依赖传递就OK,现在Rebuild progect直接报错。
解决:
使用文件夹方式(新建一个模块)目录结构如下:
1.新建testOne文件夹,test.aar放在该文件夹下,
同时新建build.gradle文件用于配置:
configurations.maybeCreate("default")
artifacts.add("default", file('test.aar'))
2.moduleA添加依赖testOne;
dependencies {
api project(':testOne')
}
3.app 依赖moduleA;
dependencies {
implementation project(':moudleA')
}
4.在settings.gradle 添加moduleA和testOne
include ':app'
rootProject.name = "My Application"
include ':moudleA'//模块A
include ':testOne'//存放test.aar的文件夹
拓展:
问:
多个aar的情况怎么解决呢?直接在testOne模块下添加就可以吗?
答:
不是的,需要新建一个模块testMultip,再新建二级文件夹模块(testA和testB),分别用于存放aar,以及新建各自的build.gradle!
1,多层级文件夹testMultip如图(testA 文件夹下存放test.aar;testB文件夹下存放testB.aar)
testA的build.gradle:
configurations.maybeCreate("default")
artifacts.add("default", file('test.aar'))
testB的 build.gradle:
configurations.maybeCreate("default")
artifacts.add("default", file('testB.aar'))
testMultiple下的build.gradle:
configurations.maybeCreate("default")
artifacts.add("default", file('testA'))
artifacts.add("default", file('testB'))
2.moduleA添加依赖testMultip,以及二级目录的testA和testB;
dependencies {
api project(':testMultiple')
api project(':testMultiple:testA')
api project(':testMultiple:testB')
}
3.app 依赖moduleA(同上不变)
dependencies {
implementation project(':moudleA')
}
4.在settings.gradle 添加moduleA和testMultip,以及二级目录的testA和testB;
include ':app'
rootProject.name = "My Application"
include ':moudleA'//模块A
include ':testMultiple'//文件夹第一层级
include ':testMultiple:testA'//文件夹第二层级,存放test.aar
include ':testMultiple:testB'//文件夹第二层级,存放testB.aar
以上就是多个aar嵌套依赖步骤。