The androidTest folder isn't recognized by Android Studio


I am picking up a project generated by Claude Code and I try to make the tests work.

I have an issue with the tests of the app module. The androidTest folder isn't recognized as a test folder, there is no green indicator on it and there is no launch icon next to the tests.

I have precised:

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

in the defaultConfig.

I have also tried to set the sourcesets manually:

    sourceSets {
        named("test") {
            java.srcDirs("src/test/kotlin")
        }
        named("androidTest") {
            java.srcDirs("src/androidTest/kotlin")
        }
        getByName("androidTest") {
            kotlin.srcDirs("src/androidTest/kotlin")
            java.srcDirs("src/androidTest/kotlin")
        }
    }

I have added the test dependencies:

    androidTestImplementation("androidx.test:runner:1.7.0")
    androidTestImplementation("androidx.test:rules:1.7.0")
    androidTestImplementation("androidx.test.ext:junit:1.2.1")
    // Optional -- UI testing with UI Automator
    androidTestImplementation("androidx.test.uiautomator:uiautomator:2.4.1")
    // Optional -- UI testing with Compose
    androidTestImplementation("androidx.compose.ui:ui-test-junit4:1.12.1")

Any idea how to get it recognized ? A hack using the .idea folder maybe ?

I also tried to delete the .idea folder and resync, invalidate caches and restart.

Any help is appreciated !

1
Nov 10 at 3:37 PM
User Avatarkalnar
#android#android-studio#gradle#testing

Accepted Answer

The Gradle model decides what the IDE marks as a test root, so the .idea folder won’t help here. It gets regenerated from the Gradle sync, and a manual “Mark Directory as > Test Sources Root” is normally overwritten by the next sync. These are the causes I’d check, in this order.

  1. Selected build variant. The androidTest component only exists for one build type, debug by default (see testBuildType). The docs say the androidTest folders are shown as active only for the selected variant. Open View > Tool Windows > Build Variants, pick debug (plus a flavor if you have any), and sync again. If release is selected, you get exactly what you describe: no green marker and no run icons.

  2. Remove the manual sourceSets block. src/androidTest/kotlin is picked up by default, so the block adds nothing. With AGP 9 and built-in Kotlin, which is on by default, adding Kotlin directories through java.srcDirs or kotlin.sourceSets is not supported. The documented way is android.sourceSets { kotlin.directories += ... }. If the sync reports errors, the IDE can keep an incomplete module model, so check Build > Sync output after cleaning this up.

  3. Something in the generated build disabling the test component. Search the build files for an androidComponents { beforeVariants { ... } } block that turns off androidTest for the variant. Also check that the app module has the Android facet under File > Project Structure > Modules.

  4. Check whether Gradle itself sees the tests. Run ./gradlew :app:assembleDebugAndroidTest (exact task names may differ slightly between AGP versions). If it compiles your sources, the build is fine and the problem is only IDE state. If the task doesn’t exist, the component is disabled in the build.

Unrelated to the recognition problem, but you’ll hit it next: Compose UI tests also need androidTestImplementation(platform(<compose bom>)) and debugImplementation("androidx.compose.ui:ui-test-manifest").

If none of this helps, please add your AGP and Android Studio versions and the full app/build.gradle.kts.

User AvatarAlbatany
Nov 10 at 4:07 PM
0