自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
通过全局配置加载协议
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
为了理解本部分内容,我们先研究一下 Tradefed @Option。
Tradefed 中的典型选项允许测试类从 XML 配置或命令行接收附加信息。此功能允许您再往前迈一步,那就是在必要时解析其中某些附加信息。
文件选项示例
文件 @option 示例:
@Option(name = 'config-file')
private File mConfigFile;
上例可以通过 XML 配置来设置:
<option name="config-file" value="/tmp/file" />
也可以通过以下命令来设置:
--config-file /tmp/file
说明
借助此功能,您可以从用户的角度将远程的文件类型 @Option 解析为可无缝使用的本地文件。
为实现此目的,需要使用远程样式路径指定文件。 例如:
--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
- optional:默认值为
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
来规避此限制,但是诸如兼容性测试套件 (CTS) 和供应商测试套件 (VTS) 等主要套件将无法启用此功能。
实现新协议
受支持的协议在 Tradefed 中有 IRemoteFileResolver 接口的实现,该实现定义了将在文件路径中通过 getSupportedProtocol
匹配的协议的短标记。例如,gs
用于 Google Cloud Storage 协议。 建议实现的接口是 #resolveRemoteFiles(RemoteFileResolverArgs)
,该接口将需要长期保持。
可以将实现的协议添加到自动化测试框架 META-INF 服务文件中,以正式启用支持。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[null,null,["最后更新时间 (UTC):2025-03-26。"],[],[],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."]]