Slide 1

Slide 1 text

Android Development with Gradle shakalaca @ Google I/O Extended Taipei http://goo.gl/QTcTlP

Slide 2

Slide 2 text

Hello ! I’m Shaka • Co-Organizer @ GDG Taipei • Consultant @ SleepNOVA, Inc. • CTO @ SmarTapper Co., Ltd. http:/ /about.me/shakalaca

Slide 3

Slide 3 text

What is gradle ? Gradle is build automation evolved. Gradle can automate the building, testing, publishing, deployment and more of software packages or other types of projects such as generated static websites, generated documentation or indeed anything else. ! Gradle combines the power and flexibility of Ant with the dependency management and conventions of Maven into a more effective way to build. Powered by a Groovy DSL and packed with innovation, Gradle provides a declarative way to describe all kinds of builds through sensible defaults. Gradle is quickly becoming the build system of choice for many open source projects, leading edge enterprises and legacy automation challenges. - http:/ /www.gradle.org/

Slide 4

Slide 4 text

.. short version An open source build system that combines the power of Ant & Maven but easy to use

Slide 5

Slide 5 text

Why (google wants to change the build system to) gradle ? • Simplify and unify project dependency management in IDE & CLI • Think about Eclipse & ant (ci server) • Android Studio is coming ! BETA

Slide 6

Slide 6 text

Should I use it ? • For small project • Easy to convert • For large project • Worth trying if it can simplify the configurations (dependency) • For project with variants • Time for better management tool

Slide 7

Slide 7 text

How gradle works ? • Lifecycle • Initialisation • Configuration • Execution • Build scripts • build.gradle • settings.gradle (optional) • gradle.properties (optional) http://www.gradle.org/docs/current/userguide/build_lifecycle.html

Slide 8

Slide 8 text

Command • gradle • tasks • clean • assemble • assembleRelease • assembleDebug • aR / asD • check • build (assemble + check)

Slide 9

Slide 9 text

Android integration • gradle android plugin • http:/ /tools.android.com/tech-docs/new- build-system/ • output • /build/output/apk/

Slide 10

Slide 10 text

Setup • Install gradle • http:/ /www.gradle.org/docs/current/ userguide/installation.html • Install add-on for Android • SDK Manager

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Project structure • / • gradle wrapper • configuration files • *.gradle / *.properties • app • library • lib1, lib2 …

Slide 13

Slide 13 text

gradle wrapper • Helper for project building • Compile your code anywhere • Easy to install • Just run “gradle wrapper” under project directory

Slide 14

Slide 14 text

gradle wrapper

Slide 15

Slide 15 text

/ gradle.properties • Gradle specific settings • org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError - Dfile.encoding=UTF-8 • org.gradle.parallel=true • Project wide variables • var_name_you_want_to_use=value • use with project.var_name_you_want_to_use http://www.gradle.org/docs/current/userguide/build_environment.html

Slide 16

Slide 16 text

/ settings.gradle

Slide 17

Slide 17 text

/build.gradle

Slide 18

Slide 18 text

/app/ build.gradle

Slide 19

Slide 19 text

/app/ build.gradle

Slide 20

Slide 20 text

/app/ build.gradle

Slide 21

Slide 21 text

Android packages ! • com.android.support:support-v4:19.1.0 • com.android.support:support-v13:19.1.0 • com.android.support:appcompat-v7:19.1.0 • com.android.support:gridlayout-v7:19.1.0 • com.android.support:mediarouter-v7:19.1.0 • com.google.android.gms:play-services:4.4.52 • com.android.support:support-annotations:19.1.0

Slide 22

Slide 22 text

/app/ build.gradle

Slide 23

Slide 23 text

defaultConfig • versionCode • versionName • minSdkVersion • targetSdkVersion • applicationId (old: packageName) • signingConfig • proguardFile • proguardFiles • testApplicationId (old: testPackageName) • testInstrumentationR unner

Slide 24

Slide 24 text

/app/ build.gradle

Slide 25

Slide 25 text

/app/ build.gradle

Slide 26

Slide 26 text

Build types • default 2 types: debug & release • for debugging & signing • custom type • custom.initWith(buildTypes.debug)

Slide 27

Slide 27 text

Build types • debuggable • jniDebugBuild • renderscriptDebugBuild • renderscriptOptimLevel • applicationIdSuffix (old: packageNameSuffix) • versionNameSuffix • signingConfig • zipAlign • runProguard • proguardFile • proguardFiles

Slide 28

Slide 28 text

/app/ build.gradle

Slide 29

Slide 29 text

/app/ build.gradle

Slide 30

Slide 30 text

lint options • change severity • options: http:/ /tools.android.com/tips/lint-checks • ex: fatal 'NewApi', ‘InlineApi' • error/warning/ignore • reports • textReport/xmlReport/htmlReport (boolean) • textOutput/xmlOutput/htmlOutput (file) http:/ /tools.android.com/tech-docs/new-build-system/user-guide#TOC-Lint-support

Slide 31

Slide 31 text

For the brave… • abortOnError true • checkAllWarnings true • warningsAsErrors true

Slide 32

Slide 32 text

Other options • packagingOptions
 { exclude ‘META-INF/LICENSE.txt' 
 exclude ‘META-INF/NOTICE.txt’ } • aaptOptions
 { useAaptPngCruncher = true } • compileOptions
 { sourceCompatibility JavaVersion.VERSION_1_7
 targetCompatibility JavaVersion.VERSION_1_7 }

Slide 33

Slide 33 text

/libproject/ build.gradle

Slide 34

Slide 34 text

/libproject/ build.gradle

Slide 35

Slide 35 text

Happy building • Write build.gradle & settings.gradle in project root directory • Write build.gradle in app / library project directory • Build: gradle assemble • /build/outputs/apk/

Slide 36

Slide 36 text

Build variant • Creating different versions of same application • free/paid • multi-apk • Build Variant = Build Type + Product Flavor

Slide 37

Slide 37 text

Product flavor • Defines a customised version of application build by the project • Free / Paid • Amazon / Play Store / blah store • Same type as defaultConfig

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Build • gradle assemblePaid • gradle aPD / gradle aPR • gradle assembleFree • gradle aFD / gradle aFR

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

All samples are here http:/ /github.com/shakalaca/ learning_gradle_android

Slide 42

Slide 42 text

References • Documentation • http:/ /tools.android.com/tech-docs/new-build-system (Guide / Samples) • http:/ /www.gradle.org/documentation • Forums • https:/ /plus.google.com/u/0/communities/114791428968349268860 • https:/ /groups.google.com/forum/#!forum/adt-dev • http:/ /stackoverflow.com/questions/tagged/gradle • Tool • http:/ /gradleplease.appspot.com/ • [email protected]:nenick/android-gradle-template.git

Slide 43

Slide 43 text

Or follow me @ • G+ • http:/ /google.com/profiles/shakalaca • Blog • http:/ /23pin.logdown.com • Taipei GDG (G+ Community) • https:/ /plus.google.com/communities/ 100566773212437391191

Slide 44

Slide 44 text

Q & A

Slide 45

Slide 45 text

Maven repository

Slide 46

Slide 46 text

Why & How ? • Time is money • Use aar instead of re-compiling everything • aar: Binary distribution of an Android Library Project • http:/ /tools.android.com/tech-docs/new- build-system/aar-format

Slide 47

Slide 47 text

Project layout

Slide 48

Slide 48 text

root-build.gradle

Slide 49

Slide 49 text

library/build.gradle

Slide 50

Slide 50 text

project/build.gradle • archivesBaseName • version

Slide 51

Slide 51 text

app/build.gradle

Slide 52

Slide 52 text

How • deploy library • ./gradlew deploy • use local repository • ./gradlew as -PuseRepo