自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
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
文件。模块内容将用于在编译时解析类路径引用,并合并到生成的 APK 中。
static_libs: [
"junit",
],
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[null,null,["最后更新时间 (UTC):2025-03-26。"],[],[],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 ],"]]