配置资源srcDirs的androidTest sourceSet资源、srcDirs、sourceSet、androidTest

2023-09-07 14:29:32 作者:柒月流璃

我想包括资源,仅用于测试进行编译。

I'd like to include resources to be compiled only for testing.

我在我的应用程序模块build.gradle文件如下:

I have the following in my app module build.gradle file:

android {

    ...

    sourceSets {
        androidTest {
            java.srcDirs = ['src/androidTest/java']
            res.srcDirs = ['src/androidTest/res']
        }
    }
}

在Java srcDirs路径是正确的,但如果我试图让从androidTest res目录资源,该资源无法找到。

The java srcDirs path is correct, yet if I try to get a resource from the res directory in androidTest, the resource can't be found.

我怎么能在那些只包括了测试androidTest目录资源?

How can I have resources in the androidTest directory that are only included for tests?

推荐答案

原来你并不需要更改默认build.gradle文件什么。

Turns out you don't need to change anything from the default build.gradle file.

的诀窍是自动生成的 R.java 文件是针对不同的资源在 androidTest

The trick is that the auto-generated R.java file is different for resources in androidTest.

如果导入语句研究放在你的主要来源组是这样的:

If the import statement for R somewhere in your main source set looks like:

import com.mycompany.myappname.R;

在测试文件中的import语句研究应该是这样的:

import com.mycompany.myappname.test.R;