The Ins and Outs of Gradle(Gradle的来龙去脉)---- 学习笔记 1

原文链接地址为:http://code.tutsplus.com/tutorials/the-ins-and-outs-of-gradle--cms-22978


1. What is Gradle?

Gradle是一个自动化构建工具,可以通过插件,继承到不同的环境中。在Android studio中,继承Gradle的插件的名称是Android Gradle plugin。

Gradle可以做到这些事情:

1)新建项目的时候,极少需要配置。使用Android studio创建新项目的时候,就产生了一个默认的gradle设置。如果默认的实在不符合要求,gradle也很容易定制(how?).

2)  声明项目的依赖。可以依赖modules, JAR files 和libraries,这些依赖项可以是本地的,也可以是远程的。

3)测试项目。Gradle automatically generates a test directory and a test APK from your project's test sources and can run your tests during the build process. 

4)给apk签名

5)由一个单独的modules产生多个apk


2. Exploring the Gradle Files

Android studio创建一个新项目的时候,会自动产生多个必要的gradle文件。有以下几种gradle文件:

1)build.gradle.  使用Domain Specific Language(DSL)来定义编译逻辑,来和Android gradle插件的元素交互。Android studio 有两种级别的build.gradle文件,一种是工程级别的,对所有module都适用。另一种是module级别的,只对该module起作用。

工程级别的build.gradle只有一个,通常不需要改变这个文件。但有必要对这个文件的内容了解一下:

buildscript {
 
//Project-level Gradle build files use buildscript to define dependencies.//
//工程级别的gradle build,适用buildscript来定义依赖项 
    repositories {
 
        jcenter()
    }
 
//This file relies on the jJCenter repository.//
//这个文件依赖于jCenter库
    dependencies {
 
   classpath 'com.android.tools.build:gradle:1.0.0'
 
//Project is dependent on version 1.0.0 of the Android plugin for Gradle.// 
//工程依赖的Android gradle插件的版本是1.0.0 
    }
}
 
allprojects {
 
//Defines the dependencies required by your application.//
//定义应用的依赖 
    repositories {
        jcenter()
    }
}
 
//Application depends on the jCenter repository.//应用依赖于jCenter库

每一个module都有一个自己的build.gradle,下面是一个带注释的module级别的build.gradle

apply plugin: 'com.android.application'
 
//Since this project is an Android app, the build.gradle file utilises the Android plugin.//
//因为这个工程是Android的,build.gradle文件适用Android的gradle插件 
android {
 
//The following section configures all your project’s Android-specific parameters, and tells Gradle which version of Android it should build your project with. If you’ve developed Android applications before, the following should all be familiar.//
//接下来的部分配置Android的参数,告诉gradle适用哪个版本的Android来编译。如果你之前有开发过Android,下面的内容会很熟悉。 
    compileSdkVersion 21
 
//The API your project is targeting.// 
 
    buildToolsVersion "21.1.1"
 
The version of the build tools you want to use.//
 
    defaultConfig {
 
        applicationId "com.example.jessica.myapplication"
 
//Defines your application’s ID. Note, earlier versions of the Android plugin used ‘packageName’ instead of ‘applicationID.’//
 
        minSdkVersion 16
 
//The minimum API required by your project.//
 
        targetSdkVersion 21
 
//The version of Android you’re developing your application for.//
         
versionCode 1
 
        versionName "1.0"
    }
 
    buildTypes {
        release {
 
//‘BuildTypes’ controls how your app is built and packaged. If you want to create your own build variants, you’ll need to add them to this section.//
//buildType控制你的应用如何编译和打包。如果你要创建新的编译变量,你需要把变量加到这一节中。 
            minifyEnabled true
 
//Gradle runs ProGuard during the build process.//
//Gradle在编译的过程中,适用混淆 
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
 
//Applies the default ProGuard settings from the Android SDK.// 
//使用Android SDK没人的混淆设置 
        }
    }
}
 
dependencies {
 
//Declares the dependencies for the current module.//
//声明当前module的依赖库 
   compile fileTree(dir: 'libs', include: ['*.jar'])
 
//Although you can add JAR files by compiling them individually, this can be time-consuming if you have lots of JARs. In this example, we’re telling Gradle to add all the JAR files in the app/libs folder.// 
//虽然你可以逐个的添加jar文件来编译他们,如果有多个文件的话,就要花费很多时间。在这个例子中,我们告诉gradle添加app/libs文件夹下面的所有jar文件 
  compile 'com.android.support:appcompat-v7:21.0.3'
 
//To create more dependencies, add them to the depencies closure.//
//如果需要更多的依赖,把他们加进来 
}

2.其他的gradle文件。通常不需要更改其他的gradle文件,在你的工程发生改变的时候,通常他们都会自动更新。不过最好是了解一下这些文件的作用。

   1) gradle-wrapper.properties。这个文件允许其他人运行你的工程,即使他们没有安装gradle。这个文件检查Gradle是否安装,如果需要的话,还会下载需要的gradle版本。看如下gradle-wrapper.properties文件:

distributionBase=GRADLE_USER_HOME
 
//Determines whether the unpacked wrapper distribution should be stored in the project, or in the Gradle user home directory.//
//解压的根目录 
distributionPath=wrapper/dists
 
//The path where the Gradle distributions required by the wrapper are unzipped.//
//解压的相对目录 
zipStoreBase=GRADLE_USER_HOME
 
zipStorePath=wrapper/dists
 
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
 
//The URL where the correct version of Gradle should be downloaded from.//gradle的下载地址
  

2)settings.gradle。这个文件包含工程中的所有module。下面的例子只有一个module:app

include ':app'

 3) gradle.properties (Project Properties)。这个文件包含整个工程的配置信息。默认是空的。你在这里可以添加工程的很多属性


4)local.properties (SDK Location)这个文件高速gradle SDK的路径。由于每个人的配置不一样,这个文件不要版本控制。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值