自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
使用 Trade Federation 與 Android 的 Scripting Layer
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
適用於 Android 的 Scripting Layer (SL4A) 是一組自動化工具,可以不依賴平台的方式呼叫 Android API。它支援透過 adb
進行的遠端自動化,以及透過一系列輕量轉譯層在裝置端執行指令碼。
專案位於 platform/external/sl4a。
使用
您可以按照 SL4A README 手動建構及安裝。透過 Tradefed 執行時,您可以利用某些 harness 公用程式,讓使用更輕鬆。
下載並安裝
您可以先查看 BT-discovery-sl4a.xml,這是使用兩部裝置的 Tradefed 設定範例。SL4A.apk
可在大多數裝置版本的 tests
資料夾中使用。
上述 Tradefed 範例會自動擷取版本、對兩部裝置進行閃燈作業,並在兩部裝置上安裝 SL4A.apk
。您可以執行如下指令碼:
source build/envsetup.sh
lunch
make sl4a
tradefed.sh run google/example/BT-discovery-sl4a
或在建構完成後:
./tradefed.sh run google/example/BT-discovery-sl4a
使用 SL4A 在 Tradefed 中編寫測試
您可以按照上述測試範例操作:Sl4aBluetoothDiscovery.java。這就是在 Tradefed 測試中使用 SL4A 的流程示例。
SL4A API 說明文件
您可以產生可透過 SL4A 使用的完整回呼清單。在 SL4A 來源目錄 platform/external/sl4a/
中執行下列指令:
python Docs/generate_api_reference_md.py
在「Docs」目錄中,您會看到 ApiReference.md
檔案,其中包含 SL4A 提供的 RPC 函式,以及 RPC 函式的說明文件。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Use Trade Federation with Scripting Layer for Android\n\nScripting Layer for Android, SL4A, is an\nautomation toolset for calling Android APIs in a platform-independent manner.\nIt supports both remote automation via `adb` and execution of scripts\nfrom on-device via a series of lightweight translation layers.\n\nThe project is located at [platform/external/sl4a](https://android.googlesource.com/platform/external/sl4a/).\n\nUse\n---\n\nYou can follow the [SL4A README](https://android.googlesource.com/platform/external/sl4a/+/refs/heads/android16-release/README.md)\nto build and install it manually. And when running through Tradefed, you\ncan take advantage of some of the harness utilities to make use easier.\n\n### Download and install\n\nYou can start by reviewing\n[BT-discovery-sl4a.xml](https://android.googlesource.com/platform/tools/tradefederation/contrib/+/refs/heads/android16-release/res/config/google/example/BT-discovery-sl4a.xml),\nan example Tradefed configuration that uses two devices. The `SL4A.apk` is\navailable in most device builds within their `tests` folder.\n\nThe Tradefed example above automatically fetches the builds, flashes both\ndevices and installs `SL4A.apk` on both devices. You can run it like so: \n\n source build/envsetup.sh\n lunch\n make sl4a\n tradefed.sh run google/example/BT-discovery-sl4a\n\nOr once built: \n\n ./tradefed.sh run google/example/BT-discovery-sl4a\n\n### Write a test in Tradefed using SL4A\n\nYou can follow the test sample describe above:\n[Sl4aBluetoothDiscovery.java](https://android.googlesource.com/platform/tools/tradefederation/contrib/+/refs/heads/android16-release/src/com/android/tradefed/Sl4aBluetoothDiscovery.java).\nThis gives a good example of the flow to use SL4A within a Tradefed test.\n\n### SL4A API documentation\n\nThe complete list of callbacks available through SL4A can be generated. From the\nSL4A source directory, `platform/external/sl4a/`, run this command: \n\n python Docs/generate_api_reference_md.py\n\nIn the Docs directory there will be an `ApiReference.md` file that contains\nthe RPC functions available in SL4A, as well as documentation for the RPC\nfunctions."]]