The idea is from the another project capacitor-sync-version
Syncing version from package.json to target platform in the capacitor projects.
The version are configured separately in package.json for Android and iOS, because this sync is not 1 to 1 possible without assumtions.
{
"name": "Your App",
...
"androidVersionName": "1.0.0",
"androidVersionCode": "2",
"iOSVersion": "1.1.0",
"iOSBuild": "2",
}
Android native | package.json |
---|---|
versionName | androidVersionName |
versionCode | androidVersionCode |
iOS native | package.json |
---|---|
version | iOSVersion |
build | iOSBuild |
The androidVersionCode and iOSBuild should be integer and increase each time
Currently supported platforms are: [android, ios]
.
npm add --save-dev capacitor-plugin-sync-version
Step 1. Create a file named app.properties
in ./android/app/
, then add the following properties:
versionName=1.0.0
versionCode=1
These properties will be updated when capacitor-plugin-sync-version running.
Step 2. Adjust codes to referrence these properties in ./android/app/build.gradle
.
-
Add the following codes after
apply plugin: 'com.android.application'
:def appProperties = new Properties(); file("app.properties").withInputStream { appProperties.load(it) }
-
Update properties in
defaultConfig {}
block:defaultConfig { versionCode appProperties.getProperty("versionCode").toInteger() versionName appProperties.getProperty("versionName") }
The simplest way to use capacitor-plugin-sync-version is running it in the capacitor hooks.
Just adding the following script to package.json
:
{
"scripts": {
"capacitor:copy:before": "capacitor-plugin-sync-version"
}
}
In this way, capacitor-plugin-sync-version will be run before capacitor copy command, e.g., npx cap copy
npx cap sync
.
Other ways to run capacitor-sync-version are:
# sync for android only
capacitor-plugin-sync-version android
# sync for both android and ios
capacitor-plugin-sync-version android ios
MIT