Repository navigation
Expand file tree
/
Copy pathbuild.gradle.kts
More file actions
180 lines (154 loc) · 6.46 KB
/
Copy pathbuild.gradle.kts
File metadata and controls
180 lines (154 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import com.android.build.api.dsl.ApplicationExtension
import com.android.build.api.dsl.CommonExtension
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
// AndroidProject-Kotlin 版本:v17.0
plugins {
alias(libs.plugins.application) apply false
alias(libs.plugins.library) apply false
alias(libs.plugins.kotlin) apply false
alias(libs.plugins.aop) apply false
alias(libs.plugins.easyLauncher) apply false
}
fun main() {
loadAppConfig()
}
subprojects {
// 通用配置
pluginManager.withPlugin("com.android.base") {
// 为所有模块应用 Kotlin 插件
plugins.apply("org.jetbrains.kotlin.android")
plugins.apply("org.jetbrains.kotlin.kapt")
// 配置 Android 扩展
extensions.configure<CommonExtension> {
// 编译源码版本
compileSdk = 37
defaultConfig.apply {
minSdk = 23
}
// 只有应用模块才执行下面这段
if (this is ApplicationExtension) {
defaultConfig.apply {
// Android 版本适配指南:https://github.com/getActivity/AndroidVersionAdapter
targetSdk = 37
versionName = "1.0"
versionCode = 10
}
}
compileOptions.apply {
// 允许项目代码使用 Java 21 的新语法
sourceCompatibility = JavaVersion.VERSION_21
// 编译后的字节码适配 Java 21 虚拟机,为了兼容安卓低版本系统 ART
targetCompatibility = JavaVersion.VERSION_21
// 开启 JDK 核心库脱糖:把高版本 JDK 新增的类 (如 java.time) 打包进 apk,让安卓低版本手机也能正常运行不闪退
// https://developer.android.google.cn/studio/write/java8-support-table?hl=zh-cn
isCoreLibraryDesugaringEnabled = true
}
// 设置存放 so 文件的目录
sourceSets.getByName("main") {
jniLibs.directories += "libs"
}
// 可在 Studio 最左侧中的 Build Variants 选项中切换默认的构建类型
buildTypes {
getByName("debug") {}
create("preview") {
matchingFallbacks += listOf("debug")
}
getByName("release") {}
}
// 代码警告配置
lint.apply {
// HardcodedText:禁用文本硬编码警告
// ContentDescription:禁用图片描述警告
disable += listOf("HardcodedText", "ContentDescription")
}
// 读取 local.properties 文件配置
val properties = java.util.Properties()
val localPropertiesFile = rootProject.file("local.properties")
if (localPropertiesFile.exists()) {
localPropertiesFile.inputStream().use { properties.load(it) }
}
val buildDirPath = properties.getProperty("build.dir")
if (!buildDirPath.isNullOrEmpty()) {
// 将构建文件统一输出到指定的目录下
layout.buildDirectory = File(buildDirPath, rootProject.name + "/${project.path.replace(':', '/')}")
}
}
// 配置 Kotlin 编译选项
tasks.withType<KotlinCompile>().configureEach {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_21)
freeCompilerArgs.add("-Xcontext-parameters")
}
}
// 通用依赖配置(排除 library:base,因为它使用 api 依赖)
dependencies {
// 依赖 libs 目录下所有的 jar 和 aar 包
add("implementation", fileTree(mapOf("include" to listOf("*.jar", "*.aar"), "dir" to "libs")))
add("implementation", libs.appCompat)
add("implementation", libs.material)
add("implementation", libs.kotlinxCoroutinesCore)
add("implementation", libs.kotlinxCoroutinesAndroid)
add("implementation", libs.lifecycleRuntimeKTX)
add("implementation", libs.lifecycleViewModelKTX)
add("implementation", libs.lifecycleLiveDatalKTX)
add("coreLibraryDesugaring", libs.desugarJdkLibs)
}
}
}
/**
* 根据 Gradle 命令行参数配置 App 的服务器类型和相关参数
*/
fun loadAppConfig() {
// 测试服
val serverTypeTest = "test"
// 预发布服
val serverTypePreview = "pre"
// 正式服
val serverTypeProduct = "product"
val taskName = gradle.startParameter.taskNames.firstOrNull() ?: ""
// 打印当前执行的任务名称
println("GradleLog TaskNameOutput $taskName")
var serverType: String
serverType = when {
taskName.endsWith("Debug") -> serverTypeTest
taskName.endsWith("Preview") -> serverTypePreview
else -> serverTypeProduct
}
// 从 Gradle 命令中读取参数配置,例如:./gradlew assembleRelease -P ServerType="test"
if (project.hasProperty("ServerType")) {
serverType = project.property("ServerType").toString()
}
// 打印当前服务器配置
println("GradleLog ServerTypeOutput $serverType")
// 友盟 AppKey
extra["UMENG_APP_KEY"] = "5cb16d93570df399fd0014e2"
// QQ AppId
extra["QQ_APP_ID"] = "101828096"
// QQ Secret
extra["QQ_APP_SECRET"] = "9dfd3300c3aa3c4596a07796c64914b2"
// 微信 AppId
extra["WX_APP_ID"] = "wxd35706cc9f46114c"
// 微信 Secret
extra["WX_APP_SECRET"] = "0c8c7cf831dd135a32b3e395ea459b5a"
when (serverType) {
serverTypeTest, serverTypePreview -> {
extra["LOG_ENABLE"] = true
extra["BUGLY_ID"] = "请自行替换 Bugly 上面的 AppId"
extra["BUGLY_KEY"] = "请自行替换 Bugly 上面的 AppKey"
extra["HOST_URL"] = if (serverType == serverTypePreview) {
"https://www.pre.baidu.com/"
} else {
"https://www.test.baidu.com/"
}
}
serverTypeProduct -> {
extra["LOG_ENABLE"] = false
extra["BUGLY_ID"] = "请自行替换 Bugly 上面的 AppId"
extra["BUGLY_KEY"] = "请自行替换 Bugly 上面的 AppKey"
extra["HOST_URL"] = "https://www.baidu.com/"
}
}
}
// 由于 Gradle 这种脚本代码没有函数入口,所以必须手动调用一下 main 方法
main()