将布局资源添加到androidTest [英] Adding layout resources to androidTest
问题描述
我想将布局xml文件添加到我的androidTest
文件夹中,仅用于测试.
I would like to add layout xml files into my androidTest
folder to be used only for testing.
我将res/layout
文件夹添加到androidTest
,并尝试向其中添加布局文件.但是它为xmlns:android="http://schemas.android.com/apk/res/android"
I added res/layout
folder to androidTest
and tried to add a layout file to it. But it gives error URI is not registered
for xmlns:android="http://schemas.android.com/apk/res/android"
以某种方式,项目无法将其识别为有效的布局文件.
Somehow the project does not recognize it as valid layout file.
推荐答案
向androidTest
添加xml资源非常困难.
It is tricky to add xml resources to androidTest
.
Android Instrumentation测试会创建另一个APK,以针对您的原始应用程序运行测试.尽管您可以从主应用程序访问Context
和对象,但是您无法修改应用程序生成的APK文件.
Android Instrumentation tests create another APK to run the tests against your original application. Although you can access your Context
and objects from your main application, you cannot modify the generated APK file of your app.
这意味着您无法通过androidTest
文件夹中的测试将其他布局xml放置到原始应用程序中.
That means you cannot put additional layout xml to your original application from tests that are in the androidTest
folder.
或者,
- 您可以创建一个名为
espresso
的buildType
. - 然后,创建一个
espresso
文件夹,您可以在其中放置要添加的任何Java或Android资源.- 您甚至可以在那里修改您的
AndroidManifest
.
- you can create a
buildType
calledespresso
. - Then, create an
espresso
folder where you can put any java or Android resource you want to add.- You can even modify your
AndroidManifest
there.
您的
build.gradle
应该看起来像这样:Your
build.gradle
should look like this:android { testBuildType 'espresso' buildTypes { espresso.initWith(buildTypes.release) } } dependencies { espressoCompile 'somedependency' // you can even have special dependencies }
围绕该风味运行浓缩咖啡测试时,您将有权访问添加的其他布局xml文件.
When you run your espresso tests around that flavor, you will have an access to additional layout xml files you added.
应如下所示:
这篇关于将布局资源添加到androidTest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- You can even modify your
- 您甚至可以在那里修改您的