Android学习PC蛋蛋源码出租笔记之build.gradle文件

不同PC蛋蛋源码出租 dsluntan.com Q:3393756370 VX:17061863513于Eclipse,AS是通过Gradle来构建项目的。Gradle是一个非常先进的构建项目的工具,它使用了一种基于Groovy的领域特定语言DSL来声明项目设置,摒弃了传统基于XML(如Ant 和 Maven)的各种繁琐配置。

在一个项目中有两个bulid.gradle文件:

先来看看上面的:(这些代码都是自动生成的)

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

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

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

}

allprojects {
repositories {
jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}
首先,两处repositories的闭包中都声明了jcenter()这行配置,表示一个代码托管仓库,很多Android开源项目都会选择托管到jcenter上,声明了这行配置后,就可以在项目中轻松引用任何jcenter上的开源项目了。

接着,dependencies闭包中使用classpath声明了一个Gradle插件。此插件可以被用来创建Android项目,后面的2.3.3是插件的版本号;

再来看看下面的build.gradle文件:

apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "28.0.1"
defaultConfig {
applicationId "com.activitytest.testsearchlocation"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
我们来看看dependencies闭包即可:

compile fileTree就是一个本地依赖声明,它表示将libs目录下的所有.jar后缀的文件都添加到项目的构建路径中。

通常AS项目一共有3种依赖方式:本地依赖、库依赖、远程依赖。

其中本地依赖可以对本地的Jar包或目录添加依赖关系;库依赖可以对项目种的库模块添加依赖关系;远程依赖可以对jcenter库上的开源项目添加依赖关系。

后面的compile语句:

compile 'com.android.support:appcompat-v7:25.3.1'

是一种标准的远程库依赖的声明格式,其中com.android.support是域名部分,用于与其他公司的库做区分;appcompat-v7是组名称,用于和同一个公司中不同的库做区分;后面的是版本号。

加上这句声明后,Gradle在构建项目时会先检查一下本地是否已经有这个库的缓存,如果没有就自动联网下载,然后添加到项目的构建路径中。

而androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2'时声明测试用例库的。

猜你喜欢

转载自blog.51cto.com/13916010/2156827