Build Android with Gradle (Pre)

Gradle Plugin User Guide

首先,可以先參考這個連結Gradle: The New Android Build System下面的影片
並且下載的gradle,連結

# Mac install
brew search gradle
brew install gradle

Gradle plug-in to build Android applications

這部分的資訊可以到Android Tools Project Site參考他的 Release Note.
可以很詳細看到各個AS版本使用的Gradle版本

Ex :

  • 0.14.4 相容Gradle 2.1, 2.2 and 2.2.1
  • 0.13.0 Requires Gradle 2.1

也可以到Version Eye查看他的 Dependencies

PS : 在專案裡有兩個build.gradle, 分別在project以及app資料夾

Project build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// This only affects the code running the build, not the project
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.14.4'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
jcenter()
}
}
App build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
apply plugin: 'com.android.application'

android {
compileSdkVersion 21
buildToolsVersion "21.0.2"

defaultConfig {
applicationId "yume190.com.tester6"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.2'
compile 'com.google.code.gson:gson:2.3.1'
androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.2.1'
// provided 'org.robolectric:robolectric-gradle-plugin:0.14.0'
}
// local.properties
// 設定 Android SDK所在位置
// 或者可以設定環境變數 ANDROID_HOME
sdk.dir=/Users/yume/Library/Android/sdk

example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
productFlavors {
pro {
applicationId = "com.example.my.pkg.pro"
}
free {
applicationId = "com.example.my.pkg.free"
}
}

buildTypes {
debug {
applicationIdSuffix ".debug"
}
}

Source :