"""Offline Flutter Build & Release configuration (pubspec version, Android Gradle signing, iOS/Web/Desktop build commands) verifier for Ch22 (build-release)."""
import json
import re
VERSION = "CPython 3.12.14 offline Flutter/Dart architecture & runtime verifier (Flutter SDK absent)"
def check(name, summary, expected, actual):
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def validate_android_release_config(pubspec_ver, gradle):
m = re.match(r"^(\d+\.\d+\.\d+)\+(\d+)$", pubspec_ver)
semver, build_num = m.group(1), int(m.group(2))
signing = gradle["signingConfigs"]["release"]
has_secrets_hardcoded = any(
isinstance(signing.get(k), str) and not signing[k].startswith("env:")
for k in ("storePassword", "keyPassword")
)
return {
"version_aligned": gradle["versionName"] == semver and gradle["versionCode"] == build_num,
"sdk_range_ok": 21 <= gradle["minSdk"] <= gradle["targetSdk"] <= gradle["compileSdk"],
"hardcoded_secret_warning": has_secrets_hardcoded,
"application_id_valid": bool(re.match(r"^[a-z]+(\.[a-z][a-z0-9_]*)+$", gradle["applicationId"])),
}
def main():
cases = []
# Case 1: pubspec.yaml version + Android build.gradle config & signing audit
gradle_cfg = {
"namespace": "com.example.myapp",
"applicationId": "com.example.myapp",
"compileSdk": 34,
"minSdk": 21,
"targetSdk": 34,
"versionCode": 1,
"versionName": "1.0.0",
"signingConfigs": {
"release": {
"storeFile": "key.jks",
"storePassword": "env:KEYSTORE_PASSWORD",
"keyAlias": "upload",
"keyPassword": "env:KEY_PASSWORD",
}
},
}
audit = validate_android_release_config("1.0.0+1", gradle_cfg)
cases.append(
check(
"pubspec_and_android_gradle_audit",
"Verify pubspec version 1.0.0+1 matches Android versionName/versionCode, SDK bounds 21<=34<=34, and env-injected signing",
{
"version_aligned": True,
"sdk_range_ok": True,
"hardcoded_secret_warning": False,
"application_id_valid": True,
},
audit,
)
)
# Case 2: Android APK vs App Bundle (AAB) & obfuscation build flags
android_cmds = {
"debug_apk": "flutter build apk --debug",
"release_apk": "flutter build apk --release",
"arm64_apk": "flutter build apk --release --target-platform android-arm64",
"play_aab": "flutter build appbundle --release",
"obfuscated_aab": "flutter build appbundle --release --obfuscate --split-debug-info=build/symbols",
}
cases.append(
check(
"android_apk_and_appbundle_commands",
"Verify Android APK, arm64 split, Play Store App Bundle (AAB), and Dart obfuscation flags",
{"cmd_count": 5, "play_uses_appbundle": True, "obfuscate_has_symbol_dir": True},
{
"cmd_count": len(android_cmds),
"play_uses_appbundle": "appbundle" in android_cmds["play_aab"],
"obfuscate_has_symbol_dir": "--split-debug-info=" in android_cmds["obfuscated_aab"],
},
)
)
# Case 3: iOS simulator vs IPA release & Xcode archive checklist
ios_cfg = {
"sim_cmd": "flutter build ios --simulator --no-codesign",
"ipa_cmd": "flutter build ipa --release",
"xcode_workspace": "ios/Runner.xcworkspace",
"prerequisites": ["Xcode", "Apple Developer Account", "App ID & Certificates"],
}
cases.append(
check(
"ios_build_and_archive_contract",
"Verify iOS simulator --no-codesign command, IPA release build, and Runner.xcworkspace archive requirement",
{"uses_xcworkspace": True, "sim_skips_codesign": True, "prereq_count": 3},
{
"uses_xcworkspace": ios_cfg["xcode_workspace"].endswith(".xcworkspace"),
"sim_skips_codesign": "--no-codesign" in ios_cfg["sim_cmd"],
"prereq_count": len(ios_cfg["prerequisites"]),
},
)
)
# Case 4: Web index.html PWA tags & Windows/macOS/Linux desktop release commands
web_html = '<base href="/"><title>我的应用</title><link rel="manifest" href="manifest.json"><script src="flutter.js" defer></script>'
desktop_cmds = {
"windows": "flutter build windows --release",
"macos": "flutter build macos --release",
"linux": "flutter build linux --release",
}
cases.append(
check(
"web_pwa_and_desktop_builds",
"Verify Web index.html base/manifest/flutter.js tags, build/web output, and 3 desktop --release commands",
{
"has_base_href": True,
"has_pwa_manifest": True,
"desktop_targets": ["windows", "macos", "linux"],
"all_release_flag": True,
},
{
"has_base_href": '<base href="/">' in web_html,
"has_pwa_manifest": 'rel="manifest"' in web_html,
"desktop_targets": list(desktop_cmds.keys()),
"all_release_flag": all("--release" in v for v in desktop_cmds.values()),
},
)
)
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/flutter-build-release-lab/explore-build-release.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-build-release.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a1988.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1990.zip