Android Gradle 學(xué)習(xí)筆記整理

前言

Gradle 是將軟件編譯、測試、部署等步驟聯(lián)系在一起自動化構(gòu)建工具。
對于Android開發(fā)人員已經(jīng)了解build.gradle 的 android{} 和 dependencies{} ,但是他的編譯過程是什么樣的?這個過程中可以干些什么事了解嗎?
此文是學(xué)習(xí)Gradle時的學(xué)習(xí)筆記,讓你重新認識Gradle,讓Gradle加快并提效構(gòu)建你的項目。此時分享給大家,與大家共勉

此筆記主要內(nèi)容如下

  • Gradle 最基礎(chǔ)的一個項目配置

  • Groovy 基礎(chǔ)語法 并解釋 apply plugin: 'xxxx'和dependencies{}

  • Gradle Project/Task 并自定義Task和Plugin

  • 自定義一個重命名APP名的插件 流程

  • APT 技術(shù)- Java AbstractProcessor

  • Android 字節(jié)碼增強技術(shù) - Transform (Android 中使用字節(jié)碼增強技術(shù))

文章內(nèi)容略長,如果你已經(jīng)掌握Gradle基礎(chǔ)知識,可以直接通過目錄查看你想看的內(nèi)容,回顧或者學(xué)習(xí)都還不錯。

初識Gradle 項目構(gòu)建配置

gralde項目結(jié)構(gòu)

image

如圖所示,是一個比較小的gradle配置,這里主要說兩部分

  1. 綠色部分: gralde版本配置及gralde所需要的腳本,其中g(shù)radlew為linux/mac下的腳本,gradle.bat為windows下所需的腳本
  2. 紅色部分:settings.gradle 為根項目的項目配置,外層的build.gradle為根項目的配置,內(nèi)層的build.gradle為子項目的配置

gradle 配置順序

gralde的項目配置是先識別 settings.gradle,然后在配置各個build.gradle.
為了說明構(gòu)建執(zhí)行順序,在上述最基礎(chǔ)的gradle項目結(jié)構(gòu)里面設(shè)置了對應(yīng)的代碼

// settings.gradle
println "settings.gradle start"
include ':app'
println "settings.gradle end"
//root build.gradle
println "project.root start"
buildscript {
    repositories {
    }
    dependencies {
    }
}

allprojects {
}
println "project.root end"
//app build.gradle
println "project.app start"
project.afterEvaluate {
    println "project.app.afterEvaluate print"
}
project.beforeEvaluate {
    println "project.app.beforeEvaluate print"
}
println "project.app end"

如果是mac/linux,執(zhí)行./gradlew 得到如下結(jié)果:

settings.gradle start
settings.gradle end

> Configure project :
project.root start
project.root end

> Configure project :app
project.app start
project.app end
project.app.afterEvaluate print

Groovy 語法

下面講一些關(guān)于groovy的語法,可以打開Android Studio Tools-> Groovy Console練習(xí)Groovy 語法 ,如下

image

可選的類型定義,可以省略語句結(jié)束符分號(;)

int vs = 1
def version = 'version1'

println(vs)
println(version)

括號也是可選的

println vs
println version

字符串定義

def s1 = 'aaa'
def s2 = "version is ${version}"
def s3 = ''' str
is
many
'''
println s1
println s2
println s3

集合

def list = ['ant','maven']
list << "gradle"
list.add('test')
println list.size()
println list.toString()
//map
def years = ['key1':1000,"key2":2000]
println years.key1
println years.getClass()

輸出結(jié)果

[ant, maven, gradle, test]
1000
class java.util.LinkedHashMap

閉包

groovy語法中支持閉包語法,閉包簡單的說就是代碼塊,如下:

def v = {
    v -> println v
}
static def testMethod(Closure closure){
    closure('閉包 test')
}
testMethod v

其中定義的v就為閉包,testMethod 為一個方法,傳入?yún)?shù)為閉包,然后調(diào)用閉包.

解釋 apply plugin: 'xxxx'和 dependencies{}

準備工作,看gradle的源碼

我們先把子項目的build.gradle改為如下形式

apply plugin: 'java-library'
repositories {
    mavenLocal()
}
dependencies {
    compile gradleApi()
}

這樣,我們就可以直接看gradle的源碼了,在External Libraries里如下

image

解釋

進入build.gradle 點擊apply 會進入到gradle的源碼,可以看到

//PluginAware
 /**
     * Applies a plugin or script, using the given options provided as a map. Does nothing if the plugin has already been applied.
     * <p>
     * The given map is applied as a series of method calls to a newly created {@link ObjectConfigurationAction}.
     * That is, each key in the map is expected to be the name of a method {@link ObjectConfigurationAction} and the value to be compatible arguments to that method.
     *
     * <p>The following options are available:</p>
     *
     * <ul><li>{@code from}: A script to apply. Accepts any path supported by {@link org.gradle.api.Project#uri(Object)}.</li>
     *
     * <li>{@code plugin}: The id or implementation class of the plugin to apply.</li>
     *
     * <li>{@code to}: The target delegate object or objects. The default is this plugin aware object. Use this to configure objects other than this object.</li></ul>
     *
     * @param options the options to use to configure and {@link ObjectConfigurationAction} before “executing” it
     */
    void apply(Map<String, ?> options);

用Groovy 語法很清楚的解釋,apply其實就是一個方法,后面?zhèn)鬟f的就是一個map,其中plugin為key.

那么dependencies{}也是一樣

//Project
/**
     * <p>Configures the dependencies for this project.
     *
     * <p>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.
     *
     * <h3>Examples:</h3>
     * See docs for {@link DependencyHandler}
     *
     * @param configureClosure the closure to use to configure the dependencies.
     */
    void dependencies(Closure configureClosure);

dependencies是一個方法 后面?zhèn)鬟f的是一個閉包的參數(shù).

問題:思考那么android {}也是一樣的實現(xiàn)嗎? 后面講解

Gradle Project/Task

在前面章節(jié)中提到gralde初始化配置,是先解析并執(zhí)行setting.gradle,然后在解析執(zhí)行build.gradle,那么其實這些build.gradle 就是Project,外層的build.gradle是根Project,內(nèi)層的為子project,根project只能有一個,子project可以有多個.

我們知道了最基礎(chǔ)的gradle配置,那么怎么來使用Gradle里面的一些東西來為我們服務(wù)呢?

Plugin

前面提到apply plugin:'xxxx',這些plugin都是按照gradle規(guī)范來實現(xiàn)的,有java的有Android的,那么我們來實現(xiàn)一個自己的plugin.

把build.gradle 改為如下代碼

//app build.gradle
class LibPlugin implements Plugin<Project>{
    @Override
    void apply(Project target) {
        println 'this is lib plugin'
    }
}
apply plugin:LibPlugin

運行./gradlew 結(jié)果如下

> Configure project :app
this is lib plugin

Plugin 之Extension

我們在自定義的Plugin中要獲取Project的配置,可以通過Project去獲取一些基本配置信息,那我們要自定義的一些屬性怎么去配置獲取呢,這時就需要創(chuàng)建Extension了,把上述代碼改為如下形式。

//app build.gradle
class LibExtension{
    String version
    String message
}
class LibPlugin implements Plugin<Project>{
    @Override
    void apply(Project target) {
        println 'this is lib plugin'
        //創(chuàng)建 Extension 
        target.extensions.create('libConfig',LibExtension)
        //創(chuàng)建一個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'
}

配置完成后,執(zhí)行./gradlew libTask
得到如下結(jié)果

> Configure project :app
this is lib plugin
> Task :lib:libTask
1.0
lib message

看完上述代碼,我們就知道android {} 其實他就是一個Extension, 他是由plugin 'com.android.application'或者'com.android.library' 創(chuàng)建。

Task

上述代碼中,創(chuàng)建了一個名字為libTask的task,gradle中創(chuàng)建task的方式由很多中, 具體的創(chuàng)建接口在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;
<T extends Task> T create(String name, Class<T> type) throws InvalidUserDataException;
<T extends Task> T create(String name, Class<T> type, Action<? super T> configuration) throws InvalidUserDataException;

Project不可以執(zhí)行跑起來,那么我們就要定義一些task來完成我們的編譯,運行,打包等。com.android.application插件 為我們定義了打包task如assemble,我們剛才定義的插件為我們添加了一個libTask用于輸出。

image

Task API

我們看到創(chuàng)建的task里面可以直接調(diào)用doLast API,那是因為Task類中有doLast API,可以查看對應(yīng)的代碼看到對應(yīng)的API

image

Gradle的一些Task

gradle 為我們定義了一些常見的task,如clean,copy等,這些task可以直接使用name創(chuàng)建,如下:

task clean(type: Delete) {
    delete rootProject.buildDir
}

依賴task

我們知道Android打包時,會使用assemble相關(guān)的task,但是僅僅他是不能直接打包的,他會依賴其他的一些task.
那么怎么創(chuàng)建一個依賴的Task呢?代碼如下

task A{
    println "A task"
}
task B({
    println 'B task'
},dependsOn: A)

執(zhí)行./graldew B 輸出

A task
B task

自定義一個重命名APP名字的插件

通過上述的一些入門講解,大概知道了gradle是怎么構(gòu)建的,那現(xiàn)在來自定義一個安卓打包過程中,重命名APP名字的一個插件。

上述在build.gradle直接編寫Plugin是OK的,那么為了復(fù)用性更高一些,那我們怎么把這個抽出去呢?

如下

image

其中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{ //當(dāng)前項目可以發(fā)布到本地文件夾中
    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<Project>{

    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",ChangeAppNameConifg)
        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"
                        }
                }
        }
    }
}

定義完成后,執(zhí)行./gradlew uploadArchives 會在本目錄生成對應(yīng)對應(yīng)的插件

image

應(yīng)用插件
在根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 設(shè)置

apply plugin: 'apkname'
nameConfig{
    prefixName = 'demo'
    notConfig = 'debug'
}

Gradle doc 官網(wǎng)

Gradle的基礎(chǔ)API差不多就介紹完了。

官網(wǎng)地址:https://docs.gradle.org/current/userguide/userguide.html
可以去查看對應(yīng)的API,也可以直接通過源碼的方式查看

但是筆記還沒完,學(xué)習(xí)了Gradle的基礎(chǔ),我們要讓其為我們服務(wù)。下面介紹幾個實際應(yīng)用.

APT 技術(shù)

http://www.itdecent.cn/p/94aee6b02b2b
https://blog.csdn.net/kaifa1321/article/details/79683246

APT 全稱Annotation Processing Tool,編譯期解析注解,生成代碼的一種技術(shù)。常用一些IOC框架的實現(xiàn)原理都是它,著名的ButterKnife,Dagger2就是用此技術(shù)實現(xiàn)的,SpringBoot中一些注入也是使用他進行注入的.

在介紹APT之前,先介紹一下SPI (Service Provider Interface)它通過在ClassPath路徑下的META-INF/**文件夾查找文件,自動加載文件里所定義的類。
上面自定義的ApkNamePlugin 就是使用這種機制實現(xiàn)的,如下.

image

SPI 技術(shù)也有人用在了組件化的過程中進行解耦合。

要實現(xiàn)一個APT也是需要這種技術(shù)實現(xiàn),但是谷歌已經(jīng)把這個使用APT技術(shù)重新定義了一個,定義了一個auto-service,可以簡化實現(xiàn),下面就實現(xiàn)一個簡單Utils的文檔生成工具。

Utils文檔生成插件

我們知道,項目中的Utils可能會很多,每當(dāng)新人入職或者老員工也不能完成知道都有那些Utils了,可能會重復(fù)加入一些Utils,比如獲取屏幕的密度,框高有很多Utils.我們通過一個小插件來生成一個文檔,當(dāng)用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')
}

應(yīng)用一個Utils

@GDoc(name = "顏色工具類",time = "2019年09月18日19:58:07",author = "dingxx")
public final class ColorUtils {
}

最后生成的文檔如下:

名稱              功能            作者
ColorUtils      顏色工具類        dingxx

當(dāng)然最后生成的文檔可以由自己決定,也可以直接是html等.

Android Transform

在說Android Transform之前,先介紹Android的打包流程,在執(zhí)行task assemble時

image

在.class /jar/resources編譯的過程中,apply plugin: 'com.android.application' 這個插件支持定義一個回調(diào) (com.android.tools.build:gradle:2.xx 以上),類似攔截器,可以進行你自己的一些定義處理,這個被成為Android的Transform

那么這個時候,可以動態(tài)的修改這些class,完成我們自己想干的一些事,比如修復(fù)第三方庫的bug,自動埋點,給第三方庫添加函數(shù)執(zhí)行耗時,完成動態(tài)的AOP等等.

我們所知道的 ARoute就使用了這種技術(shù). 當(dāng)然他是先使用了APT先生成路由文件,然后通過Transform加載.

以下內(nèi)容引用自ARoute ReadMe

使用 Gradle 插件實現(xiàn)路由表的自動加載 (可選)

apply plugin: 'com.alibaba.arouter'
buildscript {
    repositories {
       jcenter()
    }

    dependencies {
        classpath "com.alibaba:arouter-register:?"
    }
}

可選使用,通過 ARouter 提供的注冊插件進行路由表的自動加載(power by AutoRegister), 默認通過掃描 dex 的方式 進行加載通過 gradle 插件進行自動注冊可以縮短初始化時間解決應(yīng)用加固導(dǎo)致無法直接訪問 dex 文件,初始化失敗的問題,需要注意的是,該插件必須搭配 api 1.3.0 以上版本使用!

看ARoute的LogisticsCenter 可以知道,init時,如果沒有使用trasnform的plugin,那么他將在注冊時,遍歷所有dex,查找ARoute引用的相關(guān)類,如下

//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<String> routerMap;

        // 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的實現(xiàn)簡介

通過以上,我們知道,回調(diào)的是.class文件或者jar文件,那么要處理.class 文件或者jar文件就需要字節(jié)碼處理的相關(guān)工具,常用字節(jié)碼處理的相關(guān)工具都有

  • ASM
  • Javassist
  • AspectJ

具體的詳細,可以查看美團的推文 Java字節(jié)碼增強探秘

怎么定義一個Trasnfrom內(nèi),回顧上面的gradle plugin實現(xiàn),看以下代碼

public class TransfromPlugin implements Plugin<Project> {

    @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;
        }
    }
}

結(jié)合字節(jié)碼增加技術(shù),就可以實現(xiàn)動態(tài)的一些AOP,由于篇幅原因,這里就不在詳細把筆記拿出來了,如果想進一步學(xué)習(xí),推薦ARoute作者的一個哥們寫的AutoRegister,可以看看源碼

總結(jié)

到這里Gradle的學(xué)習(xí)筆記基本整理完成了,由于作者水平有限,如果文中存在錯誤,還請指正,感謝.
也推薦閱讀我的另一篇文章,Android 修圖(換證件照背景,污點修復(fù))

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容