Gradle详解

1、gradle采用了Groovy语言,完全兼容maven和ivy。更多详细介绍可以看它的官网:http://www.gradle.org/。

上面那个是module下的build.gradle文件,下面那个是project下的build.gradle文件。这两个文件是有区别的,project下的build.gradle是基于整个project的配置,而module下的build.gradle是每个模块自己的配置。

什么叫maven呢?maven是Apach基金组织推出的一款工具,简单的说就是帮我们下载jar包、并且通过配置xml文件使jar包能被我们的Java代码引用的一个工具,我们用eclipse做Android开发时其实很少有用到maven,都是直接从网上下载个jar包拷到工程目录下用。那么在使用Gradle之后,我们在Android项目中也可以用maven了,想用什么jar包,直接填写在gradle配置文件中,jar包就会自动帮我们下载到工程中,还能通过配置让jar包时刻保持最新版本,如果你网络条件好的话,用maven还是比较方便的。

2、project下的build.gradle

// 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:2.1.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
//这里面配置整个项目依赖的仓库,这样每个module就不用配置仓库了
allprojects {
    repositories {
        jcenter()
    }
}
仓库repositories需要声明两次,它们作用不同,buildscript中的仓库是gradle脚本自身需要的资源,而allprojects下的仓库是项目所有模块需要的资源。

3、settings.gradle

include ':app'
这个文件是用来配置多模块的,比如你的项目有两个模块module-a,module-b,那么你就需要在这个文件中进行配置,格式如下:

include ':module-a',':module-b'
4、gradle仓库:

gradle有三种仓库,maven仓库,ivy仓库以及flat本地仓库。声明方式如下:

maven{
  url "..."
}
ivy{
  url "..."
}
flatDir{
  dirs 'xxx'
}
有一些仓库提供了别名,可直接使用:

repositories{
	mavenCentral()
	jcenter()
	mavenLocal()
}
5、module下的build.gradle

//声明插件,这是一个android程序,如果是android库,应该是com.android.library
apply plugin: 'com.android.application'

android {
    //安卓构建过程需要配置的参数
    compileSdkVersion 23//编译版本
    buildToolsVersion "23.0.2"//buildtool版本

    //默认配置,会同时应用到debug和release版本上
    defaultConfig {
        applicationId "com.example.allcontant"//包名
        minSdkVersion 15//最小版本号
        targetSdkVersion 23//最适版本号
        versionCode 1//app版本号
        versionName "1.0"//app版本名
    }

    //这里面可以配置debug和release版本的一些参数,比如混淆、签名配置等
    buildTypes {
        release {
            minifyEnabled false//是否开启混淆
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'//混淆文件位置
        }
    }
}

//模块依赖
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])//依赖libs目录下所有jar包
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'//依赖appcompat库
}
6、导入某个project:

你的app是多模块的,假设有两个模块app和module-A,并且app模块是依赖module-A的,这时候我们就需要在app模块的build.gradle中的dependencies结点下配置依赖:

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的。

7、声明三方maven仓库:

可能你项目需要的一些库文件是在你们公司的私服上,这时候repositories中仅有jcenter就不行了,你还需要把私服地址配到里面来,注意,应该配到project的build.gradle中的allprojects结点下或者直接配到某个模块中如果仅有这个模块用到。配置方式:

repositories{
     maven{
          url="http://mvnrepo.xxx.com"
     }
}
8、aar文件

  依赖三方aar文件:

compile 'com.aaa.xxx:core:1.0.1@aar'
  将库项目导出为aar:

首先你的项目必须是一个库项目,build.gradle中进行配置

apply plugin : 'com.android.library'
然后你可以在命令行中进到项目目录,执行如下gradle任务:

gradlew assembleRelease//确保该目录下有gradlew文件
生成的aar在/build/output/aar文件夹中。

  引用本地aar:

首先将aar文件放到模块的libs目录下,然后在该模块的build.gradle中声明flat仓库:

repositories {
    flatDir {
        dirs 'libs'
    }
}
再在dependencies结点下依赖该aar模块:

dependencies{
      compile (name:'xxx',ext:'aar')
}
9、排除依赖:

当出现依赖冲突的时候可以通过排除依赖解决,具体方式如下:

compile (group:'xxx',name:'xxx',version:'xxx'){
    exclude group:'xxx',module:'xxx'//module对应的就是artifactId
}
10、多dex支持(打包65k方法数限制)

首先在build.gradle的buildConfig中增加如下配置:

defaultConfig {
    multiDexEnabled true
}
接着,在dependencies结点下增加如下依赖:

dependencies{
	compile 'com.android.support:multidex:1.0.0'
}
最后,让你的Application继承MultiDexApplication,如果你的应用没有声明Application,可以在manifest文件的application结点下增加name属性,值为android.support.multidex.MultiDexApplication。

11、自动移除不用资源

可以在buildTypes结点中增加如下配置:

buildTypes{
     release{
           minifyEnabled true
           shrinkResources true
     }
}
12、忽略lint错误:

可以在build.gradle文件中的android结点下增加如下配置:

android{
     lintOptions{
           abortOnError false
     }
}
13、声明编译的java版本

可以在build.gradle文件中的android结点下增加如下配置:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}
14、应用签名配置

首先在module的build.gradle中增加这些字段:

storeFiles:keystore文件存储位置,通常是.jks文件
storePassword 密码
keyAlias keystore别名
keyPassword 密码
具体配置方式为:

signingConfigs {
    //debug版本的签名配置,通常不用配,因为有默认的debug签名
    debug {
    }
    release {
        storeFile file("key.jks")
        storePassword "123456"
        keyAlias "mykey"
        keyPassword "123456"
    }
}
注:

注:debug的默认签名为:
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
         }
    }
}
真实开发中,把密码配置到build.gradle中不是很好的做法,最好的做法是放在gradle.properties中:

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
15、定制buildConfig:

在build.gradle中配置:

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"



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值