模块级build文件
在Android开发之Gradle基础一文中介绍了基本的安卓的项目结构与任务的定义,项目的依赖等相关基础内容,但是仅仅介绍了项目级别的build文件,在Android开发中App模块下的build文件也非常重要,它可以用于为其所在的特定模块配置 build 设置。可以通过配置这些 build 设置提供自定义打包选项(如额外的 build 类型和产品变种)等。
配置全局版本控制
在项目中不同的模块经常会导入很多的依赖,有些相同的依赖我们要尽可能的保持版本一致,一方面方便维护,另一方面为了防止依赖冲突的发生,因此我们需要定义全局的一些变量来进行依赖版本管理,我们可以在工程的build文件中添加 ext 闭包,来设置我们的全局变量。
ext {
BUILD_SDK_VERSION = 31
TARGET_SDK_VERSIOn = 31
MIN_SDK_VERSION = 23
KOTLIN_VERSION = "1.5.20"
}
在子模块中使用这些定义的变量:
rootProject.ext.BUILD_SDK_VERSION
build文件
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.example.geadlelearning5'
compileSdk 33
defaultConfig {
applicationId "com.example.geadlelearning5"
minSdk 24
targetSdk 33
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_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = '11'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
plugins块
构建配置中的第一行将 Android Gradle 插件 应用到这个构建中,并使 Android 块可用于指定Android 特定的构建选项。
defaultConfig块
DefaultConfig 块封装了所有 build 变量的默认设置和条目,并且可以从构建系统动态地覆盖AndroidManifest.xml中的一些属性。您可以配置产品风味来覆盖不同版本的应用程序的这些值。
buildTypes块
BuildTypes 块是您可以配置多个生成类型的地方。默认情况下,构建系统定义两种构建类型: debug和 release。默认生成配置中没有显式显示调试生成类型,但是它包含调试工具,并且使用调试键进行签名。Release 构建类型应用 ProGuard(混淆) 设置,默认情况下不进行签名。
productFlavors块
ProductFlavor 块是您可以配置多种产品风味的地方。这可以让你创建不同版本的应用程序,可以覆盖 defaultConfig 块与他们自己的设置。产品风格是可选的,构建系统在默认情况下不会创建它们。
dependencies块
模块级构建配置文件中的依赖项块指定仅构建模块本身所需的依赖项。
渠道化配置
在日常开发中,往往会根据产品的需求进行不同渠道的相关配置。例如某个渠道使用到了特殊的SDK,或者某个渠道的命名或者配置不一样的,此时我们就可以利用productFlavors来创建我们不同的配置了。
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
// com.android.application的节点,配置所有特殊build选项的地方
android {
// 应用程序的名称空间,主要用于访问应用程序资源
namespace 'com.example.gradlelearning3'
//编译的SDK版本
compileSdk rootProject.ext.BUILD_SDK_VERSION
// ProductFlavor:根据不同的需求对编译进行独特的设置,一般针对不同的渠道,用于渠道化
// defaultConfig:默认的产品风味
defaultConfig {
applicationId "com.example.gradlelearning3" //应用包名,唯一身份识别 package = com.example.gradlelearning3
// applicationIdSuffix "ABC" //给包名添加后缀 package = com.example.gradlelearning3.ABC
minSdk rootProject.ext.MIN_SDK_VERSION //最小支持的安卓运行版本
targetSdk rootProject.ext.TARGET_SDK_VERSIOn //针对开发的使用的SDK版本
versionCode 1 //应用版本号
versionName "1.0" //版本名称,应用商店显示
// versionNameSuffix "@sruur.ct" // versionName添加后缀
// manifest文件占位符
manifestPlaceholders = [CHANNEL_VALUE: 'default/main']
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"// 测试引擎
}
//将打包生成的文件输出到特定文件夹,并重命名
applicationVariants.all {
variant ->
def outputDir = "${
project.projectDir}/build/outputs/apk"
variant.outputs.all {
output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith(".apk")) {
productFlavors.each{
flavor->
//打包生成的文件名
def fileName = "gradleLearing_v${
defaultConfig.versionName}_${
flavor.manifestPlaceholders.CHANNEL_VALUE}_release.apk"
outputFileName = fileName
}
}
}
variant.assemble.doLast {
File out = new File(outputDir)
copy {
variant.outputs.all {
outFile ->
copy {
from outFile.outputFile
into out
include '**/*.apk'
}
}
}
}
}
flavorDimensions "channel" //维度,针对一种类型产品风味的描述
//产品自定义风味
productFlavors {
xiaomi {
dimension "channel"
manifestPlaceholders = [CHANNEL_VALUE: 'xiaomi']
}
baidu {
dimension "channel"
manifestPlaceholders = [CHANNEL_VALUE: 'baidu']
}
}
//构建类型
buildTypes {
release {
//是否启用代码混淆
minifyEnabled false
// //清理无效资源
// shrinkResources true
//
// //是否开启zipAlign
// zipAlignEnabled true
//指定代码的混淆配置文件
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
//签名信息配置
signingConfigs {
release {
storeFile file("key_store.jks")
storePassword "a123123"
keyAlias "aaa"
keyPassword "a123123"
}
}
//编译选项
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = '11'
}
}
configurations {
//排除全部依赖的某个Module
configuration {
all*.exclude module: "annotation"
}
}
//configurations.all {
// resolutionStrategy {
// //强制指定版本
// force 'androidx.appcompat:appcompat:1.1.0'
// }
//}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
}
经过以上的配置,我们同步之后,可以看到我们打包的选项变得多了
并且在Gradle任务列表也生成了大量的相关任务
具体的内容就在代码中了,更多内容请查看官网,介绍的很详细:Android Gradle介绍