博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android Gradle 引用本地 AAR 的几种方式
阅读量:6709 次
发布时间:2019-06-25

本文共 3608 字,大约阅读时间需要 12 分钟。

折衷方案:

1.方式2 - 不完美解决办法2    2.再使用"自定义Gradle代码"来减轻重复设置的问题.

自定义Gradle代码如下:

repositories {    flatDir {        dirs 'libs'    }}dependencies {    //================================================================================    // 批量导入 libs 里的所有 .aar 类库.    //================================================================================    def batchImportAar = {        fileTree(dir: 'libs', include: '*.aar').each { File file ->            def name = file.name.lastIndexOf('.').with { it != -1 ? file.name[0..
//鉴于某些类库同样引用了.aar类库,所以必须把所在路径也添加到当前项目的flatDir //否则会出现无法正确解析识别其引用的 .aar 类库. repositories.flatDir.dirs(project(name).file('libs')) //因为Studio本身的缺陷,导致无法正确处理 debug,release,导致引用的类库无法区分 //是否为调试模式.所以需要同时添加两条配置,并结合类库自身的相关设置,方能解决. debugCompile project(path: name, configuration: 'debug') releaseCompile project(path: name, configuration: 'release') } compileLibrary(':EasyAndroid') compileLibrary(':DataSync_Android') compileLibrary(':EasyAndroid_QrCodeScan') compileLibrary(':EasyAndroid_Printer') compileLibrary(':EasyAndroid_OpenCamera') compileLibrary(':EasyAndroid_GifImageView') compileLibrary(':EasyAndroid_Downloader')}

方式3:

1. 把 AAR 放入 libs    2. 在 build.gradle 添加 repositories{flatDir{dirs 'libs'}}    3. 在 build.gradle 添加 dependencies{compile '包名:类库名:版本号@aar'}

优点√:

  1. 自己类库可以自己维护自己内部的AAR引用.
  2. 能像维护libs里的jar类库一样简单.
  3. dependencies 设置方式和在线解析引用的方式一样.

缺点×:

  1. 同方式2的缺点
  2. dependencies 设置时需要放在 compile fileTree 的上面,否则无法识别.
  3. dependencies 设置的名字 和 在线解析引用的方式不一样.如
    在线解析方式:compile 'com.android.support:support-v4:23.+@aar'
    本地AAR方式:compile 'android.support:v4:23.+@aar'

如何设置正确的本地AAR名称?

  1. 解压AAR包,看AndroidManifest.xml里的 package="android.support.v4"
  2. 对应的就是名称就是 android.support:v4
  3. 然后必须设置AAR文件名为:v4-1.0.0.aar
  4. 最后拼接正确的版本号就是 android.support:v4:1.0.0

方式2:

1. 把 AAR 放入 libs    2. 在 build.gradle 添加 repositories{flatDir{dirs 'libs'}}    3. 在 build.gradle 添加 dependencies{compile(name:'nameOfYourAARFile', ext:'aar')}

优点√:

  1. 自己类库可以自己维护自己内部的AAR引用.
  2. 能像维护libs里的jar类库一样简单.

缺点×:

  1. 在类库(library)中引用AAR库,在别的类库(library)或项目(application)中引用该类库时无法解析识别其引用的AAR库.

此问题可通过以下方法不太完美的解决掉:

1.复制一份引用的AAR库文件到类库、项目的libs中,同时设置 flatDir.

(意味着有多少地方引用该类库,就要复制多少份AAR,都要设置 flatDir)
2.在所有引用该类库的 build.gradle 中 设置 flatDir 为以下代码即可.
repositories {
flatDir {
dirs 'libs', project(':类库名').file('libs')
}
}
(意味着有所有引用该类库的地方的 build.gradle,都要设置一遍 flatDir)

方式1:

1. File -> New Module -> Import .JAR/.AAR    2. import the .AAR.    3. 在 build.gradle 添加 dependencies{compile project(':Name-Of-Your-Module-aar')}

优点√:

  1. 可以像引用自己类库一样.
  2. 可以供多个库、项目引用此AAR类库.

缺点×:

  1. 需要像自己类库一样去维护.

参考资料:

官网解释:当同一个JAR,AAR类库需要多个地方同时引用时的折衷配置方案.(其实就是方式1)

If you have a local jar or aar library that you want to use in more than one project, you cannot just reference it directly as a local dependency. This is because the android plugin will complain if it finds the same jar file twice when dexing the project and all its dependencies. (Note that right now you can't actually use a local aar file even if you only reference it once).

One way to fix this is to deploy the artifact in a repository. While it's possible, it might not be convenient due to the overhead of managing such a repository.

Another option is to create a new Gradle sub-project, and to make this project's published artifact be the jar or aar file that you want to reuse. Then you can simply have other Gradle sub-projects depend on this new sub-project.

In this new sub-project, simply create a build.gradle with the following:

configurations.create("default") artifacts.add("default", file('somelib.jar'))

转载于:https://www.cnblogs.com/AsionTang/p/5974254.html

你可能感兴趣的文章
block的使用
查看>>
使用Toolbar自定义布局的时候左边右边总有一点空间无法使用
查看>>
Photoshop 常用快捷键
查看>>
外观模式
查看>>
Extjs 4 grid修改某一行style
查看>>
background-position设置无效问题解决
查看>>
对称加密算法-DES
查看>>
Android BroadcastReceiver
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
jar包版本冲突问题
查看>>
物联网世界常见传输方式简介(思维导图)
查看>>
chm文档出现乱码解决办法
查看>>
Spark SQL示例
查看>>
我的友情链接
查看>>
高负载下tokudb环境下sync_binlog 不同设置的影响
查看>>
KSM导致的警告“ ksmtuned .... read-only system ” 的一些说明
查看>>
Objective C中数组排序的三种方法
查看>>
dedecms验证自定义表单不为空
查看>>
用户测评 | EDAS Serverless 上手体验
查看>>