《第一行代码Android》(第二版)读书笔记(一):详解build.gradle文件

来源:互联网 发布:网络歌曲推荐 编辑:程序博客网 时间:2024/05/20 02:26

开发环境:

jdk:jdk8或者以上版本
Android SDK:即Android开发工具包
集成开发工具:Android Studio

一、详解build.gradle文件

Android Studio采用Gradle来构建项目。Gradle是一个非常先进的项目构建工具,它使用了一种基于Groovy的领域特定语言(DSL)来声明项目设置,摒弃了传统基于XML(如Ant和Maven)的各种繁琐配置。在Android Studio中Android项目有两个build.gradle文件,一个是最外层目录下,一个是在app目录下。相关介绍具体如下:


1、最外层目录下的build.gradle文件,代码如下所示:
buildscript {    repositories {        jcenter()    }    dependencies {        classpath 'com.android.tools.build:gradle:2.2.3'    }}allprojects {    repositories {        jcenter()    }}task clean(type: Delete) {    delete rootProject.buildDir}
其中,
(1)jcenter是一个代码托管仓库,Android开源项目都会选择将代码托管到jcenter上,有了这行声明,之后就可以在项目中轻松引用任何jcenter上的开源项目了;
(2)classpath声明gradle插件,因为gradle插件并不是专门为构建Android项目而开发,Java,C++等很多种项目可以使用gradle来构建。因此想使用它来构建Android项目,则需要声明com.android.tools.build:gradle:2.2.0这个插件,其中最后的部分是插件的版本号。
注:通常情况下并不需要修改这个文件的内容,除非想添加一些全局的项目构建配置。
2、app目录下的build.gradle文件,代码如下所示:
//这是一个应用程序模块apply plugin: 'com.android.application'//Android闭包,在其中配置项目构建的各种属性android {    compileSdkVersion 25   //指定项目的编译版本    buildToolsVersion "25.0.1"  //指定项目构建工具的版本    //defaultConfig闭包,它可以对项目的更多细节进行配置    defaultConfig {        applicationId "com.example.pathdemo"  //指定项目的包名        minSdkVersion 16  //指定项目最低兼容的Android版本        targetSdkVersion 25  //指定的值表示你在该项目标版本上已经做过了充分的测试,系统将会为你的应用程序启动一些最新的功能和特性        versionCode 1        versionName "1.0"        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"    }    /**buildTypes闭包,用于指定生成安装文件的相关配置     *通常只会有两个子闭包:     * release:用于指定生成正式版安装文件的配置     * 和debug,指定生成测试版安装文件的配置     * 其中debug子闭包可以忽略不写     * */    buildTypes {        release {            minifyEnabled false  //指定是否对项目的代码进行混淆            //指定混淆时使用的规则文件            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }}//dependencies闭包dependencies {    //本地依赖声明,将libs目录下所有.jar后缀的文件都添加到项目构建路径中去    compile fileTree(include: ['*.jar'], dir: 'libs')        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {        exclude group: 'com.android.support', module: 'support-annotations'    })    /**远程依赖声明,gradle在构建项目时首先检查一下本地是否已经有这个库的缓存,     * 如果没有,则会去自动联网下载,然后再添加到项目的构建路径中     *其中com.android.support是域名部分,和其它公司的库进行区分,     * appcompat-v7是组名称,和同一公司不同的库进行区分,     *25.0.1是版本号,同一个库不同版本进行区分     **/    compile 'com.android.support:appcompat-v7:25.0.1'    //声明测试用例库    testCompile 'junit:junit:4.12'    //库依赖,基本格式: compile project后面加上要依赖的库名称    compile project(':pathanimlib')}
通常Android Studio项目有三种依赖方式:本地依赖,库依赖,远程依赖
本地依赖:可以对本地的jar包或者目录添加依赖关系
库依赖:可以对项目中的库模块添加依赖关系
远程依赖:可以对jcenter库上的开源项目添加依赖关系
注意:应用程序模块和库模块最大的区别在于,一个是可以直接运行的,一个只能作为代码的库依附于别的应用程序模块来运行。
库模块
apply plugin: 'com.android.library'
应用程序模块
apply plugin: 'com.android.application'

二、添加库模块依赖(案例)

步骤:
(1)构建Android项目
(2)导入库模块,File——New——Import Module
(3)添加库模块依赖,File——Project structrue——app——Dependencies——Module dependency——选择导入的库模块——OK
其中,确保库模块的compileSdkVersion 、buildToolsVersion 、minSdkVersion 、targetSdkVersion 和Android项目的app模块(即应用程序模块)一致。

源代码下载:

链接:http://pan.baidu.com/s/1slMAa4X 密码:u6m5

三、添加远程依赖(案例)

(1)Android开源项目(例如:arcgis runtime for Android SDK)都会选择将代码托管到jcenter上,在Android项目的最外层build.gradle上;
buildscript {    repositories {        jcenter()    }    dependencies {        classpath 'com.android.tools.build:gradle:2.2.3'        // NOTE: Do not place your application dependencies here; they belong        // in the individual module build.gradle files    }}allprojects {    repositories {        jcenter()        // Add the following arcgis repository        maven {            url'http://dl.bintray.com/esri/arcgis'        }    }}task clean(type: Delete) {    delete rootProject.buildDir}
(2)在项目中轻松引用任何jcenter上的开源项目了,app的build.gradle上;
dependencies {    compile fileTree(include: ['*.jar'], dir: 'libs')    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.0.1'    testCompile 'junit:junit:4.12'    compile 'com.esri.arcgisruntime:arcgis-android:100.0.0' }
这样就可以android项目中调用arcgis runtime for Android sdk中的类了。





阅读全文
0 0