Skip to content

Commit

Permalink
Feature: make this plugin configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
cpdroid committed Aug 20, 2019
1 parent 4be5bdb commit df9b8eb
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 58 deletions.
75 changes: 65 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@

A plugin to merge dependencies(aar/jar) into aar file.

This plugin can embed dependency(local or remote) to an aar file, when an app module includes the aar file, the aar's dependencies code and resource
This plugin can embed dependency(local or remote) to aar file, when an app module includes the aar file, the aar's dependencies code and resource
can be referenced directly, no need to import once again.

**This plugin has been tested on android gradle plugin 3.0.0-3.4.2 and gradle 4.6-5.1.1.**
**This plugin support android gradle plugin 3.0.0-3.4.2 and gradle 4.6-5.1.1.**

## Usage
Add snippet below to your **root project's *build.gradle*** file:
Add snippet below to your ***root project's build.gradle*** file:
```gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.cpdroid:fat-aar:1.0.2'
classpath 'com.cpdroid:fat-aar:1.1.0'
}
}
```

Add snippet below to you **library module's *build.gradle*** file:
Add snippet below to you ***library module's build.gradle*** file:
```gradle
apply plugin: 'com.android.library'
apply plugin: "com.cpdroid.fat-aar"
```

**make sure 'com.cpdroid.fat-aar' is appied after 'com.android.library'**
***make sure 'com.cpdroid.fat-aar' is appied after 'com.android.library'***

Then you can use keyword *"embedded"* instead of *"implementation"* or *"compile"* to package target dependency to your generated aar file:
```gradle
Expand All @@ -41,15 +41,70 @@ embedded project(':moduleA')
embedded 'com.gongwen:marqueelibrary:1.1.3'
```

Run gradle assemble task, normally *"assembleRelease"*, copy the generated aar file to your app module's libs directory

Include the generated aar file in **app module's *build.gradle*** file:
Run gradle assemble task, normally *"assembleRelease"*, copy the generated aar file to your app module's libs directory and include the generated aar file in ***app module's build.gradle*** file:
```gradle
implementation fileTree(dir: 'libs', include: ['*.aar'])
```
Then you can reference the aar and it's dependencies code and resources.

[An example can be found here](https://github.com/cpdroid/fat-aar-sample)

## Configuration
Add *fataar* closure in ***library module's build.gradle*** file:
```gradle
apply plugin: 'com.android.library'
apply plugin: 'com.cpdroid.fat-aar'
android {
compileSdkVersion 28
...
}
fataar {
verboseLog true
ignoreAndroidSupport true
ignoreDependencies 'com.google.code.gson:gson:2.8.2'
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
...
}
```
*fataar* closure is optional, and is in the same level with *android* and *dependencies*. Below is the detailed configuration:
* ***verboseLog***: Default vaule is *false*, set to *true* to show more log
* ***ignoreAndroidSupport***: Whether ignore android support package automatically, default is *true*, *false* means won't ignore android support package
* ***ignoreDependencies***: Determine which dependency won't be embedded into aar, we can use it as follows:
+ match the ***'group:name:version'*** pattern
```gradle
ignoreDependencies 'com.google.code.gson:gson:2.8.2'
```
+ only match the ***'name'*** pattern(part between two colon):
```gradle
ignoreDependencies 'gson'
```
+ regex match:
```gradle
ignoreDependencies '^com.android.*'
```
+ you can add more than one *ignoreDependencies*, and it accept more than one patten:
```gradle
fataar {
ignoreDependencies 'com.blankj:utilcode:1.23.7', 'gson' //accept more than one pattern,split by ','
ignoreDependencies '^com.android.*' //add the second ignoreDependencies
}
```
Any dependency matched the three pattern above will be ignored, and won't be packaged into aar file.

Set ***verboseLog*** to *true* to see more log like this:
```bash
> Task :mylibrary:decompressFreeReleaseDependencies
pattern:[com.google.code.gson:gson:2.8.2], artifact:[com.google.code.gson:gson:2.8.2]
Ignore matched dependency: [com.google.code.gson:gson:2.8.2]
```
*pattern* is your configuration, *artifact* is the dependency, if it matched the log will show
*'Ignore matched dependency: [com.google.code.gson:gson:2.8.2]'*

## Features or Issues
* All android support packages and android jars will be ignored automatically, you must import them in your app module.
The detailed ignored package can be found in the log.
Expand All @@ -67,6 +122,6 @@ The detailed ignored package can be found in the log.
If you have referenced a module's *app_name*, define an *app_name* in your library's *values.xml* file.
```bash
> Task :moduleA:addReleaseSourceSets
Found value [app_name] in [D:\workspace\fat-aar-sample\moduleA\build\aar_plugin\exploded_aar\com.gongwen\marqueelibrary\1.1.3\res\values\values.xml]
Found value [app_name] in [D:\workspace\fat-aar-sample\moduleA\build\fat-aar\exploded_aar\com.gongwen\marqueelibrary\1.1.3\res\values\values.xml]
Delete 1 values...
```
81 changes: 68 additions & 13 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
[![Android Gradle Plugin 3.0.0-3.4.2](https://img.shields.io/badge/android--gradle-3.0.0--3.4.2-blue)](https://developer.android.com/studio/releases/gradle-plugin#updating-gradle)
[![Gradle 4.6-5.1.1](https://img.shields.io/badge/gradle-4.6--5.1.1-blue)](https://developer.android.com/studio/releases/gradle-plugin#updating-gradle)

Fat-aar是一个将依赖库 *(aar/jar)* 打包进 *aar* 文件的插件.
Fat-aar是一个能将依赖库 *(aar/jar)* 打包进 *aar* 文件的插件.

它能将远程或者本地依赖嵌入 *aar* 文件,当 *app* 模块使用这个 *aar* 文件时,*aar* 依赖的代码跟资源可以直接被引用到,而无需在 *app* 模块里面重新引入一遍.
它能将远程或本地依赖打包进 *aar* 文件,当 *app* 模块使用这个 *aar* 文件时,*aar* 依赖的代码跟资源可以直接被引用到,而无需在 *app* 模块里面重新引入一遍.

本插件已经在 *Android gradle plugin* 3.0.0-3.4.2 及 *Gradle* 4.6-5.1.1 版本上测试通过
本插件支持 *Android gradle plugin* 3.0.0-3.4.2 及 *Gradle* 4.6-5.1.1 版本

## 用法
**根目录的 *build.gradle*** 里面添加如下语句:
Expand All @@ -18,7 +18,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.cpdroid:fat-aar:1.0.2'
classpath 'com.cpdroid:fat-aar:1.1.0'
}
}
```
Expand All @@ -31,24 +31,80 @@ apply plugin: "com.cpdroid.fat-aar"

**确保 'com.cpdroid.fat-aar' 在'com.android.library'后面**

用关键字 *"embedded"* 替代 *"implementation"* 或者 *"compile"* 就可以把依赖的库打包进你的 *aar* 文件:
用关键字 *"embedded"* 替代 *"implementation"* 或者 *"compile"* 就可以把依赖库打包进生成的 *aar* 文件:
```gradle
embedded fileTree(dir: 'libs', include: ['*.aar'])
embedded project(':moduleA')
embedded 'com.gongwen:marqueelibrary:1.1.3'
```

运行 *gradle* 任务, 一般来说是 *"assembleRelease"*, 把生成的 *aar* 文件复制进 *app* 模块的 *libs* 目录

***app* 模块的 *build.gradle*** 里面引入生成的 *aar* 文件:
运行 *gradle* 任务, 一般来说是 *"assembleRelease"*, 把生成的 *aar* 文件复制进 *app* 模块的 *libs* 目录,然后在***app* 模块的 *build.gradle*** 里面引入生成的 *aar* 文件:
```gradle
implementation fileTree(dir: 'libs', include: ['*.aar'])
```
之后就可以在 *app* 模块里面使用 *aar* 及其依赖库的代码与资源了.

[这有个例子](https://github.com/cpdroid/fat-aar-sample)

## 配置
***library* 模块的 *build.gradle*** 里添加 *fataar* 可以进行自定义配置:
```gradle
apply plugin: 'com.android.library'
apply plugin: 'com.cpdroid.fat-aar'
android {
compileSdkVersion 28
...
}
fataar {
verboseLog true
ignoreAndroidSupport true
ignoreDependencies 'com.google.code.gson:gson:2.8.2'
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
...
}
```
注意 *fataar* 配置是可选的,不配置插件也可以正常运行, *fataar**android**dependencies* 是平级的,各项配置说明如下:
* ***verboseLog*** 默认为 *false* ,改为 *true* 可以显示更多日志
* ***ignoreAndroidSupport*** 是否自动忽略 *android support* 包,默认为 *true*,改为 *false* 则不会自动忽略 *android support*
* ***ignoreDependencies*** 配置哪些依赖不用打包进 *aar*,用法如下:
+ 完全匹配 *'group:name:version'* 的格式:
```gradle
ignoreDependencies 'com.google.code.gson:gson:2.8.2'
```
+ 只匹配 *'name'* 部分(两个冒号中间的部分):
```gradle
ignoreDependencies 'gson'
```
+ 正则匹配:
```gradle
ignoreDependencies '^com.android.*'
```
+ 可以同时添加多个 *ignoreDependencies* 配置,也可以在 *ignoreDependencies* 后面添加多个参数:
```gradle
fataar {
ignoreDependencies 'com.blankj:utilcode:1.23.7', 'gson' //接收多个参数,以','分隔
ignoreDependencies '^com.android.*' //添加第二条配置
}
```
*ignoreDependencies* 的配置是叠加的不是覆盖的,像上面的配置只要依赖库匹配到三条中的任意一条都不会被打包进 *aar*.

如果想看具体匹配到哪些依赖,可以将 *verboseLog* 设置为 *true*,运行时可以看到如下 *log*:
```bash
> Task :mylibrary:decompressFreeReleaseDependencies
pattern:[com.google.code.gson:gson:2.8.2], artifact:[com.google.code.gson:gson:2.8.2]
Ignore matched dependency: [com.google.code.gson:gson:2.8.2]
```
*pattern* 是配置的规则,*artifact* 是正在检查的依赖库,如果匹配成功,就会提示
*'Ignore matched dependency: [com.google.code.gson:gson:2.8.2]'*

## 特性或问题
* 本插件会自动忽略 *android support* 包和 *android* 开头的 *jar* 包,在 *app* 模块使用的时候必须重新导入,
* 本插件会自动忽略 *android support* 包和 *android**jar* 包,在 *app* 模块使用的时候必须重新导入,
可以将 *ignoreAndroidSupport* 设置为 *false* 以禁止自动忽略 *android support* 包和 *android**jar* 包.
具体有哪些被忽略了可以看日志:
```bash
> Task :moduleA:decompressReleaseDependencies
Expand All @@ -60,12 +116,11 @@ implementation fileTree(dir: 'libs', include: ['*.aar'])
Ignore android jar: [android.arch.core:common]
```


* 依赖库的资源里的 *"app_name"* 字段会被自动删除,因为很多依赖库都有这一字段,编译时会冲突导致编译失败。
如果你想用这个字段请在 *library**values.xml* 里重新定义一个
如果想用这个字段请在 *library**values.xml* 里重新定义一个
```bash
> Task :moduleA:addReleaseSourceSets
Found value [app_name] in [D:\workspace\fat-aar-sample\moduleA\build\aar_plugin\exploded_aar\com.gongwen\marqueelibrary\1.1.3\res\values\values.xml]
Found value [app_name] in [D:\workspace\fat-aar-sample\moduleA\build\fat-aar\exploded_aar\com.gongwen\marqueelibrary\1.1.3\res\values\values.xml]
Delete 1 values...
```


2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ apply plugin: 'java'
apply plugin: 'groovy'

ext {
versionId = '1.0.2'
versionId = '1.1.0'
groupId = 'com.cpdroid'
name = 'fat-aar'
website = 'https://github.com/cpdroid/fat-aar'
Expand Down
22 changes: 22 additions & 0 deletions src/main/groovy/com/cpdroid/fat_aar/FatAarExtension.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.cpdroid.fat_aar

import groovy.transform.CompileStatic

@CompileStatic
class FatAarExtension {
boolean verboseLog = false
boolean ignoreAndroidSupport = true
private List<String> ignoreDependencies = new ArrayList<>()

void ignoreDependencies(String... dependency) {
Utils.logLevel2(dependency)
for (String dep : dependency) {
if (!ignoreDependencies.contains(dep))
ignoreDependencies.add(dep)
}
}

List<String> getIgnoreDependencies() {
return ignoreDependencies
}
}
Loading

0 comments on commit df9b8eb

Please sign in to comment.