简体   繁体   English

如何将新的源目录添加到Android Studio项目?

[英]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. 因此,最终我试图将我的集成测试与Android Studio项目中的单元测试分开。 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. 所有这些似乎表明,要走的路是为集成测试创建一个新的sourceSet,然后创建一个新的测试任务,该任务将在该源集中构建并运行测试。 I can't get past the first step of creating a source set which is recognized by Android Studio. 我无法超越创建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: 这是我在app / build.gradle中拥有的内容,它的构建没有错误,但不会导致integrationTest源根,我可以向其中添加类:

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

My questions are: 我的问题是:

  1. Where precisely do I have to add the sourceSets block? 我到底要在哪里添加sourceSets块? In build.gradle? 在build.gradle中? in app/build.gradle? 在app / build.gradle中? In app/build.gradle inside the android block? 在android块内的app / build.gradle中?

  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? 一旦我使用正确的语法将源集添加到正确的位置,这是否足以使Android Studio在主要和测试源旁边检测并在UI中呈现它,还是有其他步骤?

edit: 编辑:

I've attempted to follow the instructions in marius' answer, but integrationTest isn't showing up in my build variants. 我尝试遵循marius答案中的指示,但是我的build变体中未显示IntegrationTest。 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. 关于您的第一个问题productFlavors块应位于android块内的app/build.gradle

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 . 关于第二个问题 :将其添加到build.gradle文件后,还需要创建文件夹/src/integrationTest/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. 完成此操作后,同步gradle文件并从“ Build Variant窗口中选择新的“ Build Variant ,以使IDE将其检测为活动的源文件夹。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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