自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
JAR 主機測試
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您應實作 Java 封存檔 (JAR) 主機測試,以便提供軟體的完整程式碼涵蓋率。請按照操作說明建構本機單元測試。編寫小型單元測試,驗證特定函式,僅此而已。
範例
下列藍圖檔案提供簡單的 Hello World JAR 主機測試範例,供您複製並調整以符合需求:platform_testing/tests/example/jarhosttest/Android.bp
這會對應至實際的測試程式碼:platform_testing/tests/example/jarhosttest/test/android/test/example/helloworld/HelloWorldTest.java
為方便您參考,我們已將藍圖檔案的快照畫面附在本文中:
java_test_host {
name: "HelloWorldHostTest",
test_suites: ["general-tests"],
srcs: ["test/**/*.java"],
static_libs: [
"junit",
"mockito",
],
}
開頭的 java_test_host
宣告表示這是 JAR 主機測試。請參閱以下範例:frameworks/base/tools/powermodel/Android.bp
設定
請參閱下文,瞭解下列設定的說明:
指定 java_test_host
模組類型 (在區塊開頭) 時,必須提供 name
設定。這項設定會為模組命名,產生的 JAR 會具有相同的名稱和 .jar
後置字串。在以下範例中,產生的測試 JAR 檔案名稱為 HelloWorldHostTest.jar
。此外,這項設定也會為模組定義 Make 目標名稱,方便您使用 make [options] <HelloWorldHostTest>
建構測試模組及其所有依附元件。
name: "HelloWorldHostTest",
test_suites
設定可讓 Trade Federation 測試輔助程式輕鬆發現測試。您可以在此新增其他測試套件,例如 CTS,以便共用 JAR 主機測試。
test_suites: ["general-tests"],
static_libs
設定會指示建構系統將命名模組的內容納入目前模組的產生 APK。這表示每個命名模組都應產生 .jar
檔案。模組的內容會用於在編譯期間解析 classpath 參照,並納入產生的 APK。
static_libs: [
"junit",
],
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。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,["# JAR host tests\n\nJava archive (JAR) host tests should be implemented to provide complete code\ncoverage of your software. Follow the instructions to [Build local unit\ntests](https://developer.android.com/training/testing/unit-testing/local-unit-tests).\nWrite small unit tests to validate a specific function and nothing more.\n\nExample\n-------\n\nThe following Blueprint file provides a simple Hello World JAR host test example\nto copy and adapt to your needs:\n[platform_testing/tests/example/jarhosttest/Android.bp](https://android.googlesource.com/platform/platform_testing/+/android16-release/tests/example/jarhosttest/Android.bp)\n\nThis corresponds to the actual test code found at:\n[platform_testing/tests/example/jarhosttest/test/android/test/example/helloworld/HelloWorldTest.java](https://android.googlesource.com/platform/platform_testing/+/android16-release/tests/example/jarhosttest/test/android/test/example/helloworld/HelloWorldTest.java)\n\nA snapshot of the Blueprint file is included here for convenience: \n\n java_test_host {\n name: \"HelloWorldHostTest\",\n\n test_suites: [\"general-tests\"],\n\n srcs: [\"test/**/*.java\"],\n\n static_libs: [\n \"junit\",\n \"mockito\",\n ],\n }\n\nThe `java_test_host` declaration at the beginning indicates that this is a JAR\nhost test. See an example of its use in:\n[frameworks/base/tools/powermodel/Android.bp](https://android.googlesource.com/platform/frameworks/base/+/refs/heads/android16-release/tools/powermodel/Android.bp)\n\nSettings\n--------\n\nSee below for explanations of the following settings:\n\n- The `name` setting is required when the `java_test_host` module type is\n specified (at the start of the block). This setting gives a name to your\n module, and the resulting JAR has the same name and a `.jar` suffix. In the\n example below,the resulting test JAR is named `HelloWorldHostTest.jar`. In\n addition, this setting also defines a make target name for your module, so\n that you can use `make [options] \u003cHelloWorldHostTest\u003e` to build your test\n module and all its dependencies.\n\n name: \"HelloWorldHostTest\",\n\n- The `test_suites` setting makes the test easily discoverable by the Trade\n Federation test harness. Other test suites can be added here, such as CTS,\n so that the JAR host test test can be shared.\n\n test_suites: [\"general-tests\"],\n\n- The `static_libs` setting instructs the build system to incorporate the\n contents of the named modules into the resulting APK of the current module.\n This means that each named module is expected to produce a `.jar` file.\n The module's content is used for resolving classpath references during\n compile time and incorporated into the resulting APK.\n\n static_libs: [\n \"junit\",\n ],"]]