You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中ExampleInstrumentedTest.java与ExampleUnitTest.java的作用、删除影响及上架问题

Hey fellow Android dev! Let's clear up the confusion around these two default test classes that come with every new Android project:

What are ExampleInstrumentedTest.java and ExampleUnitTest.java used for?

ExampleUnitTest.java

  • This is a local unit test class, designed to run directly on your development machine's JVM—no Android device or emulator required.
  • It's meant for testing pure Java/Kotlin logic that doesn't depend on Android framework APIs, like utility classes, data parsing methods, or business logic decoupled from the Android SDK.
  • The default sample code tests a simple addition operation, just to show you the basic structure of a unit test using the JUnit framework.

ExampleInstrumentedTest.java

  • This is an instrumented test class, which must run on a physical Android device or emulator.
  • It's for testing code that relies on the Android framework, like Activity lifecycle behavior, UI interactions (you'd typically use Espresso for this), Content Provider operations, or logic that needs real device context (e.g., sensor data, permission checks).
  • The default sample verifies your app's package name is correct, demonstrating how to use AndroidX Test libraries (like JUnit + AndroidJUnitRunner) for instrumented testing.
What happens if you delete these classes?
  • First and foremost: your app's core functionality won't be affected at all. These are test-only files, completely separate from your production code.
  • You'll lose the default test templates. If you decide to write tests later, you'll have to create new test classes from scratch (though Android Studio makes this easy with right-click menus and wizards).
  • If you have a CI/CD pipeline set up:
    • By default, deleting them won't break your build—Gradle's test tasks will just skip since there are no test cases to run.
    • The only exception is if you've customized your build to enforce test coverage requirements; in that case, a 0% coverage score might trigger a pipeline failure, but that's a custom rule, not a default behavior.
Will deleting them cause issues when publishing to Google Play?
  • Absolutely not. Google Play only cares about your production APK/AAB, which doesn't include test code by default.
  • When you build a release version (using commands like gradlew assembleRelease), Gradle automatically excludes all files in the test/ and androidTest/ directories from the final package. Since these classes live in those directories, they never make it into your release build—so deleting them has zero impact on publishing.

内容的提问来源于stack exchange,提问作者Chitransh Srivastava

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:00:43