從零開始寫gradle插件

一墨辛。在as中新建module如下

image.png
apply plugin: 'groovy'
apply plugin: 'maven'

dependencies {
    //gradle sdk
    compile gradleApi()
    //groovy sdk
    compile localGroovy()
    compile "com.android.tools.build:gradle:$localBuildToolsVersion"
    compile 'com.android.tools.build:transform-api:1.5.0'
    compile 'org.aspectj:aspectjrt:1.8.10'

}


repositories {
    mavenCentral()
}
//以上都為固定寫法

//group和version
group='com.col.decypt_fixer'
version='1.0.0'

//打包到本地或者遠(yuǎn)程Maven庫
uploadArchives {
    repositories {
        mavenDeployer {
            //提交到遠(yuǎn)程服務(wù)器:
            // repository(url: "http://www.xxx.com/repos") {
            //    authentication(userName: "admin", password: "admin")
            // }
            //本地的Maven地址設(shè)置為E:/Maven
            repository(url: "file:../aop-tech/repositories")
        }
    }
}

com.col.decypt_fixer.properties里面的
implementation-class=com.col.decypt_fixer.LibChanger能跳轉(zhuǎn)才說明結(jié)構(gòu)正確。

插件作用悔据,在transformNativeLibsWithStripDebugSymbolForApiTestRelease之后智蝠,packageApiTestRelease之前睛榄,替換.so庫馅闽。

Task :app:transformNativeLibsWithStripDebugSymbolForApiTestRelease
Task :app:validateSigningApiTestRelease
Task :app:packageApiTestRelease

class LibChanger implements Plugin<Project> {


    @Override
    void apply(Project project) {
        println("Plugin Start_________________________________________________________________")

        if (project.plugins.hasPlugin(AppPlugin)) {
            AppExtension android = project.extensions.getByType(AppExtension)
//            android.registerTransform(new LibChangerTransform(project))

            //創(chuàng)建一個(gè)Extension丧慈,名字叫做testCreatJavaConfig 里面可配置的屬性參照MyPlguinTestClass
            project.extensions.create("libChanger", MyPluginTestClass)

            android.applicationVariants.all { variant ->   //設(shè)置task依賴于生成BuildConfig的task,然后在生成BuildConfig后生成我們的類

                //獲取到scope,作用域
                def variantData = variant.variantData
                def scope = variantData.scope

                //拿到build.gradle中創(chuàng)建的Extension的值
                def config = project.extensions.getByName("libChanger")

                println("Plugin Start______${variantData}______________${scope}_____________________________________________")
                if (!variantData.toString().contains("ApiTestRelease")) return
//                println("Plugin Start______${variant.getVariantData().getTaskName("transform", "release")}______________________________________________________")


                //創(chuàng)建一個(gè)task
                def createTaskName = scope.getTaskName("col", "LibChangerPlugin")
                def createTask = project.task(createTaskName)

                File debugSoFile = null
                String outPath = null

                def oriTask = project.tasks.getByName("transformNativeLibsWithStripDebugSymbolForApiTestRelease")
                println("Plugin Start______${oriTask}______________________________________________________")
//                println("Plugin Start______${oriTask.inputs}_________________________${oriTask.outputs}_____________________________")

                oriTask.inputs.each { input ->
                    println("Plugin Start______input: ${input.properties}______________________________________________________")
                }
                oriTask.inputs.files.each { file ->
                    println("Plugin Start______input: ${file.absolutePath}______________________________________________________")
                    if (!file.absolutePath.endsWith("armeabi-v7a" + File.separator + "libjni-lib.so")) return

                    debugSoFile = file
                }
                oriTask.outputs.files.each { file ->
                    println("Plugin Start______output: ${file.absolutePath}______________________________________________________")
                    outPath = file.absolutePath

                }

                //設(shè)置task要執(zhí)行的任務(wù)
                createTask.doLast {
                    //生成java類
                    println("設(shè)置task要執(zhí)行的任務(wù)_____________________________________________")

                    def lastIndexOf = outPath.indexOf("reader_sh_as")
                    println("lastIndexOf---------------------------------------- " + lastIndexOf)
                    if (lastIndexOf == -1) return

                    def rootPath = outPath.substring(0, lastIndexOf + "project_name".length() + 1)
                    println("rootPath---------------------------------------- " + rootPath)
                    debugSoFile = new File(rootPath + File.separator
                            + "jni_lib" + File.separator
                            + "libjni-lib_debug.so"
                    )

                    if (outPath == null || outPath.length() == 0) return
                    if (debugSoFile == null) return

                    def releaseSoFile = new File(outPath + File.separator
                            + "0" + File.separator
                            + "lib" + File.separator
                            + "armeabi-v7a" + File.separator
                            + "libjni-lib.so")

                    println("directoryInput---------------------------------------- " + debugSoFile.exists() + " " + releaseSoFile.exists())
                    if (!debugSoFile.exists() || !releaseSoFile.exists()) return
                    println(debugSoFile.absolutePath + " -------------------\n--------------------- " + releaseSoFile.absolutePath)
                    println(" ---------------------------------------- " + debugSoFile.exists() + " " + releaseSoFile.exists())
                    println(" ---------------------------------------- " + FileUtils.checksumCRC32(debugSoFile) + " " + FileUtils.checksumCRC32(releaseSoFile))

                    FileUtils.copyFile(debugSoFile, releaseSoFile)
                    println(" ---------------------------------------- " + FileUtils.checksumCRC32(debugSoFile) + " " + FileUtils.checksumCRC32(releaseSoFile))

                }

                if (oriTask) {
                    createTask.dependsOn oriTask
                    oriTask.finalizedBy createTask
                }
            }
        }

    }

}

class MyPluginTestClass {
    def str = "默認(rèn)值";
}

二兴泥。運(yùn)行uploadArchives的gradle任務(wù)工育,會(huì)在配置的目錄生成


image.png

三。引用
在project的根目錄build.gradle引入

buildscript {
 dependencies {
classpath 'com.col.decypt_fixer:decypt_fixer:1.0.0'

在需要調(diào)用插件的地方

apply plugin: 'com.android.application'
apply plugin: 'com.col.decypt_fixer'

四搓彻。編譯時(shí)查看日志如绸。
如果plugin的代碼有問題嘱朽,編譯生成plugin時(shí)不會(huì)報(bào)錯(cuò),宿主運(yùn)行時(shí)會(huì)報(bào)錯(cuò)怔接。
要先注釋掉apply plugin: 'com.col.decypt_fixer'再修改運(yùn)行uploadArchives

五搪泳。Transform插件
Transform API 是在1.5.0-beta1版開始使用,利用Transform API扼脐,第三方的插件可以在.class文件轉(zhuǎn)為dex文件之前岸军,對一些.class 文件進(jìn)行處理。Transform API 簡化了這個(gè)處理過程瓦侮,而且使用起來很靈活艰赞。

注意getScopes和getReferencedScopes的區(qū)別

參考:
https://github.com/HujiangTechnology/gradle_plugin_android_aspectjx
http://www.reibang.com/p/811b0d0975ef

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市肚吏,隨后出現(xiàn)的幾起案子方妖,更是在濱河造成了極大的恐慌,老刑警劉巖罚攀,帶你破解...
    沈念sama閱讀 212,599評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件党觅,死亡現(xiàn)場離奇詭異,居然都是意外死亡斋泄,警方通過查閱死者的電腦和手機(jī)仔役,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,629評論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來是己,“玉大人又兵,你說我怎么就攤上這事∽浞希” “怎么了沛厨?”我有些...
    開封第一講書人閱讀 158,084評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長摔认。 經(jīng)常有香客問我逆皮,道長,這世上最難降的妖魔是什么参袱? 我笑而不...
    開封第一講書人閱讀 56,708評論 1 284
  • 正文 為了忘掉前任电谣,我火速辦了婚禮,結(jié)果婚禮上抹蚀,老公的妹妹穿的比我還像新娘剿牺。我一直安慰自己,他們只是感情好环壤,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,813評論 6 386
  • 文/花漫 我一把揭開白布晒来。 她就那樣靜靜地躺著,像睡著了一般郑现。 火紅的嫁衣襯著肌膚如雪湃崩。 梳的紋絲不亂的頭發(fā)上荧降,一...
    開封第一講書人閱讀 50,021評論 1 291
  • 那天,我揣著相機(jī)與錄音攒读,去河邊找鬼朵诫。 笑死,一個(gè)胖子當(dāng)著我的面吹牛薄扁,可吹牛的內(nèi)容都是我干的剪返。 我是一名探鬼主播,決...
    沈念sama閱讀 39,120評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼泌辫,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了九默?” 一聲冷哼從身側(cè)響起震放,我...
    開封第一講書人閱讀 37,866評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎驼修,沒想到半個(gè)月后殿遂,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,308評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡乙各,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,633評論 2 327
  • 正文 我和宋清朗相戀三年墨礁,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片耳峦。...
    茶點(diǎn)故事閱讀 38,768評論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡恩静,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蹲坷,到底是詐尸還是另有隱情驶乾,我是刑警寧澤,帶...
    沈念sama閱讀 34,461評論 4 333
  • 正文 年R本政府宣布循签,位于F島的核電站级乐,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏县匠。R本人自食惡果不足惜风科,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,094評論 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望乞旦。 院中可真熱鬧贼穆,春花似錦、人聲如沸兰粉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,850評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽亲桦。三九已至崖蜜,卻和暖如春浊仆,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背豫领。 一陣腳步聲響...
    開封第一講書人閱讀 32,082評論 1 267
  • 我被黑心中介騙來泰國打工抡柿, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人等恐。 一個(gè)月前我還...
    沈念sama閱讀 46,571評論 2 362
  • 正文 我出身青樓洲劣,卻偏偏與公主長得像,于是被迫代替她去往敵國和親课蔬。 傳聞我的和親對象是個(gè)殘疾皇子囱稽,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,666評論 2 350

推薦閱讀更多精彩內(nèi)容

  • 在 Android Studio 構(gòu)建的項(xiàng)目中,基于 Gradle 進(jìn)行項(xiàng)目的構(gòu)建二跋,同時(shí)使用 Android DS...
    Ant_way閱讀 7,334評論 0 16
  • 一战惊、插件相關(guān)API PluginAware主要定義了插件相關(guān)API。 應(yīng)用插件 apply plugin: 'ja...
    小村醫(yī)閱讀 1,573評論 0 2
  • 自定義插件涉及到幾個(gè)知識(shí)點(diǎn)扎即,比如Gradle構(gòu)建工具吞获、Groovy語法、Gradle插件開發(fā)流程等等谚鄙。這些知識(shí)我就...
    nailperry閱讀 6,492評論 10 37
  • 前言 準(zhǔn)備對微信Tinker進(jìn)行學(xué)習(xí)各拷,而微信Tinker里很重要的一部分是DexDiff算法,并且封裝了一個(gè)插件來...
    徐正峰閱讀 3,580評論 0 9
  • 廣寒殿前堆玉闷营,瑤臺(tái)廊下灑珠烤黍。皚皚下云端,爭耐北風(fēng)吹去傻盟。且住蚊荣,且住,留待梅仙指路莫杈。
    朱古力先生2021閱讀 606評論 0 1