編輯:關於Android編程
Google已經建議Android開發全部轉向Android Studio開發,Android Studio 是使用gradle編譯、打包的,那麼問題來了,gradle可是有一堆東西...,為了徹底了解gradle,今天就來學習下如何寫自己的gradle插件(當然插件源碼是使用groovy寫的),先看如下代碼目錄:
如上圖所示,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
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、執行命令
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 結果如下:
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,結果如下:
(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,結果如下:
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,結果如下:
(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,結果如下:
源碼地址:https://github.com/mickyliu945/GradleCustomPlugin
前言:本篇文章主要學習UI性能優化。1. 應用UI卡頓的原理1.1 原理用戶感覺到卡頓的原理就是在渲染時出現了掉幀。Android系統要求 1s內渲染60幀,即渲染1幀必
首先給大家展示靓照,對效果圖感興趣的朋友可以繼續往下閱讀哦。ViewPager這個小demo實現的是可以左右循環滑動圖片,下面帶索引,滑到最後一頁在往右滑動就要第一頁,第
自從騰訊QQ中的圓形頭像,火了起來後,現在我們在一些應用中都能看到圓形頭像的身影,在個人主頁或者個人資料面板中使用圓形頭像,會使整個布局變得更加優雅現在我們來進行第一步,
該文章是基於apkplug V1.6.8 進行說明的 該 IMThemeDemo 實現了簡單的聊天泡泡切換功能,同時主題包(插件)是托管在apkplug的雲