gradle : Building Android Apps Mobel Meetup 2013-10-15 @alexvb http://alex.vanboxel.be/
Biography Working with Java since the dark ages at Progress Software, Alcatel-Lucent, Interested in science and technology Alex Van Boxel Software Architect - Mobile @ Vente-Exclusive.com
Build History a brief history of java build systems
In the beginning there was nothing
Bo ttl 20 ed i 00 n created Ant and he saw it as messy
Ant example <project> <target name="clean"> <delete dir="build"/> </target> <target name="compile"> <mkdir dir="build/classes"/> <javac srcdir="src" destdir="build/classes"/> </target> <target name="jar"> <mkdir dir="build/jar"/> <jar destfile="build/jar/helloworld.jar" basedir="build/classes"> <manifest> <attribute name="main-class" value="io.mobel.helloworld"/> </manifest> </jar> </target> <target name="run"> <java jar="build/jar/helloworld.jar" fork="true"/> </target> </project>
Bo ttl 20 ed i 01 n + created Maven and he saw it was better, but...
Maven example <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.mycompany.app</groupid> <artifactid>my-app</artifactid> <version>1.0-snapshot</version> <packaging>jar</packaging> <name>maven Quick Start Archetype</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.8.2</version> <scope>test</scope> </dependency> </dependencies> </project>
Maven s build by convention my-app -- pom.xml `-- src -- main `-- java `-- com `-- mycompany `-- app `-- App.java `-- test `-- java `-- com `-- mycompany `-- app `-- AppTest.java
Bo ttl 20 ed i 11 n + created Gradle build files in XML is so 80 s
Out of the box features Declarative Syntax Build-By-Convention Task-graph Multi-Project Builds Dependency Power Tool Groovy
Android Build System = Gradle + Android Gradle Plugin + IDE integration
Why a new Build System Customizable and extensible but stable API Unified across IDE and CI Standard and Advanced features
One Build System to Rule them All Same build system from IDE, command-line and continuous-integration environment
Advanced Tool Interface API IDEA and Studio Command Line MobelDemoProject git:(master) gradle tasks :tasks -----------------------------------------------------------All tasks runnable from root project -----------------------------------------------------------Android tasks ------------androiddependencies - Displays the Android dependencies of the project signingreport - Displays the signing info for each variant Build tasks ----------assemble - Assembles all variants of all applications and secondary packages. assembledebug - Assembles all Debug builds assemblefreegoogleio - Assembles all builds for flavor FreeGoogleIO assemblefreegoogleiodebug - Assembles the Debug build for flavor FreeGoogleIO assemblefreegoogleiorelease - Assembles the Release build for flavor FreeGoogleIO assemblefreegoogleiostaging - Assembles the Staging build for flavor FreeGoogleIO TeamCity Others Eclipse Atlassian Bamboo Jenkins etc...
Advanced Tool Interface API Integrates with Android Studio based on IDEA or works in Eclipse with Android Plugin...
Declarative Domain Specific Language The Android plugin extends the Gradle DSL
Gradle Skelet buildscript { repositories { mavencentral() } dependencies { classpath 'com.android.tools.build:gradle:0.6.1' } } apply plugin: 'android' android { compilesdkversion 17 buildtoolsversion 18.1 }
Convention over Configuration my-app -- build.gradle `-- src -- main `-- java `-- test `-- java
Convention over Configuration my-app -- build.gradle `-- src -- main -- AndroidManifest.xml -- res -- assets -- aidl -- rs -- jni `-- java -- instrumentedtest -- res -- assets -- aidl -- rs -- jni `-- java `-- test `-- java
Gradle Tasks gradle <task> [<task>] gradle tasks [--all] assemble assemble the project(s) check run all the checks build assemble + check clean clean all the output
Build Variant = Build Types + Product Flavors
DSL: Build Types buildtypes { release { signingconfig signingconfigs.release } debug { packagenamesuffix ".debug" versionnamesuffix = ".D" debuggable true } staging.initwith(buildtypes.debug) staging { packagenamesuffix ".staging" buildconfig "private final static String server = \"staging.example.com\"" } }
DSL: Signing Configuration signingconfigs { release { storefile file("src/mobel.keystore") storepassword "mobelmobel" keyalias "release" keypassword "mobelmobel" } } buildtypes { release { signingconfig signingconfigs.release }
Convention over Configuration my-app -- build.gradle `-- src -- main -- AndroidManifest.xml -- res -- assets -- aidl -- rs -- jni `-- java -- debug --... -- staging --... -- instrumentedtest --... `-- test `-- java
DSL: Build Types debug release staging debug release staging
DSL: Product Flavor Example without groups productflavors { Mobel { } GoogleIO { } Example with groups flavorgroups 'version','session' productflavors { Mobel { flavorgroup 'session' } GoogleIO { flavorgroup 'session' } free { flavorgroup 'version' } paid { flavorgroup 'version' } }
Build Type + Product Flavor debug release mobel MobelDebug MobelRelease googleio GoogleIODebug GoogleIORelease and Flavor Groups free pail debug release mobel FreeMobelDebug FreeMobelRelease googleio FreeGoogleIODebug FreeGoogleIORelease mobel PaidMobelDebug PaidMobelRelease googleio PaidGoogleIODebug PaidGoogleIORelease
DEMO
Stable Build Environment keep is stable...
Stable Build Environment Gradle version use the gradle-wrapper Plugin version dependencies { classpath 'com.android.tools.build:gradle:0.6.1' }
Stable Build Environment Build tools android { compilesdkversion 18 buildtoolsversion "18.1.0" IDE doesn t build your project
Testing unit- and integration testing in the same project
Test Code Generation Testing Sources Sets Standard test/ Android instrumentedtest(flavor)/ Generated Manifest Special APK for library projects
Testing Scenarios check connecteddevices devicecheck
Test Server API use the cloud for parallel testing
Testing Server API Control your lab Services from TestDroid Manymo AppThwack image from AppThwack website
Dependency Management flexible and reuse your current repo s
Dependencies dependencies { compile 'com.android.support:support-v4:18.0.+' compile 'com.android.support:appcompat-v7:18.0.+' compile 'com.google.android.gms:play-services:3.2.65' compile 'com.google.android.gtm:tagmanager:3.0.0' compile 'joda-time:joda-time:2.3' compile 'com.fasterxml.jackson.core:jackson-core:2.2.3' compile 'com.fasterxml.jackson.core:jackson-databind:2.2.3' compile 'com.google.guava:guava:15.0' compile 'com.squareup.dagger:dagger:1.1.0' compile 'com.squareup.dagger:dagger-compiler:1.1.0' compile(group: 'de.keyboardsurfer.android.widget', name: 'crouton', version: '1.8.1') { transitive = false } }
Corporate Repositories Repo X Repo Y Maven Central Gradle Project Gradle Project Gradle Project Gradle Project Gradle Project Gradle Project Gradle Project Corporate Repository gradle.cache
Corporate Repository: Nexus Open Source repository manager...
Corporate Repository: Management Google Tag Manager example...
Reference http://tools.android.com/tech-docs/new-build-system http://www.gradle.org/ http://www.gradleware.com/ Google I/O (on youtube) Follow me @alexvb +Alex Van Boxel ( web: http://alex.vanboxel.be/ )