When working with Android development in Kotlin, writing instrumentation tests is an essential part of ensuring your app runs smoothly across devices. However, many developers encounter a frustrating issue where Kotlin instrumentation tests crash immediately on start. In this article, we will dive into the reasons behind this crash, explore common scenarios, and provide solutions with complete code examples to help you resolve the problem quickly.

Understanding Instrumentation Tests
Instrumentation tests run on a physical device or emulator and allow you to test how your Android components interact with the operating system. Unlike unit tests, they have access to system services, UI elements, and can simulate user behavior.
Why Do Instrumentation Tests Crash?
There are several possible causes when instrumentation tests crash at the start:
- Missing AndroidJUnitRunner configuration in
build.gradle
. - Incorrect dependencies for testing libraries (JUnit, Espresso, AndroidX).
- ProGuard or R8 rules stripping required classes.
- Manifest conflicts due to multiple test runners.
- Unsupported emulator/device configurations.
Typical Error Logs
When running instrumentation tests, you may see logs such as:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/androidx.test.runner.AndroidJUnitRunner}: java.lang.ClassNotFoundException: Didn't find class "androidx.test.runner.AndroidJUnitRunner"
Or:
Instrumentation run failed due to Process crashed.
Step-by-Step Solutions
1. Configure Your build.gradle
The most common mistake is not setting the correct test runner in your app/build.gradle
file. Make sure you include:
android { defaultConfig { testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } } dependencies { // JUnit testImplementation "junit:junit:4.13.2" // AndroidX Test androidTestImplementation "androidx.test:core:1.5.0" androidTestImplementation "androidx.test.ext:junit:1.1.5" androidTestImplementation "androidx.test.espresso:espresso-core:3.5.1" }
2. Create a Simple Instrumentation Test
Here is an example of a working Kotlin instrumentation test using Espresso:
package com.example.myapp import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.rule.ActivityTestRule import androidx.test.espresso.Espresso.onView import androidx.test.espresso.matcher.ViewMatchers.withId import androidx.test.espresso.action.ViewActions.click import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) class MainActivityTest { @get:Rule val activityRule = ActivityTestRule(MainActivity::class.java) @Test fun buttonClick_updatesTextView() { // Perform click on button onView(withId(R.id.my_button)).perform(click()) // Verify text is updated onView(withId(R.id.my_textview)) .check(matches(withText("Button Clicked"))) } }
3. Check AndroidManifest.xml
Ensure your test runner is properly defined. Normally, this is managed automatically, but if you have custom configurations, double-check:
4. ProGuard / R8 Rules
If your release builds crash on test start, you may need to add ProGuard rules to keep test classes:
-keep class androidx.test.** { *; } -keep class org.junit.** { *; }
5. Emulator/Device Configuration
Some crashes occur only on specific Android versions or emulators. Always test on a recent emulator with x86_64
images and verify that Google Play services are installed if your app relies on them.
Comparison Table: Unit Test vs Instrumentation Test
Feature | Unit Test | Instrumentation Test |
---|---|---|
Execution | Runs on JVM (local) | Runs on Android device/emulator |
Speed | Very fast | Slower (requires emulator/device) |
Access to Android APIs | No | Yes |
Best for | Business logic | UI, system integration |
Best Practices to Avoid Crashes
- Always specify
testInstrumentationRunner
inbuild.gradle
. - Keep dependencies updated (JUnit, Espresso, AndroidX Test).
- Use ActivityScenarioRule instead of deprecated ActivityTestRule.
- Run tests on multiple emulators (API 21+, API 30+, API 34+).
- Check Gradle Sync and Clean Project if you face repeated crashes.
Final Thoughts
Facing Kotlin instrumentation test crashes on start can be frustrating, but with proper configuration, dependency management, and emulator setup, these issues can be resolved effectively. By following the step-by-step fixes and reviewing the complete Kotlin code examples provided here, you can ensure your Android instrumentation tests run smoothly and reliably.
For more details on Android testing best practices, you can explore the official Android Testing Documentation.