Gradle enabled android project

Preview:

DESCRIPTION

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

Citation preview

Gradle enabled android project

shakalaca @ Taipei GTUG

1

Outline• Gradle introduction

• Step by step tutorial

• Basic

• Dependency

• Sign / Proguard

• Variant

• Android Studio integration

• Q & A

3

Gradle

4

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

.. short version

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

6

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

command• format

• gradle <command>

• command

• tasks

• assemble

• assemble release

• assemble debug

• aR

9

output

• <project>/build/apk/

10

Basic

11

Eclipse project

12

13

build.gradle

• apply plugin: ‘android’

• dependencies

• compileSdkVersion

• buildToolsVersion

• defaultConfig

• sourceSets

14

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

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

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

defaultConfig

• versionCode

• versionName

• minSdkVersion

• targetSdkVersion

• packageName

• testPackageName

• testInstrumentationRunner

• signingConfig

• proguardFile

• proguardFiles

18

Dependency

19

Project structure

20

21

settings.gradle

• Used in multi-project hierarchy

• Define projects to be used in build

22

settings.gradle

23

settings.gradle

24

er .. no direct way ?

25

26

27

28

Sign release key

29

30

Build Types

• default 2 types: debug & release

• for debugging & signing

31

Build Types

• debuggable

• jniDebugBuild

• renderscriptDebugBuild

• renderscriptOptimLevel

• packageNameSuffix

• versionNameSuffix

• signingConfig

• zipAlign

• runProguard

• proguardFile

• proguardFiles

32

tips

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

• .. If you work alone

33

Proguard

34

35

Variant

36

Build variants

• Creating different versions of same application

• free/paid

• multi-apk

• Build Variant = Build Type + Product Flavor

37

Product Flavors

• Defines a customized version of application build by the project

• Free / Paid

• Amazon / Play Store / blah store

• Same type as defaultConfig

38

39

40

One more thing

41

gradle wrapper

• Setup gradle automatically

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

42

43

summry

• simple android project

• sign release key

• run proguard

• build variants

• wrapper

44

Android Studio integration

45

Android Studio integrationBETA

46

Survival guide

Rule #1Always backup your project.

47

Survival guide

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

48

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

R U KIDDING ME ?

50

51

Basic

52

53

configurations

54

55

import..

56

import..

57

import..

58

import..

59

Piece of cake

60

Dependency

61

project layout

62

configurations

63

... errrrrr

• Don’t recognize as library project

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

• But you can still build the project

64

duplicated name ?

65

try another way

66

errrrrrr

67

68

Refine project layout

69

working configurations

70

tips

• as simple as possible

• library/xxx_lib

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

• ugly, but runs fine

71

Local repository

72

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

project layout

74

root-build.gradle

75

library/build.gradle

76

project/build.gradle

• archivesBaseName

• version

77

app/build.gradle

78

How

• deploy library

• ./gradlew deploy

• use local repository

• ./gradlew as -PuseRepo

79

Tricks

80

rename apk

81

launch after install

82

move out apk

83

Q & A

85