Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 為Android Studio編寫自定義Gradle插件的教程

為Android Studio編寫自定義Gradle插件的教程

編輯:關於Android編程

Google已經建議Android開發全部轉向Android Studio開發,Android Studio 是使用gradle編譯、打包的,那麼問題來了,gradle可是有一堆東西...,為了徹底了解gradle,今天就來學習下如何寫自己的gradle插件(當然插件源碼是使用groovy寫的),先看如下代碼目錄:

2016224145421168.png (326×201)

如上圖所示,plugin目錄是插件源碼目錄,sample是用來測試插件的。


1、在目錄plugin/src/main/groovy/com/micky/gradle/下新建插件類MyCustomPlugin.groovy

package com.micky.gradle; 
 
import org.gradle.api.*; 
 
class MyCustomPlugin implements Plugin<Project> { 
  void apply(Project project) { 
    project.task('myTask') << { 
      println "Hi this is micky's plugin" 
    } 
  } 
} 

看看Plugin的源碼,其實就是一接口

public interface Plugin<T> { 
  /** 
   * Apply this plugin to the given target object. 
   * 
   * @param target The target object 
   */ 
  void apply(T target); 
} 


2、在目錄plugin/src/main/resources/META-INF/gradle-plugins/下創建文件com.micky.mycustom.properties用來指定插件實現類

implementation-class=com.micky.gradle.MyCustomPlugin 

 

   特別注意下:文件名“com.micky.mycustom”即是以後我們在使用插件時的apply plugin 'java' 的java,這裡我也是折騰了半天才得出的結果,坑啊。


3、一般情況下,我們還需要指定插件項目名稱,在plugin目錄下新建settings.gradle

rootProject.name='gradle-micky' 

 

4、萬事具備,就差編譯了,編譯需要在plugin目錄下新建build.gradle

apply plugin: 'groovy' 
apply plugin: 'maven' 
 
dependencies { 
  compile gradleApi() 
  compile localGroovy() 
} 
 
repositories { 
  mavenCentral() 
} 
 
group='com.micky' 
version='1.0.0' 
 
uploadArchives { 
  repositories { 
    mavenDeployer { 
      repository(url: uri('../repo')) 
    } 
  } 
} 

在這個腳本裡使用groovy插件編譯groovy源碼,聲明gradleAPI作為即時編譯依賴,apply plugin: 'maven' 是用來創建一個插件jar文件並且存儲在本地maven庫裡,本地maven庫即我們在腳本裡創建的"../repo"目錄
執行命令:  

gradle uploadArchives


2016224145611491.png (559×192)

5、以上4個步驟已經編譯插件並上傳到了本地庫中,接下來就看看怎麼使用插件,在sample目錄下,新建build.gradle

buildscript { 
  repositories { 
    maven { 
      url uri('../repo') 
    } 
  } 
 
  dependencies { 
    classpath group: 'com.micky', 
      name: 'gradle-micky', 
      version: '1.0.0' 
  } 
} 
 
apply plugin: 'com.micky.mycustom' 

6、執行命令

2016224145636115.png (507×113)

myTask即我們在MyCustomPlugin.groovy代碼中創建的任務。

7、自定義Task

(1)copy一份改名CustomPluginTask

在plugin\src\main\groovy\com\micky\gradle目錄創建源文件 MyCustomTask.groovy

package com.micky.gradle; 
 
import org.gradle.api.DefaultTask 
import org.gradle.api.tasks.TaskAction 
 
class MyCustomTask extends DefaultTask { 
  @TaskAction 
  void output() { 
    println "Hello this is my custom task output" 
  } 
} 

(2)修改MyCustomPlugin.groovy

package com.micky.gradle; 
 
import org.gradle.api.*; 
 
class MyCustomPlugin implements Plugin<Project> { 
  void apply(Project project) { 
    project.task('customTask', type: MyCustomTask) 
  } 
} 

(3)修改plugin目錄下的build.gradle,修改版本號

apply plugin: 'groovy' 
apply plugin: 'maven' 
 
dependencies { 
  compile gradleApi() 
  compile localGroovy() 
} 
 
repositories { 
  mavenCentral() 
} 
 
group='com.micky' 
version='1.0.1' 
 
uploadArchives { 
  repositories { 
    mavenDeployer { 
      repository(url: uri('../repo')) 
    } 
  } 
} 

執行gradle uploadArchives 編譯插件包
        
(4)sample目錄下的build.gradle

buildscript { 
  repositories { 
    maven { 
      url uri('../repo') 
    } 
  } 
 
  dependencies { 
    classpath group: 'com.micky', 
      name: 'gradle-micky', 
      version: '1.0.1' 
  } 
} 
apply plugin: 'com.micky.mycustom'  

執行gradle customTask 結果如下:

2016224145747922.png (563×125)


8、向Plugin Task 傳遞參數
(1)拷貝一份上面的代碼,改名為CustomPluginTaskWithParam,修改plugin\src\main\groovy\com\micky\gradle\MyCustomPlugin.groovy

package com.micky.gradle; 
 
import org.gradle.api.*; 
 
class MyCustomPluginExtension { 
  def message = "From MyCustomPluginExtention" 
  def sender = "MyCustomPluin" 
} 
 
class MyCustomPlugin implements Plugin<Project> { 
  void apply(Project project) { 
    project.extensions.create('myArgs', MyCustomPluginExtension) 
    project.task('customTask', type: MyCustomTask) 
  } 
} 

(2)修改plugin\src\main\groovy\com\micky\gradle\MyCustomTask.groovy

package com.micky.gradle; 
 
import org.gradle.api.DefaultTask 
import org.gradle.api.tasks.TaskAction 
 
class MyCustomTask extends DefaultTask { 
  @TaskAction 
  void output() { 
    println "Sender is ${project.myArgs.sender},\nmessage: ${project.myArgs.message}" 
  } 
} 

       
(3)修改plugin/build.gradle

apply plugin: 'groovy' 
apply plugin: 'maven' 
 
dependencies { 
  compile gradleApi() 
  compile localGroovy() 
} 
 
repositories { 
  mavenCentral() 
} 
 
group='com.micky' 
version='1.0.2' 
 
uploadArchives { 
  repositories { 
    mavenDeployer { 
      repository(url: uri('../repo')) 
    } 
  } 
} 

        執行gradle uploadArchives 編譯插件包    

(4)修改sample/build.gradle

buildscript { 
  repositories { 
    maven { 
      url uri('../repo') 
    } 
  } 
 
  dependencies { 
    classpath group: 'com.micky', 
      name: 'gradle-micky', 
      version: '1.0.2' 
  } 
} 
 
apply plugin: 'com.micky.mycustom' 

(5)執行gradle customTask,結果如下:

2016224145851154.png (558×129)

(6)在gradle文件配置參數

buildscript { 
  repositories { 
    maven { 
      url uri('../repo') 
    } 
  } 
 
  dependencies { 
    classpath group: 'com.micky', 
      name: 'gradle-micky', 
      version: '1.0.2' 
  } 
} 
 
apply plugin: 'com.micky.mycustom' 
 
myArgs { 
  sender='Micky Liu' 
  message='Gradle is so simple.' 
} 

 
(7)執行gradle customTask,結果如下:

2016224145926554.png (561×131)

8、向Plugin Task 傳遞嵌套試復雜參數
(1)拷貝一份上面的代碼,改名為CustomPluginTaskWithNestParam,修改plugin\src\main\groovy\com\micky\gradle\MyCustomPlugin.groovy

package com.micky.gradle; 
 
import org.gradle.api.*; 
 
class MyNestPluginExtension { 
  def receiver = "Kate Zhou" 
  def email = "[email protected]" 
} 
 
class MyCustomPluginExtension { 
  def message = "From MyCustomPluginExtention" 
  def sender = "MyCustomPluin" 
} 
 
class MyCustomPlugin implements Plugin<Project> { 
  void apply(Project project) { 
    project.extensions.create('myArgs', MyCustomPluginExtension) 
    project.myArgs.extensions.create('nestArgs', MyNestPluginExtension) 
    project.task('customTask', type: MyCustomTask) 
  } 
} 

(2)修改MyCustomPlugin.groovy

package com.micky.gradle; 
 
import org.gradle.api.DefaultTask 
import org.gradle.api.tasks.TaskAction 
 
class MyCustomTask extends DefaultTask { 
  @TaskAction 
  void output() { 
    println "Sender is ${project.myArgs.sender} \nmessage: ${project.myArgs.message}" 
    println "Receiver is ${project.myArgs.nestArgs.receiver} \nemail: ${project.myArgs.nestArgs.email}" 
  } 
} 

 

(3)修改plugin/build.gradle

apply plugin: 'groovy' 
apply plugin: 'maven' 
 
dependencies { 
  compile gradleApi() 
  compile localGroovy() 
} 
 
repositories { 
  mavenCentral() 
} 
 
group='com.micky' 
version='1.0.3' 
 
uploadArchives { 
  repositories { 
    mavenDeployer { 
      repository(url: uri('../repo')) 
    } 
  } 
} 

(4)執行gradle uploadArchives 編譯插件包
(5)修改sample/build.gradle

buildscript { 
  repositories { 
    maven { 
      url uri('../repo') 
    } 
  } 
 
  dependencies { 
    classpath group: 'com.micky', 
      name: 'gradle-micky', 
      version: '1.0.3' 
  } 
} 
 
apply plugin: 'com.micky.mycustom' 
 
myArgs { 
  sender='Micky Liu' 
  message='Gradle is so simple.' 
} 

          
(6)執行執行gradle customTask,結果如下:

2016224150016512.png (561×156)

(7)在gradle文件配置參數

buildscript { 
  repositories { 
    maven { 
      url uri('../repo') 
    } 
  } 
 
  dependencies { 
    classpath group: 'com.micky', 
      name: 'gradle-micky', 
      version: '1.0.3' 
  } 
} 
 
apply plugin: 'com.micky.mycustom' 
 
myArgs { 
  sender='Micky Liu' 
  message='Gradle is so simple.' 
  nestArgs { 
    receiver='David Chen' 
    email='[email protected]' 
  } 
 
} 

(8)執行gradle customTask,結果如下:

2016224150049182.png (563×154)

  源碼地址:https://github.com/mickyliu945/GradleCustomPlugin

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved