Move version-code, version-name to libs.versions.toml (#1544)

Add gradle task to verify if versionCode matches versionName.
This commit is contained in:
Catfriend1 2025-07-14 17:14:34 +02:00 committed by GitHub
parent dfd94e00af
commit b4240a2e94
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 42 additions and 139 deletions

View File

@ -23,15 +23,11 @@ jobs:
submodules: true
fetch-depth: 0
- name: Get app version from build.gradle.kts
- name: Get app version from libs.versions.toml
id: get_version
run: |
set -eu
VERSION_MAJOR=$(grep 'set("versionMajor"' build.gradle.kts | grep -o '[0-9]\+')
VERSION_MINOR=$(grep 'set("versionMinor"' build.gradle.kts | grep -o '[0-9]\+')
VERSION_PATCH=$(grep 'set("versionPatch"' build.gradle.kts | grep -o '[0-9]\+')
VERSION_WRAPPER=$(grep 'set("versionWrapper"' build.gradle.kts | grep -o '[0-9]\+')
VERSION="$VERSION_MAJOR.$VERSION_MINOR.$VERSION_PATCH.$VERSION_WRAPPER"
VERSION=$(grep 'version-name = ' gradle/libs.versions.toml | cut -d '"' -f 2)
echo "VERSION=$VERSION" >> $GITHUB_ENV
- name: Get commit hash

View File

@ -32,15 +32,11 @@ jobs:
exit 1
fi
- name: Get app version from build.gradle.kts
- name: Get app version from libs.versions.toml
id: get_version
run: |
set -eu
VERSION_MAJOR=$(grep 'set("versionMajor"' build.gradle.kts | grep -o '[0-9]\+')
VERSION_MINOR=$(grep 'set("versionMinor"' build.gradle.kts | grep -o '[0-9]\+')
VERSION_PATCH=$(grep 'set("versionPatch"' build.gradle.kts | grep -o '[0-9]\+')
VERSION_WRAPPER=$(grep 'set("versionWrapper"' build.gradle.kts | grep -o '[0-9]\+')
VERSION="$VERSION_MAJOR.$VERSION_MINOR.$VERSION_PATCH.$VERSION_WRAPPER"
VERSION=$(grep 'version-name = ' gradle/libs.versions.toml | cut -d '"' -f 2)
echo "VERSION=$VERSION" >> $GITHUB_ENV
- name: Get commit hash

View File

@ -40,10 +40,6 @@ dependencies {
android {
val ndkVersionShared = rootProject.extra.get("ndkVersionShared")
val versionMajor: kotlin.Int by rootProject.extra
val versionMinor: kotlin.Int by rootProject.extra
val versionPatch: kotlin.Int by rootProject.extra
val versionWrapper: kotlin.Int by rootProject.extra
compileSdk = libs.versions.compile.sdk.get().toInt()
namespace = "com.nutomic.syncthingandroid"
@ -58,8 +54,8 @@ android {
applicationId = "com.github.catfriend1.syncthingandroid"
minSdk = libs.versions.min.sdk.get().toInt()
targetSdk = libs.versions.target.sdk.get().toInt()
versionCode = versionMajor * 1000000 + versionMinor * 10000 + versionPatch * 100 + versionWrapper
versionName = "${versionMajor}.${versionMinor}.${versionPatch}.${versionWrapper}"
versionCode = libs.versions.version.code.get().toInt()
versionName = libs.versions.version.name.get()
}
signingConfigs {
@ -142,9 +138,33 @@ tasks.register<Delete>("deleteUnsupportedPlayTranslations") {
)
}
project.afterEvaluate {
val isCopilot = System.getenv("IS_COPILOT")?.toBoolean() ?: false
tasks.register("validateAppVersionCode") {
doFirst {
val versionName = libs.versions.version.name.get()
val versionCode = libs.versions.version.code.get().toInt()
val parts = versionName.split(".")
if (parts.size != 4) {
throw GradleException("Invalid versionName format: '$versionName'. Expected format 'major.minor.patch.wrapper'.")
}
val calculatedCode = parts[0].toInt() * 1_000_000 +
parts[1].toInt() * 10_000 +
parts[2].toInt() * 100 +
parts[3].toInt()
if (calculatedCode != versionCode) {
throw GradleException("Version mismatch: Calculated versionCode ($calculatedCode) does not match declared versionCode ($versionCode). Please review 'gradle/libs.versions.toml'.")
}
}
}
project.afterEvaluate {
tasks.matching { it.name.startsWith("assemble") || it.name.startsWith("bundle") }.configureEach {
dependsOn("validateAppVersionCode")
}
val isCopilot = System.getenv("IS_COPILOT")?.toBoolean() ?: false
if (!isCopilot) {
android.buildTypes.forEach {
val capitalizedName = it.name.replaceFirstChar { ch -> ch.uppercase() }

View File

@ -13,10 +13,6 @@ buildscript {
// Cannot be called "ndkVersion" as that leads to naming collision
// Changes to this value must be reflected in `./docker/Dockerfile`
set("ndkVersionShared", "28.0.13004108")
set("versionMajor", 1)
set("versionMinor", 30)
set("versionPatch", 0)
set("versionWrapper", 1)
}
repositories {

View File

@ -1,114 +0,0 @@
@echo off
setlocal enabledelayedexpansion
title Update "fdroid" with "main" branch
::
:: Relauch from local drive.
SET "RELAUNCH_PATH=%TEMP%\%~nx0"
IF /I NOT "%~dpnx0" == "%RELAUNCH_PATH%" SET "PROJECT_ROOT=%~dp0.." & copy /y "%~dpnx0" "%RELAUNCH_PATH%" >NUL: & echo [INFO] Relaunch from local drive & call "%RELAUNCH_PATH%" %1 %2 %3 %4 %5 %6 %7 %8 %9 & goto :eof
::
echo [INFO] Relaunched.
cd /d "%PROJECT_ROOT%"
::
:: Runtime Variables.
SET "APP_BUILD_GRADLE=app\build.gradle.kts"
::
:: Consts.
SET DRY_RUN=0
::
call :runGit fetch --all
::
IF "%DRY_RUN%" == "0" call :runGit checkout fdroid
IF "%DRY_RUN%" == "0" call :runGit merge --no-commit main
::
call :readVersionFromVersionsGradle
echo [INFO] VERSION_NAME=[%VERSION_NAME%]
echo [INFO] VERSION_CODE=[%VERSION_CODE%]
::
:: Write "versionName" and "versionCode" to "build.gradle".
TYPE "%APP_BUILD_GRADLE%" 2>NUL: | psreplace "\sversionCode = .*" " versionCode = %VERSION_CODE%" | psreplace "\sversionName = .*" " versionName = `%VERSION_NAME%`" "%APP_BUILD_GRADLE%"
::
echo [INFO] Done.
pause
::
goto :eof
:: ====================
:: FUNCTION BLOCK START
:: ====================
:readVersionFromVersionsGradle
::
:: Get "versionMajor"
SET VERSION_MAJOR=
FOR /F "tokens=2 delims==) " %%A IN ('type "build.gradle.kts" 2^>^&1 ^| findstr "versionMajor"') DO SET VERSION_MAJOR=%%A
SET VERSION_MAJOR=%VERSION_MAJOR:"=%
:: echo [INFO] versionMajor="%VERSION_MAJOR%"
::
:: Get "versionMinor"
SET VERSION_MINOR=
FOR /F "tokens=2 delims==) " %%A IN ('type "build.gradle.kts" 2^>^&1 ^| findstr "versionMinor"') DO SET VERSION_MINOR=%%A
SET VERSION_MINOR=%VERSION_MINOR:"=%
:: echo [INFO] versionMinor="%VERSION_MINOR%"
::
:: Get "versionPatch"
SET VERSION_PATCH=
FOR /F "tokens=2 delims==) " %%A IN ('type "build.gradle.kts" 2^>^&1 ^| findstr "versionPatch"') DO SET VERSION_PATCH=%%A
SET VERSION_PATCH=%VERSION_PATCH:"=%
:: echo [INFO] versionPatch="%VERSION_PATCH%"
::
:: Get "versionWrapper"
SET VERSION_WRAPPER=
FOR /F "tokens=2 delims==) " %%A IN ('type "build.gradle.kts" 2^>^&1 ^| findstr "versionWrapper"') DO SET VERSION_WRAPPER=%%A
SET VERSION_WRAPPER=%VERSION_WRAPPER:"=%
:: echo [INFO] versionWrapper="%VERSION_WRAPPER%"
::
SET VERSION_NAME=%VERSION_MAJOR%.%VERSION_MINOR%.%VERSION_PATCH%.%VERSION_WRAPPER%
::
:: Calculate "versionCode".
SET VERSION_CODE_MAJOR=%VERSION_MAJOR%
::
call :addTrailingZerosToVar %VERSION_MINOR%
SET VERSION_CODE_MINOR=%ATZTV_PADDED%
::
call :addTrailingZerosToVar %VERSION_MINOR%
SET VERSION_CODE_MINOR=%ATZTV_PADDED%
::
call :addTrailingZerosToVar %VERSION_PATCH%
SET VERSION_CODE_PATCH=%ATZTV_PADDED%
::
call :addTrailingZerosToVar %VERSION_WRAPPER%
SET VERSION_CODE_WRAPPER=%ATZTV_PADDED%
::
SET VERSION_CODE=%VERSION_CODE_MAJOR%%VERSION_CODE_MINOR%%VERSION_CODE_PATCH%%VERSION_CODE_WRAPPER%
::
goto :eof
:runGit
echo [INFO] git %1 %2 %3 %4 %5 %6 %7 %8 %9
git %1 %2 %3 %4 %5 %6 %7 %8 %9
SET RESULT=%ERRORLEVEL%
IF NOT "%RESULT%" == "0" echo [ERROR] git FAILED with error code #%RESULT%. & goto :pauseExit
goto :eof
:addTrailingZerosToVar
SET ATZTV_P1=0000000000000%1
SET "ATZTV_PADDED=!ATZTV_P1:~-2!"
goto :eof
:pauseExit
pause
goto :eof
:: ==================
:: FUNCTION BLOCK END
:: ==================

View File

@ -4,6 +4,10 @@ compile-sdk = "36"
min-sdk = "21"
target-sdk = "36"
# App version
version-code = "1300001"
version-name = "1.30.0.1"
aboutLibraries = "12.2.4"
activity = "1.10.1"
android-material = "1.12.0"

View File

@ -23,7 +23,12 @@ def getSourceDateEpoch() {
def verifySyncthingNativeVersionMatchesApp() {
def syncthingNativeVersion = getSyncthingNativeVersion()
def syncthingAppVersion = "${rootProject.ext.get("versionMajor")}.${rootProject.ext.get("versionMinor")}.${rootProject.ext.get("versionPatch")}"
def syncthingAppVersion = libs.versions.version.name
.get()
.split("\\.")
.toList()
.subList(0, 3)
.join(".")
if (syncthingNativeVersion != syncthingAppVersion) {
throw new GradleException("Checked out SyncthingNative version (${syncthingNativeVersion}) differs from App version (${syncthingAppVersion}). Please verify that the submodule refers to the correct commit.")
}