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)系统 ...
随机推荐
- Spring+DBUnit+H2----项目单元测试
http://yugouai.iteye.com/blog/1879337 今天够郁闷的,早上调好的代码,到中午调试不同了,分析不出问题,H2的JDBC报错:org.h2.jdbc.JdbcSQLEx ...
- 开源 免费 java CMS - FreeCMS1.5-数据对象-job
下载地址:http://code.google.com/p/freecms/ job 从FreeCMS 1.5 开始支持 在使用职位相关标签时,标签会封装job供页面调用. 属性 说明 id id s ...
- wepy - 转换成h5
包地址:http://npm.taobao.org/package/wepy-web 1. npm 安装 npm install wepy-web 2.yarn 按照 yarn add wepy-we ...
- 使用ADS1.2的注意事项及常用技巧
如果创建的项目中有多个文件时(尤其是编译后的镜像大小超过4K时),一定要在link order栏下调整文件顺序,主要是前几个文件的顺序(2440init.s.2440slib.s.nand.c这三个文 ...
- [转]自定义Drawable实现灵动的红鲤鱼动画(下篇)
小鱼儿 上篇文章自定义Drawable实现灵动的红鲤鱼动画(上篇)我们绘制了可以摆动身体的小鱼,本篇就分享一下如何让小鱼游到手指点击的位置.用到的主要技术如下: 1).三阶贝塞尔曲线 2).Pat ...
- 与AQS有关的并发类
ReetrantLock与Condition: 參考 在java.util.concurrent包中.有两个非常特殊的工具类.Condition和ReentrantLock,使用过的人都知道,Reen ...
- 如何更改删除window服务?
Cmd 下输入 sc delete 服务名 想要看服务名直接cmd下输入services.msc 打开服务, 例如删除apache服务 命令:sc delete Apache2.4
- 除掉inline-block 间距
1.现象: <!doctype html> <html lang="en"> <head> <meta charset="UTF ...
- vs2010支持html5+css3
第一步. 先到微软官方下载一个 Microsoft Visual Studio 2010 sp1 . 给传送门:下载 下载到这个东东 ---
- 自定义AppSession
TelnetSession.cs public class TelnetSession:AppSession<TelnetSession> { protected override voi ...