简体   繁体   中英

How to add a new source directory to an Android Studio project?

So ultimately I'm trying to separate my integration tests from the unit tests in an Android Studio project. I've found a few resources on the subject:

All these seem to indicate that the way to go is to create a new sourceSet for the integration tests, and then to create a new test task which builds and runs the tests in that source set. I can't get past the first step of creating a source set which is recognized by Android Studio.

Here's what I have within app/build.gradle, which builds without errors, but does not result in an integrationTest source root I can add classes to:

android{
    ...
    sourceSets{
        integrationTest {
           java.srcDir('src/integrationTest/java')
        }
    }
}

My questions are:

  1. Where precisely do I have to add the sourceSets block? In build.gradle? in app/build.gradle? In app/build.gradle inside the android block?

  2. Once I've added my source set in he right place using the correct syntax, is this sufficient for Android Studio to detect and present it in the UI along side the main and test sources, or are there additional steps?

edit:

I've attempted to follow the instructions in marius' answer, but integrationTest isn't showing up in my build variants. Here's what I'm seeing:

构建变体窗口

This is enough:

android{
    ...
    productFlavors{
        integrationTest {
        }
    }
}

Regarding your 1st question : The productFlavors block should be in your app/build.gradle , inside android block.

Regarding your 2nd question : Once you add this to your build.gradle file, you also need to create your folders /src/integrationTest and /src/integrationTest/java . Once that is done, sync your gradle files and choose your new Build Variant from the Build Variant window, in order for the IDE to detect it as the active source folder.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM