hands on the gradle
DESCRIPTION
A talk I did on the DroidCon 2011 barcamp, it's on Gradle and its Android plug-inTRANSCRIPT
![Page 1: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/1.jpg)
S
Hands on the Gradle Painless Android builds
© 2011 Matthias Käppler Qype GmbH
![Page 2: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/2.jpg)
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.
![Page 3: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/3.jpg)
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.“
![Page 4: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/4.jpg)
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>
![Page 5: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/5.jpg)
Gradle vs. Maven (cont.)
println "dunno, must be a Maven thing."
![Page 6: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/6.jpg)
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.
![Page 7: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/7.jpg)
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.
![Page 8: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/8.jpg)
How is Gradle used?
$ls build/ build.gradle ...
...
$gradle tasks
$gradle clean build
$gradle androidInstall androidInstrument
![Page 9: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/9.jpg)
build.gradle
env = System.getenv()
dependsOnChildren()
apply from: ‘shared.gradle'
allprojects { apply plugin: 'java' }
task hello << { println ‘hello from Gradle‘ }
![Page 10: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/10.jpg)
Demo
Move along, nothing to see here.
![Page 11: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/11.jpg)
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
![Page 12: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/12.jpg)
Writing tasks
task hello << { 4.times { println ‘hello from Gradle‘ } } hello.dependsOn initLang hello.someProperty = 1
![Page 13: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/13.jpg)
Writing tasks (cont.)
project.task(‘hello‘, dependsOn: ‘initLang‘) greeter = { println it } hello.doLast greeter.curry(‘hello from Gradle‘)
![Page 14: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/14.jpg)
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/**" }
![Page 15: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/15.jpg)
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
![Page 16: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/16.jpg)
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‘ } } }
![Page 17: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/17.jpg)
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" }
![Page 18: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/18.jpg)
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" }
![Page 19: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/19.jpg)
Android
S There‘s a Gradle plug-in for Android.
https://github.com/jvoegele/gradle-android-plugin
![Page 20: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/20.jpg)
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'
![Page 21: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/21.jpg)
What‘s in store
S The plug-in adds the following tasks: S :androidProcessResourcesS :androidPackageS :androidInstallS :androidInstrumentS [:proguard]$gradle clean andInstall :test-proj:andInstr
![Page 22: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/22.jpg)
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: "…” } }
![Page 23: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/23.jpg)
Thanks
Thanks for listening!
![Page 24: Hands on the Gradle](https://reader033.vdocument.in/reader033/viewer/2022061223/54c60e844a795993288b45ff/html5/thumbnails/24.jpg)
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