能否在pubspec.yaml文件中添加变量?实现从version.properties文件获取版本名并设置为pubspec.yaml的version字段值
Absolutely! While pubspec.yaml doesn’t natively support variables or reading external files directly, there are straightforward workarounds to pull your version name from a version.properties file and inject it into the version field of pubspec.yaml. Let’s walk through the most practical approaches:
Approach 1: Cross-Platform Dart Script (Recommended)
Since Flutter projects already have a Dart environment ready, this method works seamlessly across Windows, macOS, and Linux.
Step 1: Create your version.properties file
Add a version.properties file at the root of your project with:
versionName=1.2.3+4 # Optional: Split into name and build number if needed # versionName=1.2.3 # buildNumber=4
Step 2: Write the Dart update script
Create a file named update_version.dart in your project root. This script will read the properties file, parse the version, and update pubspec.yaml:
import 'dart:io'; Future<String> getVersionFromProperties() async { final file = File('version.properties'); if (!await file.exists()) { print('version.properties not found! Using default version.'); return '1.0.0+1'; } final lines = await file.readAsLines(); for (var line in lines) { line = line.trim(); // Skip comments and empty lines if (line.isEmpty || line.startsWith('#')) continue; if (line.startsWith('versionName=')) { return line.split('=').last.trim(); } } // Fallback if versionName isn't found return '1.0.0+1'; } void main() async { final targetVersion = await getVersionFromProperties(); // Read and update pubspec.yaml final pubspecFile = File('pubspec.yaml'); var pubspecContent = await pubspecFile.readAsString(); // Regex to match the existing version line (works with multi-line content) final versionRegex = RegExp(r'^version: .*$', multiLine: true); pubspecContent = pubspecContent.replaceFirst(versionRegex, 'version: $targetVersion'); await pubspecFile.writeAsString(pubspecContent); print('Successfully updated pubspec.yaml version to: $targetVersion'); }
Step 3: Run the script before building/running
Execute the script first, then run your Flutter commands:
dart update_version.dart && flutter run
Or for builds:
dart update_version.dart && flutter build apk
Approach 2: Shell/PowerShell Script (OS-Specific)
If you prefer shell scripting, here’s how to do it for macOS/Linux and Windows:
For macOS/Linux (Bash Script)
Create update_version.sh:
#!/bin/bash # Read versionName from version.properties VERSION=$(grep 'versionName=' version.properties | cut -d'=' -f2 | xargs) # Fallback if version isn't found if [ -z "$VERSION" ]; then VERSION="1.0.0+1" fi # Update pubspec.yaml (note: macOS requires the empty '' flag for sed) sed -i '' "s/^version:.*/version: $VERSION/" pubspec.yaml echo "Updated pubspec.yaml version to: $VERSION"
Make it executable and run:
chmod +x update_version.sh && ./update_version.sh && flutter run
For Windows (PowerShell Script)
Create update_version.ps1:
# Read versionName from version.properties $versionLine = Get-Content version.properties | Where-Object { $_ -match '^versionName=' } if ($versionLine) { $VERSION = ($versionLine -split '=')[1].Trim() } else { $VERSION = "1.0.0+1" } # Update pubspec.yaml (Get-Content pubspec.yaml) -replace '^version: .*', "version: $VERSION" | Set-Content pubspec.yaml Write-Host "Updated pubspec.yaml version to: $VERSION"
Run it with:
.\update_version.ps1; flutter run
Bonus: Automate the Script Run
To avoid manually running the script every time, you can integrate it into your IDE’s run configurations:
- In VS Code: Add a task in
.vscode/tasks.jsonthat runs the Dart/shell script before launching Flutter. - In Android Studio: Edit your run configuration to include the script as a "Before launch" step.
内容的提问来源于stack exchange,提问作者Sarvesh Dalvi

