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

半世苍凉 提交于 2020-01-01 08:33:57

问题


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?


回答1:


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




回答2:


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

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


来源:https://stackoverflow.com/questions/23643189/android-gradle-src-androidtest-res-layout-mylayout-xml-not-found-in-mypackage-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!