編輯:關於 Android 程式設計
一個專案因為硬體差異(例如電池容量不同、鏡頭解析度不同)產生了兩個版本,共用同一套代碼基線。問題在於,兩者的 OTA 更新包外觀相似,一旦刷錯,設備可能直接無法開機。因此需要一套「防互刷」機制,讓 A 版本的更新包拒絕安裝在 B 版本的機器上。
讀者可能會問:既然容易混淆,直接拆成兩個專案不就解決了?確實可以,而且這是最快的方案。本文討論的是「必須維持單一專案」這個前提下的做法。
熟悉 ota_from_target_files、edify_generator.py,或讀過 install.c 與 updater-script 執行流程的人會知道,更新包在安裝之初就會對一批屬性做校驗。以下是腳本產生器中幾個關鍵函式。
校驗時間戳:
def __AssertOlderBuild(self, timestamp, timestamp_text):
self.script.append(
('(!less_than_int(%s, getprop("ro.build.date.utc"))) || '
'abort("Can\'t install this package (%s) over newer '
'build (" + getprop("ro.build.date") + ").");'
) % (timestamp, timestamp_text))
這個函式本身只產生一行 edify 語句,實際寫進 updater-script 的長這樣:
(!less_than_int(時間戳, getprop("ro.build.date.utc"))) || abort(...);
前半段為真就繼續,否則執行 abort 中斷。也就是說,若更新包的打包時間比機器上 Build 的時間還舊,安裝就會被拒絕。
校驗裝置資訊:
def __AssertDevice(self, device):
cmd = ('getprop("ro.product.device") == "%s" || '
'abort("This package is for \\"%s\\" devices; '
'this is a \\"" + getprop("ro.product.device") + "\\".");'
) % (device, device)
self.script.append(cmd)
更新包內記錄的裝置型號若與機器不一致,同樣中斷。
校驗系統指紋:
def __AssertSomeFingerprint(self, *fp):
cmd = (' ||\n '.join([
('file_getprop("/system/build.prop", "ro.build.fingerprint") == "%s"')
% i for i in fp]) +
' ||\n abort("Package expects build fingerprint of %s; this '
'device has " + getprop("ro.build.fingerprint") + ".");'
) % (" or ".join(fp),)
self.script.append(cmd)
新舊版本的 fingerprint 與機器現況不符時,一樣拒絕安裝。
這三項數值都取自 build.prop。其中 fingerprint(系統指紋)值得多說兩句:它是每個專案每個版本獨一無二的識別字,Google 要求前後不得有空格或斷行,長度不超過 91 個字元。可以透過 adb 下 getprop ro.build.fingerprint 查看,格式定義如下:
BUILD_FINGERPRINT := $(PRODUCT_BRAND)/$(TARGET_PRODUCT)/$(TARGET_DEVICE):$(PLATFORM_VERSION)/$(BUILD_ID)/$(BUILD_NUMBER):$(TARGET_BUILD_VARIANT)/$(BUILD_VERSION_TAGS)
例如:
Google/Y100-T00/GoogleY100-T:4.2.2/GoogleY100-T00/CHSC01B001:user/release-keys
各欄位對應的 getprop 項目:PRODUCT_BRAND 對應 ro.product.brand;TARGET_PRODUCT 對應 ro.product.name;TARGET_DEVICE 對應 ro.product.device;BUILD_ID 對應 ro.build.id;version.incremental 對應 ro.build.version.incremental(需為真實的內部版本號);VERSION.RELEASE 對應 ro.build.version.release,一般即 Android 版本號。此外 TYPE 必須是 user 而非 eng,TAGS 必須是 release-keys 而非 test-keys;若該版本要支援 OTA,則需採用 ota-rel-keys。
釐清之後可以發現:fingerprint 主要在差分升級時才會被校驗,而裝置資訊與簽名對同一個產品而言通常不可變動。既然如此,思路就清楚了——只要新增一個自有的系統屬性來標識版本,就能達到防互刷的效果。
先在 system.prop 的最後一行加上自訂屬性(路徑依平台與公司而異,本文位於 vendor/{專案}/{分支}/config/system.prop):
ro.update.version=a1
接著在 edify_generator.py 中定義對應的校驗方法:
def AssertUpdateVersion(self, update_version):
"""Assert that the update_version identifier is the given string."""
cmd = ('getprop("ro.update.version") == "%s" || '
'abort("This package is for \\"%s\\" update_version; '
'this is a \\"" + getprop("ro.update.version") + "\\".");'
) % (update_version, update_version)
self.script.append(cmd)
然後在 ota_from_target_file 的 AppendAssertions 中掛上去:
def AppendAssertions(script, info_dict):
device = GetBuildProp("ro.product.device", info_dict)
script.AssertDevice(device)
update_version = GetBuildProp("ro.update.version", info_dict)
script.AssertUpdateVersion(update_version)
最後修改 WriteFullOTAPackage(),把新屬性寫進 metadata:
metadata = {
"post-build": GetBuildProp("ro.build.fingerprint", OPTIONS.info_dict),
"pre-device": GetBuildProp("ro.product.device", OPTIONS.info_dict),
"post-timestamp": GetBuildProp("ro.build.date.utc", OPTIONS.info_dict),
"update-version": GetBuildProp("ro.update.version", OPTIONS.info_dict),
}
用 ro.update.version 為 a1 的版本產出整包,然後在屬性為 a2 的機器上以 SD 卡整包升級。若機制生效,安裝會被拒絕並留下類似以下的日誌:
script aborted: This package is for "a1" update_version; this is a "a2". E:Error in /sdcard/dload/update.zip (Status 7) Installation aborted.
對應的 updater-script 內容就是我們插入的那一行:
getprop("ro.update.version") == "a1" || abort("This package is for \"a1\" update_version; this is a \"" + getprop("ro.update.version") + "\".");
驗證時請務必重新產包裝機,不要直接改更新包裡的 build.prop。build.prop 已被簽名涵蓋,事後竄改會導致簽名校驗失敗,你看到的錯誤訊息會是簽名不符,而非你要驗證的 update-version 不符——症狀看似類似,原因卻完全不同,很容易誤判成機制沒生效。
這套做法的價值在於成本低、擴充容易。同一個專案若有 N 個硬體變體,只要維護 system.prop 中的一組屬性,不必複製 N 份代碼基線,也不必更動既有的 fingerprint 與簽名流程。
Android 自定義 View:單行上下滾動公告
在許多 App 首頁,公告欄會以單行方式持續上下輪播。看似簡單,其實需要處理文字切換、動畫位移與定時更新。 實作思路 可以自訂一個 LinearLayout,內部放置兩
Android 網路開發:抓取線上 XML 並用 ListView 呈現
上次我們示範了如何從網路取得一張圖片,這次換成抓取文字檔,並以實際開發中最常見的 XML 為例。 範例的 XML 內容如下: <?xml version=1.0
Android 自訂彈出式提示框:仿 QQ 退出確認的五種實作思路
從仿 QQ 提示框談起在 Android 應用裡,系統原生提示框往往難以直接符合品牌風格,因此開發者常會自訂彈出式對話框。本文以類似 QQ 退出確認的效果為例,整理五種常
Android 裝置安全提醒:鎖屏密碼與 Wi-Fi 密碼的風險
從安全角度看 Android 的敏感資料 Android 裝置上保存了許多敏感資訊,例如鎖屏憑證、Wi-Fi 設定與應用程式資料。開發者與使用者都應理解,這些資料一旦落入