全面认识Android中Gradle相关配置文件

一、简介

自从使用Android Studio(简称AS) 开发Android应用程序,Gradle就突然出现在我们面前,AS就是使用Gradle来构建编译Android应用程序的,因此有关Gradle的配置,作为开发人员应当有一定的了解。

使用AS创建一个Android应用程序,我们可以看到在project下,有settings.gradle和build.gradle文件,在gradle/wrapper文件夹下可以看到gradle-wrapper.properties文件,在app module下看到build.gradle文件,本篇文章将会对这些文件的作用以及文件中相关配置做详细讲解。

二、Settings文件

在Gradle中定义了一个设置文件,主要用于初始化以及工程树的配置,改文件的默认名是setting.gradle,存放在根目录中也即android项目的根目录中。

在AS中,project相当于根工程,根工程中可以有很多子工程,也即相当于module,一个子工程只有在setting.gradle文件中配置了,gradle才会识别到。使用AS创建一个项目,我们可以看到setting.gradle文件内容大致如下:

include ':app'

当我们在创建一个aar module时,显示如下:

include ':app'
include ':module'

只有通过include添加了module,gradle才能识别module模块,构建是才能被包含进去。

三、根目录下的build.gradle文件

每个module都会有build.gradle文件,该文件是module构建的入口,可以在该文件中针对project进行写配置,例如版本、插件、依赖库等。那在项目的根目录下同样也存在一个build.gradle文件,这个文件是该项目的全局构建脚本,因此可以做一下子module中都会用到的一些工作,例如gradle插件版本、依赖的Maven中心库等。文件大致内容如下:

// gradle脚本执行所需依赖
// The "buildscript" configuration section is for gradle itself 
// (i.e. changes to how gradle is able to perform the build). 
// So this section will usually include the Android Gradle plugin
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        // gradle插件版本
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

// 项目本身需要的依赖
// The "allprojects" section is for the modules being built by Gradle.
allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

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

Oftentimes the repository section is the same for both, since both will get their dependencies from jcenter usually (or maybe maven central). But the "dependencies" section will be different.

  • dependencies

Usually the "dependencies" section for "allprojects" is empty since the dependencies for each module are unique and will be in the "build.gradle" file within each of the modules. However, if all of the modules shared the same dependencies then they could be listed here.

四、gradle-wrapper.properties

每一个用gradle编译的工程,都会有一个gradle\wrapper目录。该目录下有2个文件:gradle-wrapper.jar和gradle-wrapper.properties。

gradle-wrapper.jar是Gradle Wrapper的主体功能包。在Android Studio安装过程中产生gradle-wrapper.jar,然后每次新建项目,会将gradle-wrapper.jar拷贝到你的项目的gradle/wrapper目录中。

gradle-wrapper.properties文件主要指定了该项目需要什么版本的Gradle,从哪里下载该版本的Gradle,下载下来放到哪里,具体内容如下:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
// GRADLE_USER_HOME 一般指~/.gradle, windows下一般是 C:\Users\Administrator\.gradle
zipStoreBase=GRADLE_USER_HOME
// 下载的gradle具体存放到哪个目录下
zipStorePath=wrapper/dists
// 需要下载的gradle版本,也即项目中使用到的gradle版本
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

五、Module下的build.gradle文件

Module下build.gradle文件中的配置只针对Module起作用,创建一个android应用程序,我们可以在app下看到build.gradle文件内容大致如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion '25.0.3'

    defaultConfig {
        applicationId "com.cn.example"
        minSdkVersion 21
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        // 设置MultiDex可用
        multiDexEnabled true

        ndk {
            //设置支持的SO库架构
            abiFilters 'armeabi' , 'armeabi-v7a'//, 'armeabi-v7a', 'x86_64', 'arm64-v8a'
        }


        jackOptions {
            enabled false
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    dexOptions {
        javaMaxHeapSize "4g"
    }

}

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:24.0.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'

    compile 'io.reactivex:rxandroid:1.1.0'

}
  • compileSdkVersion

compileSdkVersion 24 是配置我们编译android工程的sdk,24是android sdk的API Level。学习了Groovy基础不难看出 compileSdkVersion为一个函数,24是函数的入参。

  • buildToolsVersion

buildToolsVersion '25.0.3' 表示使用的android构建工具的版本。对应本地电脑sdk目录里的build-tools文件加下的工具包,包括appt、dex等工具。

  • defaultConfig

defaultConfig 表示默认配置。

defaultConfig {
    // 我们项目的包名
    applicationId "com.cn.example"
    // 编译的apk最低支持的android系统API版本
    minSdkVersion 21
    // 编译apk的目标android系统API版本
    targetSdkVersion 24
    // APP应用内部版本号,一般用于控制APP升级
    versionCode 1
    // APP应用的版本号名称
    versionName "1.0"
}
  • buildTypes

buildTypes表示构建类型,可分为release和debug两种模式;

minifyEnabled表示是否为该构建类型启用混淆;

proguardFiles表示当我们启用混淆时所使用的混淆配置文件。

未完待续。。。

猜你喜欢

转载自blog.csdn.net/u010349644/article/details/81488293
今日推荐