1

I was following the gradle docs to separate source files per test type in a Java project and I wanted to do the same thing in an Android library project. By default Android plugin com.android.library supports two types of test directories test and androidTest. How do I add say integTest which I wanted to run after test?

sourceSets {
     integTest {
         java.srcDir file('src/integTest/java')
         resources.srcDir file('src/integTest/resources')
     }
}

When I try to add the above sourceSet to the build.gradle, I get the error

ERROR: The SourceSet 'integTest' is not recognized by the Android Gradle Plugin. Perhaps you misspelled something?

Since Android Gradle Plugin doesn't support custom sourceSets like Java Plugin, is there any other way to solve this problem?

Prudhvi
  • 2,108
  • 7
  • 31
  • 51
  • `androidTest.setRoot('integTest')` – Martin Zeitler Jan 29 '19 at 02:29
  • Doesn't that look for test files only in `integTest` folder? From the documentation [here](https://developer.android.com/studio/build/build-variants#configure-sourcesets) `If all the files for a source set are located under a single root directory, you can specify that directory using the setRoot property. When gathering sources for the source set, Gradle looks only in locations relative to the root directory you specify. For example, after applying the configuration below for the androidTest source set, Gradle looks for Java sources only in the src/tests/java/ directory.` – Prudhvi Jan 29 '19 at 03:30

1 Answers1

1

The main reason for the error is defining the sourceSet for integTest inside android, just moving it outside solved the issue. See below for correct build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 28



    defaultConfig {
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

sourceSets {
    integTest {
        java.srcDir file('src/integTest/java')
        resources.srcDir file('src/integTest/resources')
    }
}

configurations {
    integTestCompile.extendsFrom testCompile
    integTestRuntime.extendsFrom testRuntime
}

task integTest(type: Test) {
    group = LifecycleBasePlugin.VERIFICATION_GROUP
    description = 'Runs the integration tests.'
    testClassesDirs = sourceSets.integTest.output.classesDirs
    classpath = sourceSets.integTest.runtimeClasspath
}

check.dependsOn integTest

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:28.0.0'
    testImplementation 'junit:junit:4.12'
    integTestImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Prudhvi
  • 2,108
  • 7
  • 31
  • 51
  • Is it possible to create the same `integTest` task which will run android(instrumentation) tests from custom named sourceSet ? – xyman Jun 02 '19 at 11:37
  • I'm not sure I understood your question correctly, can you give an example? – Prudhvi Jun 05 '19 at 20:44