Android Studio引用AAR的方式,分为gradle7.0之前与7.0之后
一、集成步骤
方法一:
1.将对应的xxx.aar包复制到项目的libs目录下(xxx代表需要引入的aar包名称)
2.然后在模块的build.gradle文件中配置implementation files('libs/library.aar')
dependencies {
implementation files('libs/library.aar')
}
方法二:
1. 单独新建文件夹 LocalRepo,在其内部新建staticip,新建build.gradle
2. build.gradle内容由于我引入的是yzwlnfcreadcard.aar,记得换成自己的aar包名称
configurations.maybeCreate("default")
artifacts.add("default", file("yzwlnfcreadcard.aar"))
3.引入的是yzwlnfcreadcard.aar,所以也需要把包复制进来
二、gradle7.0之前
1.将 aar 包放入 library module 的 libs 目录下,然后在工程的 build.gradle文件中加入如下代码:
flatDir { dirs 'libs', '../moduleName/libs' }
注:将 moduleName 替换为你自己的 library module 的 name.
2.在 module 的 build.gradle 文件中添加依赖:
dependencies {
implementation(name: 'aar包名', ext: 'aar')
}
三、常见报错
集成完成,代码引用也没问题,但是一运行就报如下错误:
Execution failed for task ':app:mergeDebugJavaResource'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.MergeJavaResWorkAction
> 2 files found with path 'org/bouncycastle/x509/CertPathReviewerMessages_de.properties' from inputs:
- E:\project\ZhongNanXiaoyaDev\LocalRepo\staticip\build\.transforms\bdb601d81fa95af2bf0d3b2b3de9e234\transformed\jetified-yzwlnfcreadcard\jars\classes.jar
- C:\Users\Administrator\.gradle\caches\transforms-3\5c9ef0b491c1ad4933dd3ce258a1758f\transformed\jetified-bcprov-jdk15on-1.59.jar
Adding a packagingOptions block may help, please refer to
https://developer.android.com/reference/tools/gradle-api/7.2/com/android/build/api/dsl/ResourcesPackagingOptions
for more information
* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
那是因为包冲突导致,按照第四步解决方案进行解决
四、解决方案
在app目录下的build.gradle添加配置:
android {
packagingOptions {
pickFirst 'org/bouncycastle/x509/CertPathReviewerMessages_de.properties'
pickFirst 'org/bouncycastle/x509/CertPathReviewerMessages.properties'
}
}