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)系统 ...
随机推荐
- C#.NET常见问题(FAQ)-无法直接启动带有类库输出类型的项目怎么办
我把Driver.cs文件去掉了一行注释,发现报错 右击这个解决方案,选择属性,然后再启动项目中改成MySample 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http:// ...
- android google map v1 v2 v3 参考
V1,V2已经不被推荐使用,谷歌强烈推荐使用V3. 本人在选择时着实纠结了良久,现在总结如下: 对于V1,现在已经申请不到API KEY了,所以不要使用这个版本.这个是网址:https://devel ...
- Struts2(一)
一.Struts下载地址 http://struts.apache.org/download.cgi 二.导入包.配置Web.xml和struts.xml 在下载的包中从示例中找到一些包就可以 str ...
- C和C++静态检查规范
- sql server 2008分页
SELECT id, name, staffopenid, imageurl, content, ordernum, praisenum, createdate, lable, label2, man ...
- ORACLE-SQL(一)
迁移时间:2017年6月1日10:02:43 CreateTime--2017年6月1日09:59:30Author:Marydon 一.SQL语句 (一)基础篇 1.1.1 where 子句 1 ...
- map()3
# -*- coding: utf-8 -*- #python 27 #xiaodeng #map()3 ''' map(...) map(function, sequence[, sequence, ...
- 利用cURL会话获取一个网页
1.curl_init 作用: 初始化一个新的会话.返回一个cURL句柄,供curl_setopt(), curl_exec()和curl_close() 函数使用. 格式: curl_ ...
- Android Listview 隐藏滚动条
在<ListView>标签中设置属性. android:fastScrollEnabled="false" 以下属性scrollbars可以设置为none也可以不设置为 ...
- 设置sqlplus访问远程oracle数据库的最快方法
设置sqlplus访问远程oracle数据库的最快方法 时间:2010-01-21 10:57来源: 作者: 点击: 2次 设置sqlplus访问远程oracle数据库的最快方法,如果要连接远程数据库 ...