Gradle Cheat Sheet (14): The Default Android Build Scripts

  新建一个Android App工程,可以看到Android Studio会自动生成Gradle脚本。

1. build.gradle

  先看项目根目录的build.gradle,里面首先指定了JCenter作为Repository,为classpath 添加了依赖com.android.tools.build:gradle ,这是Gradle的Android插件。

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.1.2' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } }
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'

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

  然后在allprojects 下指定了JCenter作为Repository,使JCenter成为所有Gradle子项目的Repository。

allprojects {
repositories {
jcenter()
}
}
allprojects { repositories { jcenter() } }
allprojects {
    repositories {
        jcenter()
    }
}

2. /app/build.gradle

  在/app下也有一个build.gradle,专门用于构建Android App。开头首先应用了Android插件:

apply plugin: 'com.android.application'
apply plugin: 'com.android.application'
apply plugin: 'com.android.application'

2.1. android Block

  对Android插件的配置位于android{} 块中:

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.nex3z.examples.shakedetector"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
android { compileSdkVersion 23 buildToolsVersion "23.0.3" defaultConfig { applicationId "com.nex3z.examples.shakedetector" minSdkVersion 16 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } }
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.nex3z.examples.shakedetector"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

其中只有compileSdkVersion 和buildToolsVersion 是必须的。defaultConfig{} 块用于配置Android Manifest的属性。buildTypes{} 块给出不同构建类型的定义。

  这里buildTypes{} 中给出了release 的配置,设置minifyEnabled 为false关闭Code Shrinking,并添加proguardFiles 。Android Studio会自动创建release 和debug 两个Build Type,这里虽然没有在buildTypes{} 中给出debug 的配置,实际上是存在一个名为debug 的Build Type的。在Gradle的任务列表中,可以找到对应每个Build Type的任务,如assembleRelease 对应release ,assembleDebug 对应debug 。

2.2. dependencies Block

  在build.gradle的最后,由dependencies{} 块给出依赖,首先包含了libs文件夹下的所有.jar文件,然后是JUnit和Android support library:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
}
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.4.0' }
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
}

 

  本部分的完整代码可以在这里找到。