SlideShare a Scribd company logo
Hands on the Gradle
     Painless Android builds


     © 2011 Matthias Käppler
          Qype GmbH




                               S
What is Gradle?


S  Gradle is a task based build system. From files and
   configuration it assembles build artifacts.

S  Gradle is flexible. It is not bound to any specific process or
   technology.

S  Gradle uses a Groovy based DSL to write configuration.
   This makes it easy to read and write Gradle scripts.
The Gradle manifesto


S  „Build scripts are code.“

S  Don‘t expect, allow.

S  Don‘t re-invent, re-use.

S  Don‘t inherit, inject.

S  Scale to the complexity of a problem.
   – „Make the impossible possible, the possible easy, and the
   easy elegant.“
Gradle vs. Maven

<build>

  <plugins>

    <plugin>

      <artifactId>maven-antrun-plugin</artifactId>

      <executions>

         <execution>

           <phase>install</phase>

           <goals>

              <goal>run</goal>

           </goals>

           <configuration>

              <tasks>

                <echo message="what’s with the bloat?" />

              </tasks>

           </configuration>

         </execution>

      </executions>

    </plugin>           

  </plugins>

</build>
Gradle vs. Maven (cont.)

	

	

	


println "dunno, must be a Maven thing."
Gradle vs. Maven (cont.)


S  Maven is declarative.
   Gradle is imperative.

S  Maven is verbose.
   Gradle is concise.

S  Maven assumes Maven.
   Gradle doesn‘t.

S  Maven scales poorly to simple problems.
   Gradle scales with the complexity of a problem.
Gradle vs. Maven (cont.)


S  Maven has a rich plug-in ecosystem.
  Gradle still needs to catch-up here.

S  Maven has very good IDE support.
  Gradle has... IDE support.

S  Maven has project archetypes.
  Gradle doesn‘t.
How is Gradle used?


$ls

build/   build.gradle   ...	

...	

$gradle tasks	

$gradle clean build	

$gradle androidInstall androidInstrument
build.gradle	


env = System.getenv()	

dependsOnChildren()	

apply from: ‘shared.gradle'	

allprojects {

   apply plugin: 'java'

}	

task hello << {

   println ‘hello from Gradle‘

}
Demo	



Move along, nothing to see here.
More about tasks


S  There are different ways how tasks can be used or exposed
  in a build script:

  1 – by writing them

  2 – through project.ant

  3 – by applying a plug-in
Writing tasks	


	

task hello << {

   4.times { println ‘hello from Gradle‘ }

}



hello.dependsOn initLang



hello.someProperty = 1
Writing tasks (cont.)	


	

project.task(‘hello‘, dependsOn: ‘initLang‘)



greeter = { println it }



hello.doLast greeter.curry(‘hello from Gradle‘)
Ant tasks


S  Ant tasks are first class citizens in Gradle. You access them
    through Groovy‘s AntBuilder DSL.



myProp = ant.properties["my.prop"]



ant.copy {

  from zipTree("/path/to/lib.jar")

  into "$buildDir/extracted-classes"

  exclude "com.example/**"

}
Plug-ins	


S  Most of Gradle‘s functionality comes from plug-ins. This helps in
    keeping the core Gradle APIs lean and clean.



    apply plugin: 'java'

    apply plugin: 'maven'



    ...



    $gradle clean install
Plug-ins (cont.)	


S  Writing Gradle plug-ins is very simple. Check this out.



    class MyPlugin implements Plugin<Project> {

       def apply(Project project) {

          project.task(‘hello‘) << {

             println ‘hello, people‘

          }

       }

    }
Dependencies	


S  Gradle doesn‘t define ist own dependency management system.
    Instead, it builds on Apache Ivy.



repositories {

   mavenCentral()

   mavenRepo urls: "http://my.repo.com"

   flatDir dirs: "libs"

}
Dependencies (cont.)	


S  Dependencies are grouped into configurations. A configuration is
    simply a set of files bound to a name.



dependencies {

   compile "commons-lang:commons-lang:2.5"

   compile fileTree(dir: "libs", include: "*.jar")

   testCompile "junit:junit:4.8.2"

}
Android	


S  There‘s a Gradle plug-in for Android.

	



https://github.com/jvoegele/gradle-android-plugin
Setting it up	


S  Now:

   

   buildscript {

   

        repositories {

           mavenRepo(urls: 'http://jvoegele.com/maven2/‘)

        }

   

        dependencies {

           classpath 'com.jvoegele.gradle.plugins:android-plugin:0.9.8‘

        }

   }

   

   apply plugin: com.jvoegele.gradle.plugins.android.AndroidPlugin	

S  Soon:

   

   apply plugin: 'android'
What‘s in store	


S  The plug-in adds the following tasks:
   S  :androidProcessResources	
   S  :androidPackage	
   S  :androidInstall	
   S  :androidInstrument	
   S  [:proguard]	
   

   

   $gradle clean andInstall :test-proj:andInstr
Instrumentation tests	


androidInstrument {



     runners {



          run testpackage: "unit", with: "com.my.UnitTestRunner",
            	name: "instrumentation-unit-tests"



          run annotation: "android.test.suitebuilder.annotation.Smoke"



          run with: "com.my.OtherTestRunner”, options: "…”



     }



}
Thanks	



Thanks for listening!
Android in Practice	


     Charlie Collins, Michael Galpin, Matthias Käppler

     •  Real world practical recipes
     •  Focus on intermediate to professional developers
     •  Two chapters on testing and build automation

     Summer 2011

     MEAP edition available

     http://manning.com/collins

More Related Content

PPTX
Java 8 - An Overview
PPTX
Java 8 streams
PDF
An Introduction To Linux
PDF
2. Develop a MapReduce program to calculate the frequency of a given word in ...
PPTX
Introduction to Julia Language
PPTX
Java Introduction
PDF
UM2019 Extended BPF: A New Type of Software
PPTX
Introduction to ParSeq: to make asynchronous java easier
Java 8 - An Overview
Java 8 streams
An Introduction To Linux
2. Develop a MapReduce program to calculate the frequency of a given word in ...
Introduction to Julia Language
Java Introduction
UM2019 Extended BPF: A New Type of Software
Introduction to ParSeq: to make asynchronous java easier

What's hot (20)

PPTX
Go Programming Language (Golang)
ODP
Introduction to Shell script
PPTX
Single Pass Assembler
PPT
Linux command ppt
PPT
A hands-on introduction to the ELF Object file format
PPT
JAVA OOP
PDF
Java 8 Workshop
PPTX
Migrating to Java 11
PPTX
Android audio system(audioplicy_service)
PPT
Java basic introduction
PDF
5 best Java Frameworks
PPTX
Networking in Java
PPTX
Java 11 to 17 : What's new !?
PPT
Intro to linux
PDF
Linux systems - Linux Commands and Shell Scripting
PPTX
Linux booting process - Linux System Administration
PPTX
java Jdbc
Go Programming Language (Golang)
Introduction to Shell script
Single Pass Assembler
Linux command ppt
A hands-on introduction to the ELF Object file format
JAVA OOP
Java 8 Workshop
Migrating to Java 11
Android audio system(audioplicy_service)
Java basic introduction
5 best Java Frameworks
Networking in Java
Java 11 to 17 : What's new !?
Intro to linux
Linux systems - Linux Commands and Shell Scripting
Linux booting process - Linux System Administration
java Jdbc
Ad

Viewers also liked (20)

PPT
Groovier testing with Spock
ZIP
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
PDF
Gradle a new Generation Build Tool
KEY
function list
PDF
レガシーコード改善はじめました 横浜道場
PDF
Testing Web Applications with GEB
PDF
Groovy, Transforming Language
KEY
Androidリリース作業の効率化(2)
PPT
Spock Framework
PDF
Grails/Groovyによる開発事例紹介
ODP
Jenkins導入ライブ
PPT
Jenkinsプラグイン開発
PDF
Jenkinsプラグインの作り方
PDF
How about Gradle?
PDF
Jenkinsを用いたAndroidアプリビルド作業効率化
PDF
GradleによるG*なビルドシステムの構築
PDF
Spock: A Highly Logical Way To Test
PDF
Spockを使おう!
PDF
Gradle talk, Javarsovia 2010
KEY
The outlineoftestprocess
Groovier testing with Spock
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Gradle a new Generation Build Tool
function list
レガシーコード改善はじめました 横浜道場
Testing Web Applications with GEB
Groovy, Transforming Language
Androidリリース作業の効率化(2)
Spock Framework
Grails/Groovyによる開発事例紹介
Jenkins導入ライブ
Jenkinsプラグイン開発
Jenkinsプラグインの作り方
How about Gradle?
Jenkinsを用いたAndroidアプリビルド作業効率化
GradleによるG*なビルドシステムの構築
Spock: A Highly Logical Way To Test
Spockを使おう!
Gradle talk, Javarsovia 2010
The outlineoftestprocess
Ad

Similar to Hands on the Gradle (20)

PDF
Why gradle
PDF
Gradle - time for a new build
ODP
Gradle: The Build System you have been waiting for!
PDF
Gradle - Build system evolved
PDF
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
PDF
What's new in Gradle 4.0
PDF
OpenCms Days 2012 - Developing OpenCms with Gradle
PDF
PDF
In the Brain of Hans Dockter: Gradle
PDF
Make Your Build Great Again (DroidConSF 2017)
PPTX
Faster Java EE Builds with Gradle
PPTX
Faster Java EE Builds with Gradle
PDF
Javaone - Gradle: Harder, Better, Stronger, Faster
ODP
Gradle - time for another build
PPTX
PDF
Why Gradle?
PDF
Enter the gradle
PDF
Getting started with building your own standalone Gradle plugin
PPTX
Gradle : An introduction
PDF
Rene Groeschke
Why gradle
Gradle - time for a new build
Gradle: The Build System you have been waiting for!
Gradle - Build system evolved
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
What's new in Gradle 4.0
OpenCms Days 2012 - Developing OpenCms with Gradle
In the Brain of Hans Dockter: Gradle
Make Your Build Great Again (DroidConSF 2017)
Faster Java EE Builds with Gradle
Faster Java EE Builds with Gradle
Javaone - Gradle: Harder, Better, Stronger, Faster
Gradle - time for another build
Why Gradle?
Enter the gradle
Getting started with building your own standalone Gradle plugin
Gradle : An introduction
Rene Groeschke

Hands on the Gradle

  • 1. Hands on the Gradle Painless Android builds © 2011 Matthias Käppler Qype GmbH S
  • 2. What is Gradle? S  Gradle is a task based build system. From files and configuration it assembles build artifacts. S  Gradle is flexible. It is not bound to any specific process or technology. S  Gradle uses a Groovy based DSL to write configuration. This makes it easy to read and write Gradle scripts.
  • 3. The Gradle manifesto S  „Build scripts are code.“ S  Don‘t expect, allow. S  Don‘t re-invent, re-use. S  Don‘t inherit, inject. S  Scale to the complexity of a problem. – „Make the impossible possible, the possible easy, and the easy elegant.“
  • 4. Gradle vs. Maven <build>
 <plugins>
 <plugin>
 <artifactId>maven-antrun-plugin</artifactId>
 <executions>
 <execution>
 <phase>install</phase>
 <goals>
 <goal>run</goal>
 </goals>
 <configuration>
 <tasks>
 <echo message="what’s with the bloat?" />
 </tasks>
 </configuration>
 </execution>
 </executions>
 </plugin> 
 </plugins>
 </build>
  • 5. Gradle vs. Maven (cont.) println "dunno, must be a Maven thing."
  • 6. Gradle vs. Maven (cont.) S  Maven is declarative. Gradle is imperative. S  Maven is verbose. Gradle is concise. S  Maven assumes Maven. Gradle doesn‘t. S  Maven scales poorly to simple problems. Gradle scales with the complexity of a problem.
  • 7. Gradle vs. Maven (cont.) S  Maven has a rich plug-in ecosystem. Gradle still needs to catch-up here. S  Maven has very good IDE support. Gradle has... IDE support. S  Maven has project archetypes. Gradle doesn‘t.
  • 8. How is Gradle used? $ls
 build/ build.gradle ... ... $gradle tasks $gradle clean build $gradle androidInstall androidInstrument
  • 9. build.gradle env = System.getenv() dependsOnChildren() apply from: ‘shared.gradle' allprojects {
 apply plugin: 'java'
 } task hello << {
 println ‘hello from Gradle‘
 }
  • 10. Demo Move along, nothing to see here.
  • 11. More about tasks S  There are different ways how tasks can be used or exposed in a build script: 1 – by writing them 2 – through project.ant 3 – by applying a plug-in
  • 12. Writing tasks task hello << {
 4.times { println ‘hello from Gradle‘ }
 }
 
 hello.dependsOn initLang
 
 hello.someProperty = 1
  • 13. Writing tasks (cont.) project.task(‘hello‘, dependsOn: ‘initLang‘)
 
 greeter = { println it }
 
 hello.doLast greeter.curry(‘hello from Gradle‘)
  • 14. Ant tasks S  Ant tasks are first class citizens in Gradle. You access them through Groovy‘s AntBuilder DSL. 
 myProp = ant.properties["my.prop"]
 
 ant.copy {
 from zipTree("/path/to/lib.jar")
 into "$buildDir/extracted-classes"
 exclude "com.example/**"
 }
  • 15. Plug-ins S  Most of Gradle‘s functionality comes from plug-ins. This helps in keeping the core Gradle APIs lean and clean. 
 apply plugin: 'java'
 apply plugin: 'maven'
 
 ...
 
 $gradle clean install
  • 16. Plug-ins (cont.) S  Writing Gradle plug-ins is very simple. Check this out. 
 class MyPlugin implements Plugin<Project> {
 def apply(Project project) {
 project.task(‘hello‘) << {
 println ‘hello, people‘
 }
 }
 }
  • 17. Dependencies S  Gradle doesn‘t define ist own dependency management system. Instead, it builds on Apache Ivy. 
 repositories {
 mavenCentral()
 mavenRepo urls: "http://my.repo.com"
 flatDir dirs: "libs"
 }
  • 18. Dependencies (cont.) S  Dependencies are grouped into configurations. A configuration is simply a set of files bound to a name. 
 dependencies {
 compile "commons-lang:commons-lang:2.5"
 compile fileTree(dir: "libs", include: "*.jar")
 testCompile "junit:junit:4.8.2"
 }
  • 19. Android S  There‘s a Gradle plug-in for Android. 
 https://github.com/jvoegele/gradle-android-plugin
  • 20. Setting it up S  Now:
 
 buildscript {
 
 repositories {
 mavenRepo(urls: 'http://jvoegele.com/maven2/‘)
 }
 
 dependencies {
 classpath 'com.jvoegele.gradle.plugins:android-plugin:0.9.8‘
 }
 }
 
 apply plugin: com.jvoegele.gradle.plugins.android.AndroidPlugin S  Soon:
 
 apply plugin: 'android'
  • 21. What‘s in store S  The plug-in adds the following tasks: S  :androidProcessResources S  :androidPackage S  :androidInstall S  :androidInstrument S  [:proguard] 
 
 $gradle clean andInstall :test-proj:andInstr
  • 22. Instrumentation tests androidInstrument {
 
 runners {
 
 run testpackage: "unit", with: "com.my.UnitTestRunner", name: "instrumentation-unit-tests"
 
 run annotation: "android.test.suitebuilder.annotation.Smoke"
 
 run with: "com.my.OtherTestRunner”, options: "…”
 
 }
 
 }
  • 24. Android in Practice Charlie Collins, Michael Galpin, Matthias Käppler •  Real world practical recipes •  Focus on intermediate to professional developers •  Two chapters on testing and build automation Summer 2011 MEAP edition available http://manning.com/collins