在这几天的工作中,我需要对我司的aar和jar 再做一次封装,生成一个aar给客户使用。于是就新建了一个module,里面放置aar和jar,以及一些调用接口的封装。
![](https://i-blog.csdnimg.cn/blog_migrate/32f11c6bdbb89507586e7a35ec4eda2d.png)
(项目里的aar和jar 有点多,没做甄别,一股脑全粘进来了==)
然后进行build 时。会遇到Direct local .aar file dependencies are not supported when building an AAR......的问题。
![](https://i-blog.csdnimg.cn/blog_migrate/ef071a1697fc48d7b6ca303ea9c44040.png)
怎么解决呢?
这时需要把module 的build.gradle中
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
改成
compileOnly fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
然后这样就能生成aar了。
![](https://i-blog.csdnimg.cn/blog_migrate/0abae1552ee1bc7cd6413b79a6bdc0d4.png)
但是把这个aar 放到写的demo 中进行运行调用时,又会报java.lang.NoClassDefFoundError的错误
![](https://i-blog.csdnimg.cn/blog_migrate/a90757858141d03c4720f1a5ea2e789c.png)
这是因为打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'
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/3f9ae5cc938d2fd0690ebc20f08818c4.png)
然后就能正常生成aar了。
但是假如libs中的aar或者jar 需要.so 文件,在生成aar时,记得也要把so 文件拷贝到存放aar和jar的module 中,
![](https://i-blog.csdnimg.cn/blog_migrate/fa702c59b3653f9fdf6522f20cbc5f8f.png)
并且需要配置
ndk {
// Specifies the ABI configurations of your native
// libraries Gradle should build and package with your APK.
abiFilters 'armeabi-v7a'
}
![](https://i-blog.csdnimg.cn/blog_migrate/57fffc14deccd6769d9a9308583596b5.png)
不然,在别处调用时,会报so 库的问题。
这样的话,生成的aar 就能在其它项目进行正常使用了。