您的位置:首页 > 移动开发 > Android开发

android studio gradle介绍

2016-02-29 10:12 501 查看
什么是Gradle

Gradle是一种依赖管理工具,基于Groovy语言,面向Java应用为主,它抛弃了基于XML的各种繁琐配置,取而代之的是一种基于Groovy的领域特定(DSL)语言。Android Studio中新建项目成功后自动下载Gradle。 Gradle有几个基本组件:

1.整个项目的gradle配置文件build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'

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

allprojects {
repositories {
mavenCentral()
}
}


内容主要包含了两个方面:一个是声明仓库的源,我这里用的是mavenCentral(), jcenter可以理解成是一个新的中央远程仓库,兼容maven中心仓库,而且性能更优。另一个是声明了android gradle plugin的版本,android studio 1.1正式版必须要求支持gradle plugin 1.1的版本。

2.app文件夹下这个Module的gradle配置文件,也可以算是整个项目最主要的gradle配置文件

文件开头apply plugin是最新gradle版本的写法,以前的写法是apply plugin: ‘android’, 如果还是以前的写法,请改正过来。
apply plugin: 'com.android.application'

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'

}
}

//buildToolsVersion这个需要你本地安装该版本才行,很多人导入新的第三方库,失败的原因之一是build version的版本不对,这个可以手动更改成你本地已有的版本或者打开 SDK Manager 去下载对应版本。
android {
compileSdkVersion 17
buildToolsVersion "21.1.2"

//applicationId代表应用的包名,也是最新的写法,这里就不在多说了。
applicationId "com.lippi.recorder"
minSdkVersion 15
targetSdkVersion 17
versionCode 1
versionName '1.4'

// dex突破65535的限制
multiDexEnabled true
// AndroidManifest.xml 里面UMENG_CHANNEL的value为 ${UMENG_CHANNEL_VALUE}
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "channel_name"]
}

sourceSets {
main {
manifest.srcFile 'src/main/AndroidManifest.xml'
java.srcDirs = ['src/main/java']
resources.srcDirs = ['src/main/resources']
aidl.srcDirs = ['src/main/aidl']
renderscript.srcDirs = ['src/maom']
res.srcDirs = ['src/main/res']
assets.srcDirs = ['src/main/assets']
jniLibs.srcDir 'src/main/jniLibs'
}

// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')

// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
//执行lint检查,有任何的错误或者警告提示,都会终止构建,我们可以将其关掉。
lintOptions {
abortOnError false
}

//签名
signingConfigs {
debug {
storeFile file("/home/lippi/.android/debug.keystore")
}
relealse {
//这样写就得把demo.jk文件放在项目目录
storeFile file("recorder.jks")
storePassword "recorder"
keyAlias "recorder"
keyPassword "recorder"
}
}

buildTypes {
debug {
// 显示Log
buildConfigField "boolean", "LOG_DEBUG", "true"

versionNameSuffix "-debug"

//minifyEnabled(混淆)也是最新的语法,很早之前是runProguard,这个也需要更新下。

minifyEnabled false
zipAlignEnabled false
shrinkResources false
signingConfig signingConfigs.debug
}

release {
// 不显示Log
buildConfigField "boolean", "LOG_DEBUG", "false"
//混淆
minifyEnabled true
//Zipalign优化
zipAlignEnabled true

// 移除无用的resource文件
shrinkResources true
//前一部分代表系统默认的android程序的混淆文件,该文件已经包含了基本的混淆声明
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard.cfg'
//签名
signingConfig signingConfigs.relealse
}
}
//渠道Flavors,配置不同风格的app
productFlavors {
GooglePlay {}
xiaomi {}
umeng {}
_360 {}
baidu {}
wandoujia {}
}
//批量配置
//proguardFiles这部分有两段,前一部分代表系统默认的android程序的混淆文件,该文件已经包含了基本的混淆声明,免去了我们很多事,这个文件的目录在 /tools/proguard/proguard-android.txt , 后一部分是我们项目里的自定义的混淆文件,目录就在 app/proguard-rules.txt , 如果你用Studio 1.0创建的新项目默认生成的文件名是 proguard-rules.pro , 这个名字没关系,在这个文件里你可以声明一些第三方依赖的一些混淆规则,后面会具体讲到。
productFlavors.all { flavor ->
flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = outputFile.name.replace(".apk", "-${defaultConfig.versionName}.apk")
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
//compile project(‘:extras:ShimmerAndroid’)这一行是因为项目中存在其他Module,你可以理解成Android Library,由于Gradle的普及以及远程仓库的完善,这种依赖渐渐的会变得非常不常见,但是你需要知道有这种依赖的。
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'org.apache.commons:commons-math:2.1'
compile 'org.slf4j:slf4j-log4j12:1.7.5'
}
}


include ‘:recorder’

文件中recorder是项目的module,如果还有其他module按照相同的格式加上去。

Gradle多渠道打包

由于国内Android市场众多渠道,为了统计每个渠道的下载及其它数据统计,就需要我们针对每个渠道单独打包,如果让你打几十个市场的包岂不烦死了,不过有了Gradle,这再也不是事了。 以友盟统计为例,在AndroidManifest.xml里面会有这么一段:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: