Skip to content

Commit f8c5c15

Browse files
author
Fastace
committed
Update:更新版本号构建逻辑
1 parent dd21b1b commit f8c5c15

2 files changed

Lines changed: 65 additions & 74 deletions

File tree

.github/workflows/build.yaml

Lines changed: 44 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build
1+
name: Buildon
22

33
on:
44
workflow_dispatch:
@@ -10,11 +10,11 @@ on:
1010
Premium:
1111
description: 'Premium'
1212
required: true
13-
default: 'false'
13+
default: 'true'
1414
Foss:
1515
description: 'Foss'
1616
required: true
17-
default: 'false'
17+
default: 'true'
1818
release_type:
1919
description: 'Release Type'
2020
required: true
@@ -27,26 +27,20 @@ on:
2727
description: 'Version Name (e.g., 2.0.13-S3)'
2828
required: false
2929
default: ''
30-
version_code:
31-
description: 'Version Code (e.g., 3501200)'
32-
required: false
33-
default: ''
3430

3531
jobs:
3632
build:
3733
name: "Build APKs"
3834
runs-on: ubuntu-latest
39-
4035
permissions:
4136
contents: write
42-
4337
timeout-minutes: 60
44-
4538
steps:
4639
- name: Checkout
4740
uses: actions/checkout@v4
4841
with:
49-
ref: feature/restic-integration # 指定使用 s3 分支
42+
ref: feature/restic-integration
43+
fetch-depth: 0 # 获取完整提交历史以统计 commit 数量
5044

5145
- name: Validate gradle wrapper
5246
uses: gradle/wrapper-validation-action@v1
@@ -60,26 +54,34 @@ jobs:
6054
- name: Setup gradle
6155
uses: gradle/gradle-build-action@v2
6256

63-
- name: Change to source directory
64-
run: cd source
65-
66-
- name: Setup gradlew
67-
run: chmod +x gradlew
68-
working-directory: source
69-
70-
- name: Check build-logic
71-
run: ./gradlew check -p build-logic
57+
- name: Calculate Dynamic Version
58+
id: calc_version
59+
run: |
60+
# 核心逻辑:基础 30000 + Commit 总数
61+
COMMITS=$(git rev-list --count HEAD)
62+
BASE_CODE=30000
63+
DYNAMIC_CODE=$((BASE_CODE + COMMITS))
64+
echo "code=$DYNAMIC_CODE" >> $GITHUB_OUTPUT
65+
66+
# 确定版本名称
67+
if [ -n "${{ github.event.inputs.version_name }}" ]; then
68+
VERSION_NAME="${{ github.event.inputs.version_name }}"
69+
else
70+
VERSION_NAME=$(grep "versionName" gradle/libs.versions.toml | cut -d'"' -f2)
71+
fi
72+
echo "name=$VERSION_NAME" >> $GITHUB_OUTPUT
73+
74+
echo "Final Version Code: $DYNAMIC_CODE"
75+
echo "Final Version Name: $VERSION_NAME"
7276
working-directory: source
7377

74-
- name: Build with gradle (Skip signing)
78+
- name: Build with gradle (Dynamic Version Injection)
7579
id: gradle
7680
env:
7781
BUILD_ALPHA: ${{ github.event.inputs.Alpha }}
7882
BUILD_PREMIUM: ${{ github.event.inputs.Premium }}
7983
BUILD_FOSS: ${{ github.event.inputs.Foss }}
8084
run: |
81-
echo "Building without signing - using debug variants"
82-
8385
TASKS=""
8486
if [ "${BUILD_ALPHA}" = 'true' ]; then
8587
TASKS="$TASKS assembleArm64-v8aAlphaDebug assembleArmeabi-v7aAlphaDebug assembleX86AlphaDebug assembleX86_64AlphaDebug"
@@ -90,77 +92,54 @@ jobs:
9092
if [ "${BUILD_FOSS}" = 'true' ]; then
9193
TASKS="$TASKS assembleArm64-v8aFossDebug assembleArmeabi-v7aFossDebug assembleX86FossDebug assembleX86_64FossDebug"
9294
fi
95+
9396
if [ -z "${TASKS}" ]; then
9497
TASKS="assembleDebug"
9598
fi
9699
97-
echo "Running tasks: $TASKS"
98-
./gradlew $TASKS --daemon --parallel
100+
chmod +x gradlew
101+
# 注入版本参数,对应 build.gradle.kts 中的 project.hasProperty 逻辑
102+
./gradlew $TASKS \
103+
-PversionCode=${{ steps.calc_version.outputs.code }} \
104+
-PversionName="${{ steps.calc_version.outputs.name }}" \
105+
--daemon --parallel
99106
working-directory: source
100107

101108
- name: Move APKs to root
102109
run: |
103-
# 在 source 目录创建 APKs 目录并移动文件
104110
mkdir -p ../build-output
105111
find . -name "*.apk" -exec mv {} ../build-output/ \;
106-
echo "APK files in root build-output:"
107-
ls -la ../build-output/
112+
echo "Collected APKs:"
113+
ls -R ../build-output/
108114
working-directory: source
109115

110116
- name: Upload APKs
111117
uses: actions/upload-artifact@v4
112118
with:
113-
name: APKs
119+
name: APKs-v${{ steps.calc_version.outputs.name }}-b${{ steps.calc_version.outputs.code }}
114120
path: build-output/*.apk
115121

116-
- name: Get version name
117-
id: version
118-
run: |
119-
if [ -n "${{ github.event.inputs.version_name }}" ]; then
120-
VERSION="${{ github.event.inputs.version_name }}"
121-
else
122-
VERSION=$(grep "versionName" gradle/libs.versions.toml | cut -d'"' -f2)
123-
fi
124-
echo "version=$VERSION" >> $GITHUB_OUTPUT
125-
126-
if [ -n "${{ github.event.inputs.version_code }}" ]; then
127-
CODE="${{ github.event.inputs.version_code }}"
128-
else
129-
CODE=$(grep "versionCode" gradle/libs.versions.toml | cut -d'"' -f2)
130-
fi
131-
echo "code=$CODE" >> $GITHUB_OUTPUT
132-
working-directory: source
133-
134122
- name: Create Release
135123
if: github.event.inputs.release_type != ''
136124
uses: softprops/action-gh-release@v1
137125
with:
138-
tag_name: v${{ steps.version.outputs.version }}
139-
name: v${{ steps.version.outputs.version }}
126+
tag_name: v${{ steps.calc_version.outputs.name }}.${{ steps.calc_version.outputs.code }}
127+
name: v${{ steps.calc_version.outputs.name }} (Build ${{ steps.calc_version.outputs.code }})
140128
prerelease: ${{ github.event.inputs.release_type == 'pre-release' }}
141129
files: build-output/*.apk
142130
body: |
143-
## What's New in v${{ steps.version.outputs.version }}
144-
145-
### Version Info
146-
- Version Name: ${{ steps.version.outputs.version }}
147-
- Version Code: ${{ steps.version.outputs.code }}
131+
## Release Info
132+
- **Version:** `${{ steps.calc_version.outputs.name }}`
133+
- **Build Code:** `${{ steps.calc_version.outputs.code }}` (Base 30000 + Commits)
134+
- **Branch:** `feature/restic-integration`
148135
149-
### Features
150-
- Build type: ${{ github.event.inputs.release_type }}
136+
### Included Variants
151137
- Alpha: ${{ github.event.inputs.Alpha }}
152138
- Premium: ${{ github.event.inputs.Premium }}
153139
- Foss: ${{ github.event.inputs.Foss }}
154-
- Branch: s3 (S3-specific features)
155140
156141
### Note
157-
⚠️ This is a DEBUG build without signing, for testing purposes only.
158-
159-
### Download
160-
Choose the APK file matching your device architecture:
161-
- arm64-v8a (recommended for most modern devices)
162-
- armeabi-v7a (for older 32-bit ARM devices)
163-
- x86_64 (for x86 64-bit devices)
164-
- x86 (for x86 32-bit devices)
142+
⚠️ This is a **DEBUG** build without formal signing, created for testing purposes.
143+
All ABIs in this build share the same Version Code.
165144
env:
166145
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

source/app/build.gradle.kts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import com.android.build.gradle.internal.api.BaseVariantOutputImpl
22

3-
43
plugins {
54
alias(libs.plugins.application.common)
65
alias(libs.plugins.application.hilt)
@@ -29,15 +28,30 @@ tasks.register("downloadResticBinaries") {
2928
}
3029

3130
android {
31+
// 动态获取 Actions 传入的版本信息
32+
val cmdVersionCode = if (project.hasProperty("versionCode")) {
33+
project.property("versionCode").toString().toInt()
34+
} else {
35+
libs.versions.versionCode.get().toInt()
36+
}
37+
38+
val cmdVersionName = if (project.hasProperty("versionName")) {
39+
project.property("versionName").toString()
40+
} else {
41+
libs.versions.versionName.get()
42+
}
43+
3244
namespace = "com.xayah.databackup" // 修改:改回原来的包名
3345
compileSdk = libs.versions.compileSdk.get().toInt()
3446

3547
defaultConfig {
3648
applicationId = "com.xayah.databackup.revived" // 保持:新的应用包名
3749
minSdk = libs.versions.minSdk.get().toInt()
3850
targetSdk = libs.versions.targetSdk.get().toInt()
39-
versionCode = libs.versions.versionCode.get().toInt()
40-
versionName = libs.versions.versionName.get()
51+
52+
// 使用动态版本号
53+
versionCode = cmdVersionCode
54+
versionName = cmdVersionName
4155

4256
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
4357

@@ -52,22 +66,19 @@ android {
5266
productFlavors {
5367
create("arm64-v8a") {
5468
dimension = "abi"
55-
versionCode = 4 + (android.defaultConfig.versionCode ?: 0)
69+
// 移除偏移逻辑,统一使用基础版本号
5670
ndk.abiFilters.add("arm64-v8a")
5771
}
5872
create("armeabi-v7a") {
5973
dimension = "abi"
60-
versionCode = 3 + (android.defaultConfig.versionCode ?: 0)
6174
ndk.abiFilters.add("armeabi-v7a")
6275
}
6376
create("x86_64") {
6477
dimension = "abi"
65-
versionCode = 2 + (android.defaultConfig.versionCode ?: 0)
6678
ndk.abiFilters.add("x86_64")
6779
}
6880
create("x86") {
6981
dimension = "abi"
70-
versionCode = 1 + (android.defaultConfig.versionCode ?: 0)
7182
ndk.abiFilters.add("x86")
7283
}
7384
create("foss") {
@@ -81,8 +92,9 @@ android {
8192
create("alpha") {
8293
dimension = "feature"
8394
applicationIdSuffix = ".alpha"
84-
versionCode = libs.versions.versionCodeAlpha.get().toInt()
85-
versionName = libs.versions.versionCodeAlpha.get()
95+
// 确保 Alpha 变体也遵循动态版本号
96+
versionCode = cmdVersionCode
97+
versionName = cmdVersionName
8698
}
8799
}
88100

0 commit comments

Comments
 (0)