@@ -14,9 +14,10 @@ String composeBoMVersion = "2024.12.01"
14
14
15
15
def time = new Date ()
16
16
17
- def gitCommitCount = executeCmd(" git rev-list --count HEAD" )?. toInteger() ?: 1
18
- def gitLastTag = executeCmd(" git describe --tags --always" ) ?: " 1.0"
19
- def gitCommitId = executeCmd(" git rev-parse --short HEAD" ) ?: " nocommitid"
17
+ def gitCommitCount = executeCmd(" git rev-list --count HEAD" )?. toInteger() ?: 0
18
+ def gitCommitId = executeCmd(" git rev-parse --short HEAD" ) ?: " v0.0.0" //
19
+ def gitTag = executeCmd(" git tag --points-at HEAD" ). split(' \n ' )
20
+ def gitLastTag = executeCmd(" git describe --tags --always" ). split(" -" ). first() ?: " v0.0.0"
20
21
21
22
static def executeCmd (String cmd ) {
22
23
try {
@@ -30,6 +31,29 @@ static def executeCmd(String cmd) {
30
31
}
31
32
}
32
33
34
+ // 兼容本地开发和 workflow 脚本
35
+ def getEnvOrLocal (String key ) {
36
+ def value = System . getenv(key) // github secrets > CI > gradle
37
+ if (value == null || value. isEmpty()) {
38
+ // 尝试从 local.properties 中获取
39
+ def localProperties = new Properties ()
40
+ def localPropertiesFile = file(" ../local.properties" )
41
+ if (localPropertiesFile. exists()) {
42
+ localPropertiesFile. withInputStream { stream ->
43
+ localProperties. load(stream)
44
+ }
45
+ } else {
46
+ throw new FileNotFoundException (" local.properties 文件不存在,请求队友助攻" )
47
+ }
48
+ // 获取 key 的 value
49
+ value = localProperties. getProperty(key)
50
+ }
51
+ if (value == null || value. isEmpty()) {
52
+ throw new IllegalArgumentException (" 环境变量 或 local.properties 中缺少 ${ key} 的值,请求队友助攻" )
53
+ }
54
+ return value
55
+ }
56
+
33
57
android {
34
58
namespace ' com.example.wan.android'
35
59
// 确定编译应用时,项目代码可以使用哪些 API。不影响应用运行时的行为,只影响编译时可以访问到哪些类和方法
@@ -42,7 +66,7 @@ android {
42
66
// 指定应用在运行时目标的 Android 版本。不影响可以使用的 API,而是影响应用在运行时的行为
43
67
targetSdk 35
44
68
versionCode gitCommitCount
45
- versionName gitLastTag // 假如每个 tag 都是一个 release
69
+ versionName gitLastTag
46
70
47
71
multiDexEnabled true
48
72
@@ -73,63 +97,62 @@ android {
73
97
74
98
// ndkVersion project.properties.ndkVersion
75
99
76
- /*
77
100
signingConfigs {
78
101
// https://developer.android.com/studio/publish/app-signing#secure_key
79
- config {
80
- storeFile file(rootProject.ext.sign.filePath)
81
- storePassword rootProject.ext.sign.storePassword
82
- keyAlias rootProject.ext.sign.keyAlias
83
- keyPassword rootProject.ext.sign.keyPassword
102
+ release {
103
+ storeFile file(" ../zjmok.jks" ) // 密钥库文件路径,本地开发直接放到此路径,CI 环境脚本解密输出到此路径
104
+ storePassword getEnvOrLocal(" KEYSTORE_PASSWORD" )
105
+ keyAlias getEnvOrLocal(" KEY_ALIAS" )
106
+ keyPassword getEnvOrLocal(" KEY_PASSWORD" )
107
+ }
108
+ // Android 自动生成的 debug.keystore 签名
109
+ debugAsRelease {
110
+ storeFile file(" ${ System.getenv('ANDROID_SDK_HOME') ?: System.getProperty('user.home')} /.android/debug.keystore" )
111
+ storePassword " android"
112
+ keyAlias " androiddebugkey"
113
+ keyPassword " android"
84
114
}
85
115
}
86
- */
87
116
88
117
// https://developer.android.com/build/build-variants
89
118
buildTypes {
90
119
debug {
120
+ versionNameSuffix " .${ gitCommitId} "
91
121
debuggable true // 可调试
92
122
minifyEnabled false // 代码压缩 (R8/ProGuard)
93
123
shrinkResources false // 资源压缩 (shrinkResources)
94
- signingConfig signingConfigs. debug
95
124
}
96
125
release {
97
126
debuggable false
98
127
minifyEnabled true
99
128
shrinkResources true
100
129
proguardFiles getDefaultProguardFile(' proguard-android-optimize.txt' ), ' proguard-rules.pro'
101
- signingConfig signingConfigs. debug
130
+ // signingConfig signingConfigs.debugAsRelease
131
+ signingConfig signingConfigs. release
102
132
}
103
133
}
104
134
android. applicationVariants. all { variant ->
105
135
variant. outputs. all {
106
- def flavorType = variant. productFlavors. collect { it. name. capitalize() }. join() + variant. buildType. name. capitalize() // 格式 GithubRelease
107
- def name
108
- if (variant. productFlavors[0 ]. name == " github" ) {
109
- // 与 github actions 脚本获取的名称对应
110
- name = (" ${ model.name} " +
111
- " -${ variant.buildType.name} " +
112
- " .apk" )
113
- } else {
114
- name = (" ${ model.name} " +
115
- " _${ variant.versionName} " +
116
- " _${ variant.versionCode} " +
117
- // "_${variant.buildType.name}" +
118
- " _${ flavorType} " +
119
- " _${ gitCommitId} " +
120
- " _${ time.time} " +
121
- " .apk" )
122
- }
136
+ def flavorType = variant. productFlavors. collect {
137
+ it. name. capitalize()
138
+ }. join() + variant. buildType. name. capitalize() // 格式 GithubRelease
139
+ def name = (" WanAndroid" +
140
+ " _${ variant.versionName} " +
141
+ // "_${variant.versionCode}" +
142
+ // "_${variant.buildType.name}" +
143
+ // "_${flavorType}" +
144
+ // "_${gitCommitId}" +
145
+ // "_${time.time}" +
146
+ " .apk" )
123
147
println (" ${ flavorType} outputFileName = ${ name} " )
124
148
outputFileName = name
125
149
}
126
150
}
127
- // 变体、维度
151
+ // 维度、风味,组合生成最终变体
128
152
// https://developer.android.com/build/build-variants
129
- flavorDimensions + = [" channel" ] // 可以使用多个维度,最终构建的安装包会包含每个维度的一个变体
130
- productFlavors {
131
- // 维度1 渠道
132
- normal {
153
+ flavorDimensions + = [" channel" ] // 维度
154
+ productFlavors { // 风味
155
+ app {
133
156
dimension " channel"
134
157
manifestPlaceholders = [app_channel : " \" ${ name} \" " ]
135
158
buildConfigField " String" , " APP_CHANNEL" , " \" ${ name} \" "
@@ -225,7 +248,7 @@ dependencies {
225
248
// `project` 使用指定模块
226
249
// `lintChecks` 是 lint-checks 库的,Lint 专用
227
250
// noinspection DependencyNotationArgument
228
- lintChecks project(' :module_lint' )
251
+ // lintChecks project(':module_lint')
229
252
230
253
// Test
231
254
testImplementation " junit:junit:4.13.2"
0 commit comments