您的位置:首页 > 产品设计 > UI/UE

Android官网:Configuring Gradle Builds英译

2015-12-29 14:52 483 查看
配置Gradle编译

编译配置基础内容

Android studio 的一个Project包含了一个build文件,另外每个module都包含了一个build文件。这些文件的文件名为build.gradle。它们原本是空白的文本,用了Groovy语法来配置Android提供的Gradle插件提供的元素。在大多数情况下,你只需要配置module里面的那个gradle文件,例如,BuildSystemGradle项目中的build文件,如下:

apply plugin: 'com.android.application'

android {
compileSdkVersion 19
buildToolsVersion "19.0.0"

defaultConfig {
minSdkVersion 8
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile project(":lib")
compile 'com.android.support:appcompat-v7:19.0.1'
compile fileTree(dir: 'libs', include: ['*.jar'])
}


apply plugin:’com.android.application’

将Gradle的Android插件用到了这个build中:添加了Android指定的构建任务到这个顶级的构建任务中,并且使得android {…}元素对于指定的Android元素是可用的

android {…}

配置了所有关于Android的构建选项。

compileSdkVersion :指定了编译的目标。

buildToolsVersion :指定了使用哪个版本的构建工具。可以使用SDK Manager来安装build tools的几个不同版本。Note:建议总是使用一个修订版本比你当前的编译目标版本或者目标SDK版本高的build tools版本。

defaultConfig :从构建系统中灵活的配置了清单文件中一些主要的核心内容和记录。在defaultConfig中配置的内容应用到所有的构建变量中,除非有些构建变量再次被复写。

buildTypes :控制了怎样去构建和打包你的app,默认情况下,构建系统定义了两个构建类型:debug和release。debug构建类型包含了调试的符号,使用debug key来进行签名。release构建类型不使用默认的debug key来进行签名。在这个demo中构建文件使用proGuard来配置发布版本。

dependencies :在android元素外面。这个元素指明了module所需要的依赖。dependecies在下列部分汇中被覆盖了。Note:When you make changes to the build files in your project, Android Studio requires a project sync to import the build configuration changes. Click Sync Now on the yellow notification bar that appears for Android Studio to import the changes.



依赖的声明

在这个demo中app这个module声明了三种依赖:

...
dependencies {
// Module dependency
compile project(":lib")

// Remote binary dependency
compile 'com.android.support:appcompat-v7:19.0.1'

// Local binary dependency
compile fileTree(dir: 'libs', include: ['*.jar'])
}


每种依赖在下面都有解释:

Module dependencies:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android