引入aar的冲突无所不在,经过远程依赖maven的包能够经过exclude
关键字搭配module
和group
去除某个组,没办法去除具体的类。html
那么若是是单独的aar包,想要排除aar下classes.jar包里的某个单独的包或者类怎么办?java
须要接入的jar包已经带了腾讯X5核心,当前依赖的已经包含X5核心,冲突又该如何解决呢?git
当前的gradle脚本(项目连接:https://github.com/luohongxfb... )能够解决。github
如excludelib/libs/exampleAAR.aar,左边是未去除的包结构,右边是去除com.baidu
以后的:maven
如excludelib/libs/exampleJAR.jar:gradle
(1)将须要排除的aar或者jar包放在excludelib/libs下。ui
(2)更改excludelib/build.gradlethis
//须要排除的aar或者jar。(替换成须要排除的) artifacts.add("exclude", file('libs/exampleAAR.aar')) artifacts.add("exclude", file('libs/exampleJAR.jar'))
(3)设置排除规则 若是您须要排除aar,那么请更改excludelib/excludeAar.gradle;若是您须要排除jar,那么请更改excludelib/excludeJar.gradle
//须要排除的包名 def excludePackages = ['com.baidu'] //须要排除的类(须要全类名) def excludeClasses = []
(4)运行排除任务
排除后生成的aar在excludelib/build/excludeaar下,排除后生成的jar位于excludelib/build/excludejar。
而后就能够愉快的使用啦~
aar排除步骤:
一、获取到须要排除的原始AAR包
二、解压AAR包(zipTree配合Task Copy)
三、解压AAR包中的class.jar(zipTree配合Task Copy)
四、按照排除规则对解压的class.jar从新打包(Task Jar)
五、从新打包成AAR包(Task Zip)
jar排除步骤
一、获取到须要排除的原始jar包
二、解压jar包(zipTree配合Task Copy)
三、按照排除规则对解压的jar从新打包(Task Jar)
task unZipAar(type: Copy) { def zipFile = getDefaultAar() def outputDir = unZipAarFile from zipTree(zipFile) into outputDir }
主要原理:zipTree配合Copy,实现解压。
Copy Task官方讲解:https://docs.gradle.org/curre...
ziptree源码主要解析:建立一个新的file tree包含原来zip的内容,能够配合Copy实现解压。
public interface Project{ /** * <p>Creates a new {@code FileTree} which contains the contents of the given ZIP file. * You can combine this method with the {@link #copy(groovy.lang.Closure)} * method to unzip a ZIP file * @param zipPath The ZIP file. Evaluated as per {@link #file(Object)}. * @return the file tree. Never returns null. */ FileTree zipTree(Object zipPath); }
task zipJar(type: Jar) { baseName = 'classes' from unZipJarFile destinationDir unZipAarFile exclude getExcludePackageRegex(excludePackages) exclude getExcludeClassRegex(excludeClasses) }
这个步骤就是把以前解压的classes.jar文件,按照排除规则用Task Jar从新打包成jar文件。
Task Jar官方讲解:https://docs.gradle.org/curre...
Property/Method | Description |
---|---|
baseName | 压缩后的jar文件名。 |
from(sourcePaths) | 须要压缩的目录。 |
destinationDir | 压缩后存放的目录。 |
exclude(excludes) | 须要排除的文件。 |
task excludeAar(type: Zip) { group 'ex' description '生成一个排除以后的aar包' baseName excludeAarName extension "aar" from unZipAarFile destinationDir excludeAarFile }
对classes.jar处理完成的aar重打包,主要用到Task Zip。
Task Zip官方讲解:https://docs.gradle.org/curre...
Property/Method | Description |
---|---|
group | setGroup(String group) 将当前的Task设置到指定组。 |
description | setDescription(@Nullable String description) Task描述。 |
baseName | 压缩后的aar文件名。 |
extension | 压缩后的文件扩展名。 |
from(sourcePaths) | 须要压缩的目录。 |
destinationDir | 压缩后存放的目录。 |