Android Gradle 引用本地 AAR 的几种方式
折衷方案:
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..<it] : file.name };
compile(name: name, ext: 'aar')
//在内部类外时,可用以下等价形式.
//dependencies.add("compile", [name: name, ext: 'aar'])
}
}
batchImportAar()
//================================================================================
// 批量导入 libs 里的所有 .jar 类库.
//================================================================================
compile fileTree(dir: 'libs', include: ['*.jar'])
//================================================================================
// 正确引用自定义类库
//================================================================================
def compileLibrary = { name ->
//鉴于某些类库同样引用了.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'}
优点√:
- 自己类库可以自己维护自己内部的AAR引用.
- 能像维护libs里的jar类库一样简单.
- dependencies 设置方式和在线解析引用的方式一样.
缺点×:
- 同方式2的缺点
- dependencies 设置时需要放在 compile fileTree 的上面,否则无法识别.
- dependencies 设置的名字 和 在线解析引用的方式不一样.如
在线解析方式:compile 'com.android.support:support-v4:23.+@aar'
本地AAR方式:compile 'android.support:v4:23.+@aar'
如何设置正确的本地AAR名称?
- 解压AAR包,看AndroidManifest.xml里的 package="android.support.v4"
- 对应的就是名称就是 android.support:v4
- 然后必须设置AAR文件名为:v4-1.0.0.aar
- 最后拼接正确的版本号就是 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')}
优点√:
- 自己类库可以自己维护自己内部的AAR引用.
- 能像维护libs里的jar类库一样简单.
缺点×:
- 在类库(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')}
优点√:
- 可以像引用自己类库一样.
- 可以供多个库、项目引用此AAR类库.
缺点×:
- 需要像自己类库一样去维护.
参考资料:
官网解释:当同一个JAR,AAR类库需要多个地方同时引用时的折衷配置方案.(其实就是方式1)
Handling transitive dependencies for local artifacts (jars and aar)
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'))
Android Gradle 引用本地 AAR 的几种方式的更多相关文章
- 在Android Studio添加本地aar包引用
1.如何在Android Studio添加本地aar包引用 https://jingyan.baidu.com/article/2a13832890d08f074a134ff0.html 2.完成上述 ...
- Android 查看项目依赖树的四种方式
Android 查看项目依赖树的四种方式: 方式一: ./gradlew 模块名:dependencies //查看单独模块的依赖 ./gradlew :app:dependencies --conf ...
- ios网络学习------4 UIWebView的加载本地数据的三种方式
ios网络学习------4 UIWebView的加载本地数据的三种方式 分类: IOS2014-06-27 12:56 959人阅读 评论(0) 收藏 举报 UIWebView是IOS内置的浏览器, ...
- Android跟蓝牙耳机建立连接有两种方式
Android 跟蓝牙耳机建立连接有两种方式. 1. Android 主动跟蓝牙耳机连BluetoothSettings 中和蓝牙耳机配对上之后, BluetoothHeadsetService 会收 ...
- OpenCV4Android释疑: 透析Android以JNI调OpenCV的三种方式(让OpenCVManager永不困扰)
OpenCV4Android释疑: 透析Android以JNI调OpenCV的三种方式(让OpenCVManager永不困扰) 前文曾详细探讨了关于OpenCV的使用,原本以为天下已太平.但不断有人反 ...
- HTML 引用Css样式的四种方式
不才,只知道HTML引用CSS样式有四种方式,内部引用和外部引用各两种,因为老是忘记细节,记下了随时翻阅亦可方便如我般的初学者 内部引用方式1: 直接在标签内用 style 引用,如: <div ...
- [转载]-win7启动本地MongoDB的四种方式
2016年04月07日 09:52:34 cherry__cheng 阅读数:19451 标签: win7启动本地MongoDB的四种方式快速启动本地mongodb 更多 个人分类: mongodb& ...
- [Android Studio] 使用本地 aar 文件
导出aar 首先Android Library项目的gradle脚本只需要在开头声明 apply plugin: 'com.android.library' 之后就和导出apk文件一样的方法,执行 . ...
- Android中访问sdcard路径的几种方式
以前的Android(4.1之前的版本)中,SDcard路径通过"/sdcard"或者"/mnt/sdcard"来表示,而在JellyBean(安卓4.1)系统 ...
随机推荐
- ECharts演习(一)
前几天小组讨论,窗外的麻雀在电线杆上多嘴,想想很有夏天的感觉,手中的铅笔在纸上来了又回,我用几行字形容孰是孰非......... Echarts使用指南 百度网站:http://echarts.bai ...
- (字符串)最长公共子序列(Longest-Common-Subsequence,LCS)
问题: 最长公共子序列就是寻找两个给定序列的子序列,该子序列在两个序列中以相同的顺序出现,但是不必要是连续的. 例如序列X=ABCBDAB,Y=BDCABA.序列BCA是X和Y的一个公共子序列,但是不 ...
- QQ2010如何开启透明效果皮肤
QQ2010可在WIN7下实现皮肤透明效果. 腾讯已于近日发布了QQ2010的BETA版本,经笔者试验,可在WIN7下实现皮肤透明化效果. 设置如下: 1.先打开QQ皮肤控制面板,如下: 2.然后任选 ...
- Opencv2.4.9安装和在visualstudio 2013中配置
Opencv2.4.9安装和在visualstudio 2013中配置 下载opencv和在windows下安装: 最新版本号的opencv是2014.4.25的opencv2.4.9,这里选择当前最 ...
- AVL树的实现例程
/* AVL树的节点声明 */ #ifndef _AVLTREE_H #define _AVLTREE_H struct AvlNode; typedef struct AvlNode *Positi ...
- html5调用摄像头实现拍照
技术时刻都在前进着.我们的需求也是时刻在改变着.最近在开发中遇到了用户进行账号注册时需要个人图像,网站提供自动拍照功能.还有在登录了PC之后,手机端进行登录时只需要扫描一下PC上的二维码就可以登录.这 ...
- 【Android开发经验】Cannot generate texture from bitmap异常的解决方式
异常现象: 今天在处理用户头像的过程中,由于头像的处理比較复杂,由于,没有使用afinal自带的自己主动载入.而是自己依据头像的下载路径.手动进行下载和使用.可是在手动回收bitmap对象的过程中,会 ...
- EXCEPTION-STRUTS2
CreateTime--2016年8月29日17:05:50Author:Marydon 声明:异常类文章主要是记录了我遇到的异常信息及解决方案,解决方案大部分都是百度解决的,(这里只是针对我遇到 ...
- Eclipse常用且不易记快捷键
大小写转换:CTRL+SHIFT+X,Y 复制行:CTRL+ALT+↑,↓(部分无法使用) 查看继承关系:CTRL+T 直接查看系统源码:CTRL+SHIFT+T 查看所有快捷键:CTRL+SHIFT ...
- apachebench的简单使用
apachebench的简单使用 2013-03-08 15:48:47 分类: LINUX ApacheBench是 Apache 附带的一个小工具,专门用于 HTTP Server 的benchm ...