Android Gradle 學習筆記整理
-
Configures the dependencies for this project. * *
This method executes the given closure against the {@link DependencyHandler} for this project. The {@link * DependencyHandler} is passed to the closure as the closure's delegate. * *
Examples:
* See docs for {@link DependencyHandler} * * @param configureClosure the closure to use to configure the dependencies. */ void dependencies(Closure configureClosure);
dependencies是一個方法 後面傳遞的是一個閉包的引數. 問題:思考那麼android {}也是一樣的實現嗎? 後面講解 Gradle Project/Task =================== 在前面章節中提到gralde初始化配置,是先解析並執行setting.gradle,然後在解析執行build.gradle,那麼其實這些build.gradle 就是Project,外層的build.gradle是根Project,內層的為子project,根project只能有一個,子project可以有多個. 我們知道了最基礎的gradle配置,那麼怎麼來使用Gradle裡面的一些東西來為我們服務呢? Plugin ------ 前面提到apply plugin:'xxxx',這些plugin都是按照gradle規範來實現的,有java的有Android的,那麼我們來實現一個自己的plugin. 把build.gradle 改為如下程式碼
//app build.gradle
class LibPlugin implements Plugin
@Override
void apply(Project target) {
println 'this is lib plugin'
}
}
apply plugin:LibPlugin
執行./gradlew 結果如下
Configure project :app
this is lib plugin
### Plugin 之Extension 我們在自定義的Plugin中要獲取Project的配置,可以通過Project去獲取一些基本配置資訊,那我們要自定義的一些屬性怎麼去配置獲取呢,這時就需要建立Extension了,把上述程式碼改為如下形式。
//app build.gradle
class LibExtension{
String version
String message
}
class LibPlugin implements Plugin
@Override
void apply(Project target) {
println 'this is lib plugin'
//建立 Extension
target.extensions.create('libConfig',LibExtension)
//建立一個task
target.tasks.create('libTask',{
doLast{
LibExtension config = project.libConfig
println config.version
println config.message
}
})
}
}
apply plugin:LibPlugin
//配置
libConfig {
version = '1.0'
message = 'lib message'
}
配置完成後,執行./gradlew libTask 得到如下結果
Configure project :app
this is lib plugin
Task :lib:libTask
1.0
lib message
看完上述程式碼,我們就知道android {} 其實他就是一個Extension, 他是由plugin 'com.android.application'或者'com.android.library' 建立。
Task
----
上述程式碼中,建立了一個名字為libTask的task,gradle中建立task的方式由很多中, 具體的建立介面在TaskContainer類中
//TaskContainer
Task create(Map<String, ?> options) throws InvalidUserDataException;
Task create(Map<String, ?> options, Closure configureClosure) throws InvalidUserDataException;
Task create(String name, Closure configureClosure) throws InvalidUserDataException;
Task create(String name) throws InvalidUserDataException;
Project不可以執行跑起來,那麼我們就要定義一些task來完成我們的編譯,執行,打包等。com.android.application外掛 為我們定義了打包task如assemble,我們剛才定義的外掛為我們添加了一個libTask用於輸出。
![](https://user-gold-cdn.xitu.io/2019/9/17/16d3e77fc9cc60ff?imageView2/0/w/1280/h/960/ignore-error/1)
### Task API
我們看到建立的task裡面可以直接呼叫doLast API,那是因為Task類中有doLast API,可以檢視對應的程式碼看到對應的API
![](https://user-gold-cdn.xitu.io/2019/9/17/16d3e818c3dbb8c5?imageView2/0/w/1280/h/960/ignore-error/1)
### Gradle的一些Task
gradle 為我們定義了一些常見的task,如clean,copy等,這些task可以直接使用name建立,如下:
task clean(type: Delete) {
delete rootProject.buildDir
}
### 依賴task
我們知道Android打包時,會使用assemble相關的task,但是僅僅他是不能直接打包的,他會依賴其他的一些task. 那麼怎麼建立一個依賴的Task呢?程式碼如下
task A{
println "A task"
}
task B({
println 'B task'
},dependsOn: A)
執行./graldew B 輸出
A task
B task
自定義一個重新命名APP名字的外掛
================
通過上述的一些入門講解,大概知道了gradle是怎麼構建的,那現在來自定義一個安卓打包過程中,重新命名APP名字的一個外掛。
上述在build.gradle直接編寫Plugin是OK的,那麼為了複用性更高一些,那我們怎麼把這個抽出去呢?
如下
![](https://user-gold-cdn.xitu.io/2019/9/17/16d3e91b752658ef?imageView2/0/w/1280/h/960/ignore-error/1)
其中build.gradle為
apply plugin: 'groovy'
apply plugin: 'maven'
repositories {
mavenLocal()
jcenter()
}
dependencies {
compile gradleApi()
}
def versionName = "0.0.1"
group "com.ding.demo"
version versionName
uploadArchives{ //當前專案可以釋出到本地資料夾中
repositories {
mavenDeployer {
repository(url: uri('../repo')) //定義本地maven倉庫的地址
}
}
}
apkname.properties為
implementation-class=com.ding.demo.ApkChangeNamePlugin
ApkChangeNamePlugin
package com.ding.demo
import org.gradle.api.Project
import org.gradle.api.Plugin
class ApkChangeNamePlugin implements Plugin
static class ChangeAppNameConfig{
String prefixName
String notConfig
}
static def buildTime() {
return new Date().format("yyyy_MM_dd_HH_mm_ss", TimeZone.getTimeZone("GMT+8"))
}
@Override
void apply(Project project) {
if(!project.android){
throw new IllegalStateException('Must apply \'com.android.application\' or \'com.android.library\' first!');
}
project.getExtensions().create("nameConfig",ChangeAppNameConfig)
ChangeAppNameConfig config
project.afterEvaluate {
config = project.nameConfig
}
project.android.applicationVariants.all{
variant ->
variant.outputs.all {
output ->
if (output.outputFile != null && output.outputFile.name.endsWith('.apk')
&& !output.outputFile.name.contains(config.notConfig)) {
def appName = config.prefixName
def time = buildTime()
String name = output.baseName
name = name.replaceAll("-", "_")
outputFileName = "${appName}-${variant.versionCode}-${name}-${time}.apk"
}
}
}
}
}
定義完成後,執行./gradlew uploadArchives 會在本目錄生成對應對應的外掛
![](https://user-gold-cdn.xitu.io/2019/9/17/16d3e94decfb19ed?imageView2/0/w/1280/h/960/ignore-error/1)
應用外掛 在根build.gralde 配置
buildscript {
repositories {
maven {url uri('./repo/')}
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
classpath 'com.ding.demo:apkname:0.0.1'
}
}
在app.gralde 設定
apply plugin: 'apkname'
nameConfig{
prefixName = 'demo'
notConfig = 'debug'
}
Gradle doc 官網
=============
Gradle的基礎API差不多就介紹完了。
官網地址:[docs.gradle.org/current/use…](
)
可以去檢視對應的API,也可以直接通過原始碼的方式檢視
但是筆記還沒完,學習了Gradle的基礎,我們要讓其為我們服務。下面介紹幾個實際應用.
APT 技術
======
[www.jianshu.com/p/94aee6b02…](
)
[blog.csdn.net/kaifa1321/a…](
)
APT 全稱Annotation Processing Tool,編譯期解析註解,生成程式碼的一種技術。常用一些IOC框架的實現原理都是它,著名的ButterKnife,Dagger2就是用此技術實現的,SpringBoot中一些注入也是使用他進行注入的.
在介紹APT之前,先介紹一下SPI (Service Provider Interface)它通過在ClassPath路徑下的META-INF/\*\*資料夾查詢檔案,自動載入檔案裡所定義的類。 上面自定義的ApkNamePlugin 就是使用這種機制實現的,如下.
![](https://user-gold-cdn.xitu.io/2019/9/18/16d4232c54745794?imageView2/0/w/1280/h/960/ignore-error/1)
SPI 技術也有人用在了元件化的過程中進行解耦合。
要實現一個APT也是需要這種技術實現,但是谷歌已經把這個使用APT技術重新定義了一個,定義了一個auto-service,可以簡化實現,下面就實現一個簡單Utils的文件生成工具。
Utils文件生成外掛
-----------
我們知道,專案中的Utils可能會很多,每當新人入職或者老員工也不能完成知道都有那些Utils了,可能會重複加入一些Utils,比如獲取螢幕的密度,框高有很多Utils.我們通過一個小外掛來生成一個文件,當用Utils可以看一眼文件就很一目瞭然了.
### 新建一個名為DocAnnotation的Java Libary
定義一個註解
@Retention(RetentionPolicy.CLASS)
public @interface GDoc {
String name() default "";
String author() default "";
String time() default "";
}
### 新建一個名為DocComplie 的 Java Libary先
然後引入谷歌的 auto-service,引入DocAnnotation
apply plugin: 'java-library'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.auto.service:auto-service:1.0-rc2'
implementation 'com.alibaba:fastjson:1.2.34'
implementation project(':DocAnnotation')
}
定義一個Entity類
public class Entity {
public String author;
public String time;
public String name;
}
定義註解處理器
@AutoService(Processor.class) //其中這個註解就是 auto-service 提供的SPI功能
public class DocProcessor extends AbstractProcessor{
Writer docWriter;
@Override
public synchronized void init(ProcessingEnvironment processingEnvironment) {
super.init(processingEnvironment);
}
@Override
public Set<String> getSupportedAnnotationTypes() {
//可處理的註解的集合
HashSet<String> annotations = new HashSet<>();
String canonicalName = GDoc.class.getCanonicalName();
annotations.add(canonicalName);
return annotations;
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
Messager messager = processingEnv.getMessager();
Map<String,Entity> map = new HashMap<>();
StringBuilder stringBuilder = new StringBuilder();
for (Element e : env.getElementsAnnotatedWith(GDoc.class)) {
GDoc annotation = e.getAnnotation(GDoc.class);
Entity entity = new Entity();
entity.name = annotation.name();
entity.author = annotation.author();
entity.time = annotation.time();
map.put(e.getSimpleName().toString(),entity);
stringBuilder.append(e.getSimpleName()).append(" ").append(entity.name).append("\n");
}
try {
docWriter = processingEnv.getFiler().createResource(
StandardLocation.SOURCE_OUTPUT,
"",
"DescClassDoc.json"
).openWriter();
//docWriter.append(JSON.toJSONString(map, SerializerFeature.PrettyFormat));
docWriter.append(stringBuilder.toString());
docWriter.flush();
docWriter.close();
} catch (IOException e) {
//e.printStackTrace();
//寫入失敗
}
return true;
}
}
### 專案中引用
dependencies {
implementation project(':DocAnnotation')
annotationProcessor project(':DocComplie')
}
應用一個Utils
@GDoc(name = "顏色工具類",time = "2019年09月18日19:58:07",author = "dingxx")
public final class ColorUtils {
}
最後生成的文件如下:
名稱 功能 作者
ColorUtils 顏色工具類 dingxx
當然最後生成的文件可以由自己決定,也可以直接是html等.
Android Transform
=================
在說Android Transform之前,先介紹Android的打包流程,在執行task assemble時
![](https://user-gold-cdn.xitu.io/2019/9/18/16d424831e279ae3?imageView2/0/w/1280/h/960/ignore-error/1)
在.class /jar/resources編譯的過程中,apply plugin: 'com.android.application' 這個外掛支援定義一個回撥 (com.android.tools.build:gradle:2.xx 以上),類似攔截器,可以進行你自己的一些定義處理,這個被成為Android的Transform
那麼這個時候,可以動態的修改這些class,完成我們自己想幹的一些事,比如修復第三方庫的bug,自動埋點,給第三方庫新增函式執行耗時,完成動態的AOP等等.
我們所知道的 ARoute就使用了這種技術. 當然他是先使用了APT先生成路由檔案,然後通過Transform載入.
以下內容引用自ARoute ReadMe
> > 使用 Gradle 外掛實現路由表的自動載入 (可選)
apply plugin: 'com.alibaba.arouter'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "com.alibaba:arouter-register:?"
}
}
> > 可選使用,通過 ARouter 提供的註冊外掛進行路由表的自動載入(power by AutoRegister), 預設通過掃描 dex 的方式 進行載入通過 gradle 外掛進行自動註冊可以縮短初始化時間解決應用加固導致無法直接訪問 dex 檔案,初始化失敗的問題,需要注意的是,該外掛必須搭配 api 1.3.0 以上版本使用!
看ARoute的LogisticsCenter 可以知道,init時,如果沒有使用trasnform的plugin,那麼他將在註冊時,遍歷所有dex,查詢ARoute引用的相關類,如下
//LogisticsCenter
public synchronized static void init(Context context, ThreadPoolExecutor tpe) throws HandlerException {
if (registerByPlugin) {
logger.info(TAG, "Load router map by arouter-auto-register plugin.");
} else {
Set
// It will rebuild router map every times when debuggable.
if (ARouter.debuggable() || PackageUtils.isNewVersion(context)) {
logger.info(TAG, "Run with debug mode or new install, rebuild router map.");
// These class was generated by arouter-compiler.
routerMap = ClassUtils.getFileNameByPackageName(mContext, ROUTE_ROOT_PAKCAGE);
if (!routerMap.isEmpty()) {
context.getSharedPreferences(AROUTER_SP_CACHE_KEY, Context.MODE_PRIVATE).edit().putStringSet(AROUTER_SP_KEY_MAP,routerMap).apply();
}
PackageUtils.updateVersion(context); // Save new version name when router map update finishes.
} else {
logger.info(TAG, "Load router map from cache.");
routerMap = new HashSet<>(context.getSharedPreferences(AROUTER_SP_CACHE_KEY, Context.MODE_PRIVATE).getStringSet(AROUTER_SP_KEY_MAP, new HashSet<String>()));
}
}
....
}
Android Transform的實現簡介
----------------------
通過以上,我們知道,回撥的是.class檔案或者jar檔案,那麼要處理.class 檔案或者jar檔案就需要位元組碼處理的相關工具,常用位元組碼處理的相關工具都有
* ASM
* Javassist
* AspectJ
具體的詳細,可以檢視美團的推文 [Java位元組碼增強探祕](
)
怎麼定義一個Trasnfrom內,回顧上面的gradle plugin實現,看以下程式碼
public class TransfromPlugin implements Plugin
@Override
public void apply(Project project) {
AppExtension appExtension = (AppExtension) project.getProperties().get("android");
appExtension.registerTransform(new DemoTransform(), Collections.EMPTY_LIST);
}
class DemoTransform extends Transform{
@Override
public String getName() {
return null;
}
@Override
public Set<QualifiedContent.ContentType> getInputTypes() {
return null;
}
@Override
public Set<? super QualifiedContent.Scope> getScopes() {
return null;
}
@Override
public boolean isIncremental() {
return false;
}
}
}
結合位元組碼增加技術,就可以實現動態的一些AOP,由於篇幅原因,這裡就不在詳細把筆記拿出來了,如果想進一步學習,推薦ARoute作者的一個哥們寫的[AutoRegister](
),可以看看原始碼