build: Android版本号自动同步package.json

android/app/build.gradle中通过读取package.json自动设置versionName与versionCode,实现前后端版本号一致,避免手动同步出错。
This commit is contained in:
谦君
2025-12-26 01:16:08 +08:00
parent fe2e5bfad9
commit 7ed508945a
2 changed files with 13 additions and 3 deletions

View File

@@ -1,5 +1,15 @@
apply plugin: 'com.android.application'
// 从 package.json 读取版本号
def packageJsonFile = file('../../package.json')
def packageJsonText = packageJsonFile.text
// 使用正则提取版本号
def versionMatcher = packageJsonText =~ /"version"\s*:\s*"([^"]+)"/
def appVersionName = versionMatcher ? versionMatcher[0][1] : "1.0.0"
// 将版本号转换为 versionCode例如 "1.5.5" -> 1*10000 + 5*100 + 5 = 10505
def versionParts = appVersionName.split('\\.')
def appVersionCode = versionParts[0].toInteger() * 10000 + versionParts[1].toInteger() * 100 + versionParts[2].toInteger()
android {
namespace = "games.wenzi.ogame"
compileSdk = rootProject.ext.compileSdkVersion
@@ -7,8 +17,8 @@ android {
applicationId "games.wenzi.ogame"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 15
versionName "1.5.0"
versionCode appVersionCode
versionName appVersionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
aaptOptions {
// Files and dirs to omit from the packaged assets dir, modified to accommodate modern web apps.

View File

@@ -9,7 +9,7 @@
},
"private": true,
"version": "1.5.5",
"buildDate": "2025/12/25 18:23:43",
"buildDate": "2025/12/26 01:02:13",
"main": "dist-electron/main.js",
"type": "module",
"scripts": {