Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android gradle src/androidTest/res/layout/mylayout.xml not found in mypackage.R

I'm writing my unit tests using gradle in Android Studio. I have the tests located in:

src/androidTest/java

That works just fine. Now in my tests I want to access a resource that is defined in:

src/androidTest/res

Specifically, I want to get src/androidTest/res/layout/mylayout.xml. The problem is the R class for my test package or any package for that matter has mylayout defined.

I did this on my build.gradle and it didn't make a difference:

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

How do I supply resources to androidTest classes that is only visible to the androidTest?

like image 578
vangorra Avatar asked May 13 '14 23:05

vangorra


2 Answers

I found the solution, add this to your build.gradle file.

sourceSets {
    androidTest {
        java.srcDirs = ['src/androidTest/src']
        resources.srcDirs = ['src/androidTest/src']
    }
}
like image 157
vangorra Avatar answered Nov 15 '22 11:11

vangorra


For me, the solution was to put the source files in src/androidTest/java and the resource files in src/androidTest/res. Those are the standard locations, so you don't need to edit anything in gradle.build.

Plus, rename your R import from

import com.mycompany.myappname.R;

to

import com.mycompany.myappname.test.R;

Source: Configuring res srcDirs for androidTest sourceSet

like image 15
Damnum Avatar answered Nov 15 '22 11:11

Damnum