Gradle 构建工具 的本质是 Java 应用程序 , 是运行在 Java 虚拟机 上的 ; 在空白的目录中 执行该 Gradle 构建工具 , 可以 生成一系列的构建文件 ;
之前在 Groovy 专栏 https://blog.csdn.net/shulianghan/category_11542621.html 学习了很多 Groovy 语法 , 涉及
等内容 ;
但是在写 build.gradle 构建脚本 时 , 发现上面的内容都用不上 , 基本配置中只涉及到调用方法 , 传入一个 闭包 , 在闭包中配置各种参数 ;
在构建脚本中配置使用的都是 DSL ( Domain Specific Language , 领域特定语言 ) , 在 build.gradle 构建脚本中使用的是 Groovy DSL , 该语言专门用于配置 Gradle 构建脚本 ;
在 构建脚本 中 , 同样也可以使用 Groovy 语言 或 Kotlin 语言 的普通语法进行脚本编写 ;
Android Studio 工程中 app 模块下的 build.gradle 构建脚本 都是如下样式的 :
android {
compileSdkVersion 31
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "kim.hsl.myapplication"
minSdkVersion 18
targetSdkVersion 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
settings.gradle 配置文件也是一个 Gradle 构建脚本 , 在其中也可以编写 Groovy 脚本执行 ;
settings.gradle 配置文件 是 Gradle 构建工程 的重要文件 , 其内容如下 :
rootProject.name = "My Application"
include ':app'
如果 Gradle 构建的工程中 , 涉及到 多个子工程 , 就需要在 settings.gradle 配置文件 中配置各个子工程模块 ; 配置时 , 使用
include ‘:模块名称’
的格式进行配置 ; 如 模块名称为 " app " , 则使用如下配置进行配置 ;
include ':app'
上述代码中 ':app'
表示的是 模块的路径 , 其含义相当于 './app'
;
app 模块如果不在这里进行配置 , 则 app 模块就会被 Gradle 作为普通目录进行处理 , 不会参与到构建编译打包的流程中 ; 如下图所示 , 如果不配置 include ':app'
, 左侧 app 目录就是普通的文件目录 ;
include ':app'
配置中 , 冒号 " : " 相当于 " / " 符号 , 如果想要在不同的目录创建 Module 模块 , 如 在 hello 目录下创建 Hello1 模块 , 则需要进行
rootProject.name = "My Application"
include ':app'
include ':hello:hello1'
配置 , 相当于配置了 ./hello/hello1
目录 ;
rootProject 配置 是 最顶层配置的信息 , rootProject.name 配置的是当前的工程名 ,
注意 , 不是目录名称 , 该工程的目录名称是 " D:\002_Project\002_Android_Learn\MyApplication2 " ;