gradle enabled android project

86

Click here to load reader

Upload: shaka-huang

Post on 26-Aug-2014

5.553 views

Category:

Self Improvement


4 download

DESCRIPTION

Learn to use gradle in android project You can find the samples here: https://github.com/shakalaca/learning_gradle_android

TRANSCRIPT

Page 1: Gradle enabled android project

Gradle enabled android project

shakalaca @ Taipei GTUG

1

Page 3: Gradle enabled android project

Outline• Gradle introduction

• Step by step tutorial

• Basic

• Dependency

• Sign / Proguard

• Variant

• Android Studio integration

• Q & A

3

Page 4: Gradle enabled android project

Gradle

4

Page 5: Gradle enabled android project

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/

5

Page 6: Gradle enabled android project

.. short version

An open source build system that combines the power of Ant & Maven

6

Page 7: Gradle enabled android project

Why gradle ?

• Powerful Build System

• Declarative, Flexible

• Imperative customization

• Tooling API

• Build System Toolkit

• Plugins create own DSL, APIs, IDE integration

• Free / Open Source

7

Page 9: Gradle enabled android project

command• format

• gradle <command>

• command

• tasks

• assemble

• assemble release

• assemble debug

• aR

9

Page 10: Gradle enabled android project

output

• <project>/build/apk/

10

Page 11: Gradle enabled android project

Basic

11

Page 12: Gradle enabled android project

Eclipse project

12

Page 13: Gradle enabled android project

13

Page 14: Gradle enabled android project

build.gradle

• apply plugin: ‘android’

• dependencies

• compileSdkVersion

• buildToolsVersion

• defaultConfig

• sourceSets

14

Page 15: Gradle enabled android project

dependencies

• compile project(‘:project1’)

• compile fileTree(dir: ‘libs’, include: ‘*.jar’)

• compile files(‘libs/android-support-v4.jar’)

• compile ‘com.android.support:support-v4:13.0.0’

15

Page 16: Gradle enabled android project

android packages

• com.android.support:support-v4:13.0.0

• com.android.support:support-v13:13.0.0

• com.android.support:gridlayout-v7:13.0.0

• com.google.android.gms:play-services:3.1.36

16

Page 17: Gradle enabled android project

android packages

• com.android.support:support-v4:18.0.0

• com.android.support:support-v13:18.0.0

• com.android.support:gridlayout-v7:18.0.0

• com.google.android.gms:play-services:3.2.25

17

Page 18: Gradle enabled android project

defaultConfig

• versionCode

• versionName

• minSdkVersion

• targetSdkVersion

• packageName

• testPackageName

• testInstrumentationRunner

• signingConfig

• proguardFile

• proguardFiles

18

Page 19: Gradle enabled android project

Dependency

19

Page 20: Gradle enabled android project

Project structure

20

Page 21: Gradle enabled android project

21

Page 22: Gradle enabled android project

settings.gradle

• Used in multi-project hierarchy

• Define projects to be used in build

22

Page 23: Gradle enabled android project

settings.gradle

23

Page 24: Gradle enabled android project

settings.gradle

24

Page 25: Gradle enabled android project

er .. no direct way ?

25

Page 26: Gradle enabled android project

26

Page 27: Gradle enabled android project

27

Page 28: Gradle enabled android project

28

Page 29: Gradle enabled android project

Sign release key

29

Page 30: Gradle enabled android project

30

Page 31: Gradle enabled android project

Build Types

• default 2 types: debug & release

• for debugging & signing

31

Page 32: Gradle enabled android project

Build Types

• debuggable

• jniDebugBuild

• renderscriptDebugBuild

• renderscriptOptimLevel

• packageNameSuffix

• versionNameSuffix

• signingConfig

• zipAlign

• runProguard

• proguardFile

• proguardFiles

32

Page 33: Gradle enabled android project

tips

• Save your gradle.properties in ~/.gradle/

• .. If you work alone

33

Page 34: Gradle enabled android project

Proguard

34

Page 35: Gradle enabled android project

35

Page 36: Gradle enabled android project

Variant

36

Page 37: Gradle enabled android project

Build variants

• Creating different versions of same application

• free/paid

• multi-apk

• Build Variant = Build Type + Product Flavor

37

Page 38: Gradle enabled android project

Product Flavors

• Defines a customized version of application build by the project

• Free / Paid

• Amazon / Play Store / blah store

• Same type as defaultConfig

38

Page 39: Gradle enabled android project

39

Page 40: Gradle enabled android project

40

Page 41: Gradle enabled android project

One more thing

41

Page 42: Gradle enabled android project

gradle wrapper

• Setup gradle automatically

• $ANDROID_HOME/tools/templates/gradle/wrapper/

42

Page 43: Gradle enabled android project

43

Page 44: Gradle enabled android project

summry

• simple android project

• sign release key

• run proguard

• build variants

• wrapper

44

Page 45: Gradle enabled android project

Android Studio integration

45

Page 46: Gradle enabled android project

Android Studio integrationBETA

46

Page 47: Gradle enabled android project

Survival guide

Rule #1Always backup your project.

47

Page 48: Gradle enabled android project

Survival guide

Rule #2Don’t import production code until 1.0 is out..

48

Page 49: Gradle enabled android project

Best practice ?

• Import modules manually

• pro

• short compile / AS startup time

• consider project with multiple libraries..

• con

• no variant builds (ok for most people)

49

Page 50: Gradle enabled android project

R U KIDDING ME ?

50

Page 51: Gradle enabled android project

51

Page 52: Gradle enabled android project

Basic

52

Page 53: Gradle enabled android project

53

Page 54: Gradle enabled android project

configurations

54

Page 55: Gradle enabled android project

55

Page 56: Gradle enabled android project

import..

56

Page 57: Gradle enabled android project

import..

57

Page 58: Gradle enabled android project

import..

58

Page 59: Gradle enabled android project

import..

59

Page 60: Gradle enabled android project

Piece of cake

60

Page 61: Gradle enabled android project

Dependency

61

Page 62: Gradle enabled android project

project layout

62

Page 63: Gradle enabled android project

configurations

63

Page 64: Gradle enabled android project

... errrrrr

• Don’t recognize as library project

• No typo checking, no function reference, no ...

• But you can still build the project

64

Page 65: Gradle enabled android project

duplicated name ?

65

Page 66: Gradle enabled android project

try another way

66

Page 67: Gradle enabled android project

errrrrrr

67

Page 68: Gradle enabled android project

68

Page 69: Gradle enabled android project

Refine project layout

69

Page 70: Gradle enabled android project

working configurations

70

Page 71: Gradle enabled android project

tips

• as simple as possible

• library/xxx_lib

• use ‘:’ instead of ‘/’ in path

• ugly, but runs fine

71

Page 72: Gradle enabled android project

Local repository

72

Page 73: Gradle enabled android project

What & Why ?

• 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

• Time is money

• DRY

73

Page 74: Gradle enabled android project

project layout

74

Page 75: Gradle enabled android project

root-build.gradle

75

Page 76: Gradle enabled android project

library/build.gradle

76

Page 77: Gradle enabled android project

project/build.gradle

• archivesBaseName

• version

77

Page 78: Gradle enabled android project

app/build.gradle

78

Page 79: Gradle enabled android project

How

• deploy library

• ./gradlew deploy

• use local repository

• ./gradlew as -PuseRepo

79

Page 80: Gradle enabled android project

Tricks

80

Page 81: Gradle enabled android project

rename apk

81

Page 82: Gradle enabled android project

launch after install

82

Page 83: Gradle enabled android project

move out apk

83

Page 85: Gradle enabled android project

Q & A

85