自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
使用全域設定載入通訊協定
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
為了瞭解本節內容,請先研究 Tradefed 的 @Option。
Tradefed 中的常見選項可讓測試類別從 XML 設定檔或指令列接收其他資訊。這項功能可讓您額外執行一個步驟,並視需要解決部分額外資訊。
檔案選項範例
範例檔案 @option:
@Option(name = 'config-file')
private File mConfigFile;
您可以透過 XML 設定設定上述項目:
<option name="config-file" value="/tmp/file" />
或透過指令:
--config-file /tmp/file
說明
這項功能可讓您將遠端的 File 型 @Options 解析為本機檔案,從使用者角度來看,這可讓這些檔案無縫運作。
如要讓這項功能運作,您必須使用遠端樣式路徑指定檔案。例如:
--config-file gs://bucket/tmp/file
這個路徑會指向 Google Cloud Storage (GCS) 值區中儲存的檔案。Tradefed 看到該遠端路徑後,會嘗試在本機下載檔案,並將其指派給 @Option。這會導致 mConfigFile
變數現在指向本機版本的檔案,可供測試使用。
如果因任何原因無法下載遠端檔案,Tradefed 就會擲回 ConfigurationException
,導致測試無法執行。我們會將缺少這些檔案視為重大失敗,因為部分測試構件也會遺失。
使用查詢參數
您可以使用 ?
在網址中加入查詢參數。例如:gs://bucket/path?unzip=true
。鍵/值 unzip=true
會在 IRemoteFileResolver 介面 實作中提供。
您可以使用下列兩種內建行為:
- unzip:如果設為
true
,且下載的檔案為 ZIP 檔案,系統會自動將該檔案解壓縮至臨時位置。示例:?unzip=true
- 選用:預設為
false
。如果設定為 true
且解析度失敗,系統不會擲回例外狀況,只會不替換檔案。示例:
?optional=true
您也可以透過 --dynamic-download-args key=value
傳遞全域查詢引數,該引數會將鍵/值傳遞至在叫用中嘗試的所有動態下載項目。
支援的通訊協定
官方支援的通訊協定及其對應格式如下:
- Google Cloud Storage,通訊協定:
gs
,格式:gs://<bucket name>/path
- 本機檔案,通訊協定:
file
,格式:file:/local/path
- http 連結,通訊協定:
http
,格式:http://url
- https 連結、通訊協定:
https
、格式:https://url
限制
@Option 的動態解析度目前僅支援少數通訊協定和下載位置。目前只有主要 XML Tradefed 設定可使用 @Option 解析。
如果以套件形式執行,則目前的模組 (AndroidTest.xml
) 預設不會解析檔案。這麼做是為了避免模組建立一些不明的依附元件。您可以在套件層級使用 --enable-module-dynamic-download
來逃逸,但主要套件 (例如 Compatibility Test Suite (CTS) 和 Vendor Test Suite (VTS)) 不會啟用此功能。
實作新通訊協定
支援的通訊協定在 Tradefed 中實作 IRemoteFileResolver 介面,定義通訊協定的短標記,並透過 getSupportedProtocol
在檔案路徑中比對。例如,gs
是用於 Google Cloud Storage 通訊協定。建議實作的介面是 #resolveRemoteFiles(RemoteFileResolverArgs)
,這是長期維護的介面。
實作的通訊協定可新增至harness META-INF 服務檔案,正式啟用支援功能。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Load protocols with global config\n\nIn order to understand this section, first study the Tradefed\n[@Option](/docs/core/tests/tradefed/fundamentals/options).\n\nTypical options in Tradefed allow for test classes to receive additional\ninformation from the XML configuration or command line. This feature allows you\nto go one extra step and resolve some of this additional information if\nnecessary.\n\nFile option example\n-------------------\n\nExample File @option: \n\n @Option(name = 'config-file')\n private File mConfigFile;\n\nThe above can be set via XML configuration: \n\n \u003coption name=\"config-file\" value=\"/tmp/file\" /\u003e\n\nor via command: \n\n --config-file /tmp/file\n\nDescription\n-----------\n\nThe feature allows you to resolve File-typed @Options that are remote into a\nlocal file to be available seamlessly from a user standpoint.\n\nFor this to work, the file needs to be specified with a *remote style path*. For\nexample: \n\n --config-file gs://bucket/tmp/file\n\nThis path points to a file within a Google Cloud Storage (GCS) bucket where it's\nstored. Tradefed upon seeing that remote path, will attempt to download the file\nlocally and assign it to the @Option. This results in the `mConfigFile` variable\nto now point to the local version of the file, which can be used by the test.\n\nIf the remote file cannot be downloaded for any reason, Tradefed will throw a\n`ConfigurationException` that will prevent the test from running. We consider\nmissing those files a critical failure since some test artifacts will also be\nmissing.\n\nUse query parameters\n--------------------\n\nAdding query parameters to a URL is possible using `?`. For example,\n`gs://bucket/path?unzip=true`. The key/value `unzip=true` will be available in\nthe [IRemoteFileResolver\ninterface](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/src/com/android/tradefed/config/remote/IRemoteFileResolver.java)\nimplementation.\n\nTwo built-in behaviors are available:\n\n- unzip: If set to `true` and the downloaded file is a zip, it will be automatically unzipped to a temporary location. Example: `?unzip=true`\n- optional: Defaults to `false`. If set to `true` and the resolution fails, it won't throw an exception and will simply not replace the file. Example: `?optional=true`\n\nYou can also pass global query arguments via `--dynamic-download-args key=value`\nthat will pass the key/value to all the dynamic downloads attempted in the\ninvocation.\n\nSupported protocols\n-------------------\n\nThe officially supported protocols and their corresponding formats are:\n\n- Google Cloud Storage, protocol: `gs`, format: `gs://\u003cbucket name\u003e/path`\n- Local files, protocol: `file`, format: `file:/local/path`\n- http links, protocol: `http`, format: `http://url`\n- https links, protocol: `https`, format: `https://url`\n\nLimitations\n-----------\n\nThe dynamic resolution of @Option currently supports only a limited number of\nprotocols and locations to download from. The resolution of @Option is currently\nenabled only for the main XML Tradefed configuration.\n\nIf running as a suite, current modules (`AndroidTest.xml`) will not resolve the\nfiles by default.\n*This is meant to prevent modules from creating some unknown dependencies* . This\ncan be escaped by using `--enable-module-dynamic-download` at the suite level,\nbut major suites such as the [Compatibility Test Suite\n(CTS)](/docs/compatibility/cts) and [Vendor Test Suite\n(VTS)](/docs/compatibility/vts) will not enable it.\n\nImplement a new protocol\n------------------------\n\nThe protocols that are supported have an implementation in Tradefed of the\n[IRemoteFileResolver\ninterface](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/src/com/android/tradefed/config/remote/IRemoteFileResolver.java)\n,which defines the short tag of the protocol that will be matched in the\nfile path through `getSupportedProtocol`. For example, `gs` is used for the\nGoogle Cloud Storage protocol. The recommended interface to implement is\n`#resolveRemoteFiles(RemoteFileResolverArgs)` which will be the long-term\nmaintained interface.\n\nThe protocols implemented can be added to [the harness META-INF\nservices](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/res/META-INF/services/com.android.tradefed.config.remote.IRemoteFileResolver)\nfile to officially turn on the support."]]