Android(二)build.gradle文件解析

一个项目中存在两个build.gradle文件,一个是最外层目录下的,一个是app目录下的。

最外层的build.gradle文件

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

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

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

allprojects {
    repositories {
//        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

jcenter()   一个代码托管仓库,有很多Android开源项目,声明该代码,就可以引用jcenter上的开源项目了

calsspath声明了Gradle插件,用于表示来构建Android项目

app目录下的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.andan.helloworld"
        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'
}

第一行代码表示应用了一个插件,有两个值可选com.android.application(表示这是一个应用程序模块,可以直接运行)和com.android.library(表示这是一个库模块,只能作为代码库依赖于别的应用程序模块来运行)

android闭包:

  • compileSdkVersion 用于指定项目的编译版本
  • buildToolsVersion用于指定项目构建工具的版本
  • applicationId 指定项目的包名
  • minSdkVersion 项目兼容的最低Android版本
  • targetSdkVersion 当前使用的Android版本,经过测试的
  • versionCode  项目的版本号
  • versionName 项目版本名
  • testInstrumentationRunner  单元测试
  • buildTypes用于指定声场安装文件的相关配置
  • minifyEnabled 指定是否对项目中的代码进行混淆
  • proguardFiles 用于指定混淆时使用的规则文件(.txt是sdk目录下通用的混淆规则,第二个pro文件是当前项目的根目录下,特有的混淆规则)

dependencies闭包:可以指定当前项目所有的依赖关系(三种依赖关系:本地依赖,库依赖和远程依赖)

本地依赖:对本地的Jar包或目录添加依赖关系(compile fileTree)

库依赖:对项目中的库模块添加依赖关系(compile project  加上依赖的库名称)

远程依赖:对jcenter库上的开源项目添加依赖关系(compile com.android.support:********)

                       

猜你喜欢

转载自blog.csdn.net/andanwubian/article/details/81144463