Launch single unit test from gradle test task in android project

I need a gradle test task that launches specific unit test.

Here is my android project root level build gradle file

plugins {
id 'com.android.application' version '8.0.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.9.10' apply false
}

app level build gradle file

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    namespace 'com.example.test'
    compileSdk 34

    defaultConfig {
        applicationId "com.example.test"
        minSdk 24
        targetSdk 34
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = '17'
    }
}

sourceSets {
    test {
        java.srcDir 'src/test/java'
        resources.srcDir 'src/test/resources'
    }
}

task sampleTask(type: Test) {
    testClassesDirs = sourceSets.test.output.classesDirs
    classpath = sourceSets.test.runtimeClasspath

    filter {
        includeTestsMatching("*Example*")
    }
}

dependencies {
    implementation 'androidx.core:core-ktx:1.12.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.11.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

here is the unit test

package com.example.test

import org.junit.Test

import org.junit.Assert.*

class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        assertEquals(4, 2 + 2)
    }
}

here is the output

Task :app:compileTestJava NO-SOURCE
Task :app:processTestResources NO-SOURCE
Task :app:testClasses UP-TO-DATE
Task :app:sampleTask NO-SOURCE
BUILD SUCCESSFUL in 282ms

What should I do to make the unit test launched?

PS
if I run the same task with gradle 8.2 then I receive error

FAILURE: Build failed with an exception.
* What went wrong:
Could not determine the dependencies of task ':app:compileTestJava'.
> Resolving dependency configuration 'testAnnotationProcessor' is not allowed as it is defined as 'canBeResolved=false'.
  Instead, a resolvable ('canBeResolved=true') dependency configuration that extends 'testAnnotationProcessor' should be resolved.

Where exactly do you have the Java file of the unit test?
Task :app:compileTestJava NO-SOURCE suggests that you do not have it in the correct directory.
Besides that, the java.srcDir and resources.srcDir are pointless, those are the defaults anyway.

The testAnnotationProcessor error suggests that you use some plugin that is not compatible with the Gradle version you try to use. Can you share a build --scan or at least --stacktrace output? That should show where exactly this is coming from.

Where exactly do you have the Java file of the unit test?

package com.example.test

Besides that, the java.srcDir and resources.srcDir are pointless, those are the defaults anyway.

If I dont put it that way, then sourceSets are not available inside sampleTask.
Here is the error message for this case

A problem occurred evaluating project ':app'.
> Could not get unknown property 'test' for SourceSet container of type org.gradle.api.internal.tasks.DefaultSourceSetContainer.

Ah, that means there is no source set called test.
That’s probably different in an Android build then.
I’m not really familiar with Android builds.
Are you sure you are trying to do the right thing?

Also I’d like to suggest using Kotlin DSL instead of Groovy DSL.
By now it is the default DSL, you immediately get type-safe build scripts, actually helpful error messages if you mess up the syntax, and amazingly better IDE support when using a good IDE like IntelliJ or Android Studio.

Ah, that means there is no source set called test.
That’s probably different in an Android build then.
I’m not really familiar with Android builds.

there is a task which shows that sourceSets are available,
I’ll provide output of this task below.
These sourceSets are not available through sourceSets or project.sourceSets.
Instead I can call android.sourceSets.getbyName("test"), but it is not the same.
It’s AndroidSourceSet and there is no output property in it.

> Task :app:sourceSets

------------------------------------------------------------
Project ':app'
------------------------------------------------------------

androidTest
-----------
Compile configuration: androidTestCompile
build.gradle name: android.sourceSets.androidTest
Java sources: [app/src/androidTest/java]
Kotlin sources: [app/src/androidTest/kotlin, app/src/androidTest/java]
Manifest file: app/src/androidTest/AndroidManifest.xml
Android resources: [app/src/androidTest/res]
Assets: [app/src/androidTest/assets]
AIDL sources: [app/src/androidTest/aidl]
RenderScript sources: [app/src/androidTest/rs]
Baseline profile sources: [app/src/androidTest/baselineProfiles]
JNI sources: [app/src/androidTest/jni]
JNI libraries: [app/src/androidTest/jniLibs]
Java-style resources: [app/src/androidTest/resources]

androidTestDebug
----------------
Compile configuration: androidTestDebugCompile
build.gradle name: android.sourceSets.androidTestDebug
Java sources: [app/src/androidTestDebug/java]
Kotlin sources: [app/src/androidTestDebug/kotlin, app/src/androidTestDebug/java]
Manifest file: app/src/androidTestDebug/AndroidManifest.xml
Android resources: [app/src/androidTestDebug/res]
Assets: [app/src/androidTestDebug/assets]
AIDL sources: [app/src/androidTestDebug/aidl]
RenderScript sources: [app/src/androidTestDebug/rs]
Baseline profile sources: [app/src/androidTestDebug/baselineProfiles]
JNI sources: [app/src/androidTestDebug/jni]
JNI libraries: [app/src/androidTestDebug/jniLibs]
Java-style resources: [app/src/androidTestDebug/resources]

androidTestRelease
------------------
Compile configuration: androidTestReleaseCompile
build.gradle name: android.sourceSets.androidTestRelease
Java sources: [app/src/androidTestRelease/java]
Kotlin sources: [app/src/androidTestRelease/kotlin, app/src/androidTestRelease/java]
Manifest file: app/src/androidTestRelease/AndroidManifest.xml
Android resources: [app/src/androidTestRelease/res]
Assets: [app/src/androidTestRelease/assets]
AIDL sources: [app/src/androidTestRelease/aidl]
RenderScript sources: [app/src/androidTestRelease/rs]
Baseline profile sources: [app/src/androidTestRelease/baselineProfiles]
JNI sources: [app/src/androidTestRelease/jni]
JNI libraries: [app/src/androidTestRelease/jniLibs]
Java-style resources: [app/src/androidTestRelease/resources]

debug
-----
Compile configuration: debugCompile
build.gradle name: android.sourceSets.debug
Java sources: [app/src/debug/java]
Kotlin sources: [app/src/debug/kotlin, app/src/debug/java]
Manifest file: app/src/debug/AndroidManifest.xml
Android resources: [app/src/debug/res]
Assets: [app/src/debug/assets]
AIDL sources: [app/src/debug/aidl]
RenderScript sources: [app/src/debug/rs]
Baseline profile sources: [app/src/debug/baselineProfiles]
JNI sources: [app/src/debug/jni]
JNI libraries: [app/src/debug/jniLibs]
Java-style resources: [app/src/debug/resources]

main
----
Compile configuration: compile
build.gradle name: android.sourceSets.main
Java sources: [app/src/main/java]
Kotlin sources: [app/src/main/kotlin, app/src/main/java]
Manifest file: app/src/main/AndroidManifest.xml
Android resources: [app/src/main/res]
Assets: [app/src/main/assets]
AIDL sources: [app/src/main/aidl]
RenderScript sources: [app/src/main/rs]
Baseline profile sources: [app/src/main/baselineProfiles]
JNI sources: [app/src/main/jni]
JNI libraries: [app/src/main/jniLibs]
Java-style resources: [app/src/main/resources]

release
-------
Compile configuration: releaseCompile
build.gradle name: android.sourceSets.release
Java sources: [app/src/release/java]
Kotlin sources: [app/src/release/kotlin, app/src/release/java]
Manifest file: app/src/release/AndroidManifest.xml
Android resources: [app/src/release/res]
Assets: [app/src/release/assets]
AIDL sources: [app/src/release/aidl]
RenderScript sources: [app/src/release/rs]
Baseline profile sources: [app/src/release/baselineProfiles]
JNI sources: [app/src/release/jni]
JNI libraries: [app/src/release/jniLibs]
Java-style resources: [app/src/release/resources]

test
----
Compile configuration: testCompile
build.gradle name: android.sourceSets.test
Java sources: [app/src/test/java]
Kotlin sources: [app/src/test/kotlin, app/src/test/java]
Java-style resources: [app/src/test/resources]

testDebug
---------
Compile configuration: testDebugCompile
build.gradle name: android.sourceSets.testDebug
Java sources: [app/src/testDebug/java]
Kotlin sources: [app/src/testDebug/kotlin, app/src/testDebug/java]
Java-style resources: [app/src/testDebug/resources]

testFixtures
------------
Compile configuration: testFixturesCompile
build.gradle name: android.sourceSets.testFixtures
Java sources: [app/src/testFixtures/java]
Kotlin sources: [app/src/testFixtures/kotlin, app/src/testFixtures/java]
Java-style resources: [app/src/testFixtures/resources]

testFixturesDebug
-----------------
Compile configuration: testFixturesDebugCompile
build.gradle name: android.sourceSets.testFixturesDebug
Java sources: [app/src/testFixturesDebug/java]
Kotlin sources: [app/src/testFixturesDebug/kotlin, app/src/testFixturesDebug/java]
Java-style resources: [app/src/testFixturesDebug/resources]

testFixturesRelease
-------------------
Compile configuration: testFixturesReleaseCompile
build.gradle name: android.sourceSets.testFixturesRelease
Java sources: [app/src/testFixturesRelease/java]
Kotlin sources: [app/src/testFixturesRelease/kotlin, app/src/testFixturesRelease/java]
Java-style resources: [app/src/testFixturesRelease/resources]

testRelease
-----------
Compile configuration: testReleaseCompile
build.gradle name: android.sourceSets.testRelease
Java sources: [app/src/testRelease/java]
Kotlin sources: [app/src/testRelease/kotlin, app/src/testRelease/java]
Java-style resources: [app/src/testRelease/resources]

Are you sure you are trying to do the right thing?

Doc says that I have to provide testClassesDirs and classPath. So I was trying to get it from sourceSets, I’m not sure if its the right way on android, I just followed examples

In order to operate, the `Test` task type requires just two pieces of information:
* Where to find the compiled test classes (property: [Test.getTestClassesDirs()])
* The execution classpath, which should include the classes under test as well as the test library that you’re using (property: [Test.getClasspath()])

When you’re using a JVM language plugin — such as the [Java Plugin] — you will automatically get the following:

* A dedicated `test` source set for unit tests
* A `test` task of type `Test` that runs those unit tests

The testAnnotationProcessor error suggests that you use some plugin that is not compatible with the Gradle version you try to use. Can you share a build --scan or at least --stacktrace output? That should show where exactly this is coming from.

here is build scan for gradle 8.2. This version gives error, 8.0 runs fine

https://scans.gradle.com/s/bychs6fj5ppyi

Probably another effect of what you try.
As you see in that build scan in the deprecations, the 4 test related configurations are created that are reserved for creation by Gradle.