标签:
二.为什么使用gradle?
更容易重用资源和代码;
可以更容易创建不同的版本的程序,多个类型的apk包;
更容易配置,扩展;
更好的IDE集成;
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
//构建过程依赖的仓库
repositories {
jcenter()
}
//构建过程需要依赖的库
dependencies {//下面声明的是gradle插件的版本
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
}
}
//这里面配置整个项目依赖的仓库,这样每个module就不用配置仓库了
allprojects {
repositories {
jcenter()
}
}//声明插件,这是一个android程序,如果是android库,应该是com.android.library
apply plugin: ‘com.android.application‘
android {//安卓构建过程需要配置的参数
compileSdkVersion 21//编译版本
buildToolsVersion "21.1.2"//buildtool版本
defaultConfig {//默认配置,会同时应用到debug和release版本上
applicationId "com.taobao.startupanim"//包名
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {//这里面可以配置debug和release版本的一些参数,比如混淆、签名配置等
release {//release版本
minifyEnabled false//是否开启混淆
proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘//混淆文件位置
}
}
}
dependencies {//模块依赖
compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])//依赖libs目录下所有jar包
compile ‘com.android.support:appcompat-v7:21.0.3‘//依赖appcompat库
}defaultConfig中是一些基本配置,它会同时应用到debug/release版本上,下面列举了所有可配项及对应的值:include ‘:module-a‘,‘:module-b‘
maven{
url "..."
}
ivy{
url "..."
}
flatDir{
dirs ‘xxx‘
}有一些仓库提供了别名,可直接使用:repositories{
mavenCentral()
jcenter()
mavenLocal()
}assemble 构建项目输出 check 运行检测和测试任务 build 运行assemble和check clean 清理输出任务执行任务可以通过gradle/gradlew+任务名称的方式执,执行一个顶级任务会同时执行与其依赖的任务,比如你执行
gradlew assemble它通常会执行:
gradlew assembleDebug gradlew assembleRelease
chmod +x gradlew ./gradlew assemble可以通过:
gradlew tasks列出所有可用的任务。在Android Studio中可以打开右侧gradle视图查看所有任务。
compile files(‘libs/xxx.jar‘)如果libs下有多个jar文件,可以这样声明:
compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])
compile ‘com.android.support:appcompat-v7:21.0.3‘可见,格式为
compile ‘groupId:artifactId:version‘
compile project(‘:module-A‘)并且你需要在settings.gradle中把module-A模块包含进来:
include ‘:module-A‘,‘:app‘此外,这种情况下module-A模块是作为库存在的,因而它的build.gradle中的插件声明通常应该是这样的:
apply plugin: ‘com.android.library‘而且,作为library的模块module-A的build.gradle文件的defaultConfig中是不允许声明applicationId的,这点需要注意。
repositories{
maven{
url="http://mvnrepo.xxx.com"
}
}compile ‘com.aaa.xxx:core:1.0.1@aar‘
apply plugin : ‘com.android.library‘然后你可以在命令行中进到项目目录,执行如下gradle任务:
gradlew assembleRelease//确保该目录下有gradlew文件生成的aar在/build/output/aar文件夹中
repositories{
flatDir{
dirs ‘libs‘
}
}最后在dependencies结点下依赖该aar模块:dependencies{
compile (name:‘xxx‘,ext:‘aar‘)
}compile (group:‘xxx‘,name:‘xxx‘,version:‘xxx‘){
exclude group:‘xxx‘,module:‘xxx‘//module对应的就是artifactId
}multiDexEnabled true接着,在dependencies结点下增加如下依赖:
dependencies{
compile ‘com.android.support:multidex:1.0.0‘
}buildTypes{
release{
minifyEnabled true
shrinkResources true
}
}android{
lintOptions{
abortOnError false
}
}compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}storeFiles:keystore文件存储位置,通常是.jks文件 storePassword 密码 keyAlias keystore别名 keyPassword 密码
signingConfigs {
//debug版本的签名配置,通常不用配,因为有默认的debug签名
debug {
}
release {
storeFile file("key.jks")
storePassword "123456"
keyAlias "mykey"
keyPassword "123456"
}
}signingConfig android.signingCongfigs.debug位置为
${home}\.android\debug.keystore然后在buildTypes结点下的对应版本中添加上面的配置:buildTypes{
release{
signingConfig signingConfigs.release
}
}当然,release不是固定的名称,你可以随便取,比如这样:android {
signingConfigs {
debug {
storeFile file("debug.keystore")
}
myConfig {
storeFile file("other.keystore")
storePassword "android"
keyAlias "androiddebugkey"
keyPassword "android"
}
}
buildTypes {
foo {
debuggable true
jniDebuggable true
signingConfig signingConfigs.myConfig
}
}
}RELEASE_STOREFILE=xxx.jks RELEASE_STORE_PASSWORD=123456 RELEASE_KEY_ALIAS=mykey RELEASE_KEY_PASSWORD=123456然后直接引用即可:
storeFile file(RELEASE_STOREFILE) storePassword RELEASE_STORE_PASSWORD keyAlias RELEASE_KEY_ALIAS keyPassword RELEASE_KEY_PASSWORD
buildTypes{
release{
buildConfigField "string","type","\"release\""
}
debug{
buildConfigField "string","type","\"debug\""
}
}这样就会在BuildConfig类中生成type字段://build/generate/source/buildConfig/release/包名/ 路径下的BuildConfig.java public static final String type = "release" //build/generate/source/buildConfig/debug/包名/ 路径下的BuildConfig.java public static final String type = "debug"
标签:
原文地址:http://blog.csdn.net/chdjj/article/details/44975037