How to write Gradle Plugins with Groovy for Android

Since Android Studio being released,  I could always find that Google announce that Gradle is better than Maven and Ant. Especially it supports Groovy as script language natively. However I could not get chance to try it.

Recently our project requires us to implement CI and something else. So I got chance to learn it more.

Firstly I need to know how to create a customised Gradle Task using Groovy.

I tried building some simple "Hello world " tasks inside of build.gradle file. They all work fine. But once I tried to implement some more complicated ones which needs importing some other libraries, it will complain can not import.

So I recheck the doc from Gradle. There is another way to create a standalone plugin library.

http://www.gradle.org/docs/current/userguide/custom_tasks.html

So I followed the doc to create mine one.

1. Create Gradle project using Intellij
2. Add framework support -> Groovy
3. Create the source structure like this.
4. Change the build.gradle file like this:
 // File: build.gradle  
 apply plugin: 'groovy' // Need it to compile plugin classes.  
 apply plugin: 'maven'// Need it to deploy to Maven repository.  
 // Project configuration is used for Maven deploy:  
 version = '1.0'  
 group = 'com.damon.gradle.plugins'  
 uploadArchives {  
   repositories.mavenDeployer {  
     repository url: 'file:///Users/damon/work/temp/repo' // this is your local maven repo  
   }  
 }  
 dependencies {  
   // Add all JAR files in the lib directory of theGradle installation directory  
   // to the groovy configuration (is also used by compile configuration)  
   groovy fileTree(dir: new File(gradle.gradleHomeDir, 'lib'), includes: ['*.jar'])  
 }  
 // Make sure all code is compiled, tested and checked before uploadArchives.  
 uploadArchives.dependsOn ':build'  

5.  Create your Groovy class . Like this:

package com.damon.gradle.plugins;  
 import org.gradle.api.DefaultTask  
 import org.gradle.api.tasks.TaskAction  
 /**  
  * Created by damon on 25/3/14.  
  */  
 class GreetingTask extends DefaultTask {  
   String greeting = 'hello from GreetingTask'  
   @TaskAction  
   def greet() {  
     println greeting  
   }  
 }

6. Then just run the gradle task uploadArchievs.
     You can type in Terminal : gradle uploadArchievs
     Or you can also double click the gradle tasks list from your right tab of Intellij.

7. After this your gradle plugin already been deployed to your local maven repo.
    If you have a remote maven repo , you can push the update the latest.

8. I just tried it using local maven repo with my Android Project.
   Inside of your build.gradle file of Android project add these lines of codes:
 buildscript {  
   repositories {  
     maven {  
       url uri('file:///Users/damon/work/temp/repo')  
     }  
   }  
   dependencies {  
     classpath group: 'com.damon.gradle.plugins', name: 'HelloGradle', version: '1.0'  
   }  
 }  
 task greeting(type : com.damon.gradle.plugins.GreetingTask) {  
   greeting = 'howdy!'  
 }  

Then gradle sync. you will see your task "greeting" is listed in your tasks tab.

Done.


http://mrhaki.blogspot.sg/2009/11/gradle-goodness-deploying-our-gradle.html
http://www.gradle.org/docs/current/userguide/custom_tasks.html

Comments

Popular posts from this blog

A wired issue of MediaPlayer on Android

Problem when using Jackson json parser in Android.

Gradle issue - peer not authenticated