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

关于Android Studio里的Gradle,你所需要知道的都在这里了

2016-01-27 18:34 585 查看
Gradle介绍

Gradle是一个先进的build toolkit,可以方便的管理依赖包和定义自己的build逻辑。到底有多先进,Android Studio官方集成Gradle,Google还专门写了Android Plugin for Gradle,你们感受一下。

基础配置

Android Studio中有一个顶级的build.gradle文件,每一个module还有一个自己的build.gradle。这个文件是使用Groovy语法和Android Plugin for Gradle元素的配置文件。通常我们只需要修改module的build文件就可以了。 下面是一个简单的例子

apply plugin: 'com.android.application'

android {
compileSdkVersion 19
buildToolsVersion "19.0.0"

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

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'])
}


第一句apply plugin: ‘com.android.application’说明在这个build文件中使用Android plugin for Gradle,有了这一句后,那些针对Android的元素才有意义。比如android {…}这个就是配置所有针对Android项目build时的选项。

compileSdkVersion: 编译时用的sdk版本

buildToolsVersion: build工具的版本

defaultConfig: 动态的在build时配置AndroidManifest.xml里的项目,defaultConfig里的配置可以覆盖manifest里的配置。

buildTypes: 配置如何构建和打包你的App,默认有debug和release两个类型。debug类型包含调试时的信息,并且有debug key签名。release默认是不含签名的。本例中release版本用了ProGuard。

dependencies:android {…}这个元素之外,配置此模块的依赖。

依赖

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'])
}


模块依赖: compile project(“:lib”),表明本模块依赖于lib模块,编译的时候系统会把lib模块包含进来

远程的二进制依赖: compile ‘com.android.support:appcompat-v7:19.0.1’,当本地没有此依赖时会默认从Maven Central Repository下载相应的依赖。从哪里下载可以在顶级的build文件中配置。

*本地二进制依赖:**compile fileTree(dir: ‘libs’, include: [‘.jar’]),把jar包拷贝到/libs文件夹下,compile fileTree(dir: ‘libs’, include: [‘*.jar’])意思是,app/libs下的所有jar包都包含进来

ProGuard

ProGuard的作用是在byte级别对你的app进行分析优化,使得你的App变得更小,更快。值得一提的是,当你使用某些开源项目时,他们会提醒你把一些包排除在ProGuard里,防止出错。

android {
...
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}


getDefaultProguardFile(‘proguard-android.txt’)获得默认的ProGuard配置。每个模块也都有模块级别的ProGuard规则,在proguard-rules.pro这个文件里,当然你也可以自己配置ProGuard规则。关于ProGuard的详细配置,请参考ProGuard Manual

Application ID

Application ID是用来唯一标识要发行的应用的。在build.gradle里设置。

defaultConfig {
applicationId "com.example.my.app"
minSdkVersion 15
targetSdkVersion 19
versionCode 1
versionName "1.0"
}


系统允许你对不同的产品类型设定不同的id,比如免费版和高级版。

productFlavors {
pro {
applicationId = "com.example.my.pkg.pro"
}
free {
applicationId = "com.example.my.pkg.free"
}
}

buildTypes {
debug {
applicationIdSuffix ".debug"
}
}


配置签名

debug版本与release版本的区别在于是否可以在一个安全的设备上debug和APK如何被签名的。在debug版本中,系统会提供一个默认的key来签名和已知的证书来授权App,避免在构建的时候出现密码提示。但是在release版本中,除非你主动提供一个key,不然系统是不会构建此项目的。

Release签名步骤

创建keystore。keystore是一个包含一系列私钥的二进制文件,这个文件必须妥善保管。

创建一个私钥。一个私钥代表着一个app实体。

在build文件中配置

android {
...
defaultConfig { ... }
signingConfigs {
release {
storeFile file("myreleasekey.keystore")
storePassword "password"
keyAlias "MyReleaseKey"
keyPassword "password"
}
}
buildTypes {
release {
...
signingConfig signingConfigs.release
}
}
}


4.在Android Studio中启动assembleRelease。app/build/apk/app-release.apk这个文件就是包含你release key的apk了。 注:一般不会直接在build文件中写入密码的,密码信息可以写在环境变量中

storePassword System.getenv("KSTOREPWD")
keyPassword System.getenv("KEYPWD")


或者用命令行输入

storePassword System.console().readLine("\nKeystore password: ")
keyPassword System.console().readLine("\nKey password: ")


在Android Studio中配置签名

菜单栏里,点击Build > Generate Signed APK.

在弹出的窗口里点击Create new

填写相关信息



Build Type 选择release



到此为止,你就可以去应用市场发布你的应用啦 ^_^

Build变量

定义productFlavors

有时你需要对同一个App发行不同的版本,如免费版、付费版之类的。这时你就需要用到productFlavors了。

android {
...
defaultConfig { ... }
signingConfigs { ... }
buildTypes { ... }
productFlavors {
demo {
applicationId "com.buildsystemexample.app.demo"
versionName "1.0-demo"
}
full {
applicationId "com.buildsystemexample.app.full"
versionName "1.0-full"
}
}
}


productFlavors里的配置会覆盖defaultConfig里的配置,如上配置后,系统会为每一个不同版本使用一个不同的id。

给每一个版本添加对应的资源

当你有不同的版本的时候,不同版本之间肯定有不同,所以需要添加不同的资源。以demo版为例,在src目录下新建以下目录



把需要的Activity,String.xml等资源加入到对应文件夹下。 在IDE左侧的Build Variants窗口里就可以看到如下四个Build变量



这样你需要哪个版本,直接选择就可以了。

最后

看到这里,再次打开build.gradle这个文件也不是感觉也没那么复杂了?是不是更爱Android Studio了?如有什么问题欢迎留言讨论。喜欢的话别忘了点个“顶”啊 ♪(^∀^●)ノ

转载请注明:Android开发中文站 » 关于Android Studio里的Gradle,你所需要知道的都在这里了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: