android自訂gradle外掛程式之當前項目使用

來源:互聯網
上載者:User

標籤:href   meta   cto   github   定義   tps   sources   res   end   

1、建立一個Java library 項目命名為BuildSrc,刪除build.gradle裡面的內容,添加下面內容

apply plugin: ‘groovy‘dependencies {     compile gradleApi()//gradle sdk    compile localGroovy()//groovy sdk}

2、刪除src/main下所有目錄檔案

  • 在main 目錄下建立groovy目錄,建立包名com.home.plugin,建立MyPlugin.groovy 內容如下
package com.home.pluginimport org.gradle.api.Pluginimport org.gradle.api.Projectpublic class MyPlugin implements Plugin<Project> {    void apply(Project project) {        System.out.println("========================");        System.out.println("這是個外掛程式!");        System.out.println("========================");    }}
  • 在main目錄下建立resources目錄然後在resources目錄裡面再建立META-INF目錄
    再在META-INF裡面建立gradle-plugins目錄。最後在gradle-plugins目錄裡面建立properties檔案
    注意這個檔案的命名,你可以隨意取名,但是後面使用這個外掛程式的時候,會用到這個名字
    比如,你取名為com.home.plugin.properties,而在其他build.gradle檔案中使用自訂的外掛程式時候則需寫成:apply plugin: ‘com.home.plugin‘
    com.home.plugin.properties檔案內容為:implementation-class=com.home.plugin.MyPlugin

  • 工程目錄結構如下:

3、在app下的build.gradle添加引用 apply plugin: ‘com.home.plugin‘

Demo源碼:https://github.com/xuguohongai/android/tree/master/GradlePlugin

參考串連:http://kvh.io/cn/tags/EmbraceAndroidStudio/

android自訂gradle外掛程式之當前項目使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.