Learn Gradle Basics (For Android developers)

Learn Gradle Basics (For Android developers)

As an Android developer, Learning Gradle will help us improve the building process of Android.

Groovy is the language used to write Gradle build script files. Even though it is not necessary to learn groovy, we should understand the basics of the language. It will help us to improve our project building procedure.


Download & install Gradle.

Note :- I will show how to install and run Gradle in macOSX.

There are bunch of ways to install gradle to your pc. If have HomeBrew Installed in your pc, we can do installation in a few clicks.

open terminal and run the following command,


$ brew install gradle


Download & install Gradle manually.


First, First,  go to https://gradle.org/gradle-download/, and download the latest
version of Gradle. Don’t forget  that we want the complete distribution.

After you downloaded the zip file,  use following commands to set it up in your environment.

Assuming that you have downloaded the zip file in Downloads folder..


unzip ~/Downloads/gradle-3.3-all.zip -d /usr/local/gradle/ &&\

echo '# Adding Gradle to system path

export GRADLE_HOME=/usr/local/gradle/gradle-3.3


export PATH' >> ~/.bash_profile &&\

source ~/.bash_profile



Gradle is added to PATH now.  you can now access the gradle using command like .

To verify the gradle installation, check the version of the gradle.


$ gradle --version


Your result should be something like this,



Gradle 4.2.1


Build time:   2017-10-02 15:36:21 UTC

Revision:     a88ebd6be7840c2e59ae4782eb0f27fbe3405ddf

Groovy:       2.4.12

Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015

JVM:          1.8.0_111 (Oracle Corporation 25.111-b14)

OS:           Mac OS X 10.12 x86_64


Lets create simple Gradle task


Move to your testing folder


cd ~/Documents/gradle/testGradle/


Create new File called build.gradle


Using command line,

touch build.gradle



Now edit the build.gradle file and append the following code.


task helloTask {
println "Hello, This is Test Gradle Task!"


Now save and run the task like below.


gradle helloTask


Result should be like this,


> Task :helloTask

Hello, This is Test Gradle Task!


1 actionable task: 1 executed


Java inside the gradle build file


add the following code and save the gradle.build file

task javaClassInGradleTask << {}

class Myclass {

public void message() {

System.out.println("this is message from java class");



Myclass testObject = new Myclass();



Execute the gradle file now

gradle javaClassInGradleTask


Code succeeded even though the left shift is deprecated.


> Configure project :

The Task.leftShift(Closure) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use Task.doLast(Action) instead.

at build_8kwyh5u1biozuvcumx9ypf9c3.run(/Users/zumry/Documents/Zumry_Data/Udacity/gradle/testme/build.gradle:2)

(Run with --stacktrace to get the full stack trace of this deprecation warning.)

this is message from java class


1 actionable task: 1 executed




lets invoke simple method call in gradle 


Save the build.gradle file with the following call


task javaClassInGradleTask << {}

def doubleIt(n) {

n + n;   //no need return key

def foo = 5

println("doubleIt($foo) == ${doubleIt(foo)}");


and the result will be


doubleIt(5) == 10



Nested method call in build.gradle file


task javaClassInGradleTask << {}
def noArg() {

println("No argument")

def oneArg(first) {

println("first argument is ${first}")

def twoArg(first, second) {

println("first argument is ${first} and second argument is ${second}")


noArg //incorrect call

noArg()  //Correct call

oneArg 100  // calling oneArg method without paranthesis . it works

oneArg(100) //this works

twoArg oneArg 250, 400 //not working

twoArg oneArg(250), 400 //it works

twoArg(250, 400) // it works



How to pass closure into function


task javaClassInGradleTask << {}

def doubleIt = {

x -> x + x


def doubleIt = { x -> x + x}

def callfunc(func, arg) {



def vall = 5

def returnVal = callfunc(doubleIt, vall)

println("two times doubled value is ${returnVal}")


result would be,

two times doubled value is 20



Initiate List and print list values


task javaClassInGradleTask << {}

def myList = ["mango", "orange", "papaya", "banana"]

def callSingleItem = {item -> println("Item is $item")}


myList.each{println "this is the value item $it"} //in groovy $it has the first parameter value assigned.







Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.