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,

 

[code]

$ brew install gradle

[/code]

 

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..

[code]

 

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

PATH=$GRADLE_HOME/bin:$PATH

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

source ~/.bash_profile

 

[/code]

 

Gradle is added to [code]PATH[/code] now.  you can now access the gradle using command like .

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

 

[code]

$ gradle –version

[/code]

 

Your result should be something like this,

 

[code]

————————————————————

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

[/code]

 

Lets create simple Gradle task

 

Move to your testing folder

 

[code]

cd ~/Documents/gradle/testGradle/

[/code]

 

Create new File called [code]build.gradle[/code]

 

Using command line,

[code]

touch build.gradle

[/code]

 

 

Now edit the [code]build.gradle[/code] file and append the following code.

[code]

//~/Documents/gradle/testGradle/build.gradle

task helloTask {
doLast{
println “Hello, This is Test Gradle Task!”
}
}

[/code]

 

Now save and run the task like below.

 

[code]

gradle helloTask

[/code]

 

Result should be like this,

 

[code]

> Task :helloTask

Hello, This is Test Gradle Task!

BUILD SUCCESSFUL in 0s

1 actionable task: 1 executed

[/code]

 

Java inside the gradle build file

 

add the following code and save the gradle.build file

[code]

task javaClassInGradleTask << {}

class Myclass {

public void message() {

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

}

}

Myclass testObject = new Myclass();

testObject.message();

[/code]

 

Execute the gradle file now

[code]

gradle javaClassInGradleTask

[/code]

 

Code succeeded even though the left shift is deprecated.

[code]

 

> 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

BUILD SUCCESSFUL in 0s

1 actionable task: 1 executed

 

[/code]

 

 

lets invoke simple method call in gradle 

 

Save the build.gradle file with the following call

 

[code]

task javaClassInGradleTask << {}

def doubleIt(n) {

n + n;   //no need return key

}
def foo = 5

println(“doubleIt($foo) == ${doubleIt(foo)}”);

[/code]

 

and the result will be

 

[code]

doubleIt(5) == 10

BUILD SUCCESSFUL in 0s

[/code]

 

Nested method call in build.gradle file

 

[code]

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

 

[/code]

 

How to pass closure into function

 

[code]

task javaClassInGradleTask << {}

def doubleIt = {

x -> x + x

}

def doubleIt = { x -> x + x}

def callfunc(func, arg) {

func(func(arg))

}

def vall = 5

def returnVal = callfunc(doubleIt, vall)

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

[/code]

 

result would be,

[code]

two times doubled value is 20

BUILD SUCCESSFUL in 0s

[/code]

 

Initiate List and print list values

 

[code]

task javaClassInGradleTask << {}

def myList = [“mango”, “orange”, “papaya”, “banana”]

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

myList.each(callSingleItem)

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

[/code]

 

 

 

 

cheers…!!

 

Add a Comment

Your email address will not be published. Required fields are marked *

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