自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
指定準備者
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
系統會在測試層級中定義的測試前,呼叫目標準備工具。這樣一來,您就能設定任何裝置,讓測試順利執行。
基礎介面
基礎介面為 ITargetPreparer
,可讓 setUp
方法實作。建議您實作基本抽象類別 BaseTargetPreparer
,該類別提供內建停用功能,可輕鬆停用準備工具。
更清爽的介面
setUp
的自然擴充功能是 tearDown
,由不同的介面 ITargetCleaner
提供。這會提供 tearDown
介面,可在測試執行後清除 setUp
中執行的任何作業。
BaseTargetPreparer
類別也會擴充 ITargetCleaner
。
建議
建議每個準備工具僅限於單一主要函式,例如安裝 APK 或執行指令。這樣一來,您就能更輕鬆地重複使用準備工具。
新增新的準備者前,請先查看可用的準備者清單,以免重複工作。您可以在 tools/tradefederation/core/src/com/android/tradefed/targetprep/
中找到準備者。
XML 設定
物件代碼為 target_preparer
,例如:
<target_preparer class="com.android.tradefed.targetprep.InstallApkSetup">
<option name="install-arg" value="-d"/>
</target_preparer>
如需相關背景資訊,請參閱「設定套件」。
頂層設定
如果在頂層設定中指定,準備工具只會針對每部裝置執行一次。例如 cts-common.xml
,這是 Android 相容性測試套件 (CTS) 測試的頂層設定。
模組層級設定
如果在模組層級指定,準備工具一律會在該模組前執行。例如 backup/AndroidTest.xml
,可定義 Tradefed 執行 backup
CTS 模組的方式。
請注意,雖然預備程序會在模組之前執行,但會在任何系統狀態檢查器「之後」執行。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Target preparers are invoked before the tests in the\n[test level](/docs/core/tests/tradefed/testing/through-suite/setup#definitions)\nin which they are defined. This allows the setup of any device for tests to run\nsmoothly.\n\nBase interface\n--------------\n\nThe base interface is\n[`ITargetPreparer`](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/src/com/android/tradefed/targetprep/ITargetPreparer.java),\nwhich allows implementation of a `setUp` method to be executed. We\nrecommend implementing our basic abstract class\n[`BaseTargetPreparer`](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/src/com/android/tradefed/targetprep/BaseTargetPreparer.java),\nwhich provides a built-in disablement feature to easily disable a preparer.\n\nCleaner interface\n-----------------\n\nThe natural extension of `setUp` is `tearDown` and is provided by a different\ninterface,\n[`ITargetCleaner`](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/src/com/android/tradefed/targetprep/ITargetCleaner.java). That provides the `tearDown` interface\nthat allows cleaning up anything that was done in `setUp` after the test\nexecution.\n\nThe `BaseTargetPreparer` class also extends `ITargetCleaner`.\n\nRecommendations\n---------------\n\nWe recommend each preparer be limited to a single main function, for example\ninstalling an APK or running a command. This allows for easier reuse of\npreparers.\n\nAlso check the list of available preparers before adding a new one to\navoid duplicating work. Preparers are available in [`tools/tradefederation/core/src/com/android/tradefed/targetprep/`](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/src/com/android/tradefed/targetprep/).\n\nXML configuration\n-----------------\n\nThe object tag is `target_preparer`, for example: \n\n \u003ctarget_preparer class=\"com.android.tradefed.targetprep.InstallApkSetup\"\u003e\n \u003coption name=\"install-arg\" value=\"-d\"/\u003e\n \u003c/target_preparer\u003e\n\nAlso refer to\n[Set up suites](/docs/core/tests/tradefed/testing/through-suite/setup)\nfor context.\n\n### Top-level setup\n\nIf specified in a top-level setup, the preparer runs only once for each\ndevice. An example is\n[`cts-common.xml`](https://android.googlesource.com/platform/cts/+/refs/heads/android16-release/tools/cts-tradefed/res/config/cts-common.xml),\nwhich is a top-level setup for Android Compatibility Test Suite (CTS) tests.\n\n### Module-level setup\n\nIf specified at the module level, the preparer always runs before that\nmodule. An example is\n[`backup/AndroidTest.xml`](https://android.googlesource.com/platform/cts/+/refs/heads/android16-release/tests/backup/AndroidTest.xml),\nwhich defines how Tradefed runs the `backup` CTS module.\n\nNote that while the preparer runs before the module, it runs *after* any\n[system status checkers](/docs/core/tests/tradefed/testing/through-suite/system-status-checker)."]]