Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding layout resources to androidTest

I would like to add layout xml files into my androidTest folder to be used only for testing.

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.

like image 642
Abdullah Avatar asked Nov 21 '16 07:11

Abdullah


People also ask

What is layout resources in Android?

A layout resource defines the architecture for the UI in an Activity or a component of a UI. file location: res/layout/filename.xml. The filename will be used as the resource ID. compiled resource datatype: Resource pointer to a View (or subclass) resource.

What is purpose of resource folder in Android?

The res/values folder is used to store the values for the resources that are used in many Android projects to include features of color, styles, dimensions etc.

What are the different layouts in Android?

Android Layout TypesTableLayout is a view that groups views into rows and columns. AbsoluteLayout enables you to specify the exact location of its children. The FrameLayout is a placeholder on screen that you can use to display a single view. ListView is a view group that displays a list of scrollable items.


3 Answers

It is tricky to add xml resources to androidTest.

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.

That means you cannot put additional layout xml to your original application from tests that are in the androidTest folder.

Solution:

Alternatively,

  • you can create a buildType called espresso.
  • 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.
  • Then, use testBuildType 'espresso'

Your build.gradle should look like this:

android {
  testBuildType 'espresso'

  buildTypes {
    espresso.initWith(buildTypes.release)
  }
}

dependencies {
  espressoCompile 'somedependency' // you can even have special dependencies
}

When you run your espresso tests around that flavor, you will have an access to additional layout xml files you added.

Should look like this:

enter image description here

like image 197
tasomaniac Avatar answered Oct 30 '22 06:10

tasomaniac


That's easy! In general, you should just put your resources under the src/androidTest/res folder. And that is! Then you can use it in your src/androidTest/java files. Yes, you can't use test layouts in your production APK, but you can use your test layouts in your test APK.

There're some problems that might confuse you. For instance autocompletion works well not so very often, but, anyway, it builds and works.

Recently I wrote custom control for masked EditText so I don't want to put any activity into the library, but I do want to have an activity to check the view and I do want inflate it from XML. You can see the whole code on the github page, here're some key moments:

$ tree androidTest/
androidTest/
├── AndroidManifest.xml
├── java
│   └── ru
│       └── egslava
│           └── lib_phone
│               ├── MainActivityTest.java
│               ├── TestActivity.java
│               └── actions
│                   ├── HintViewAction.java
│                   ├── KeepHintViewAction.java
│                   └── SetTextViewAction.java
└── res
    ├── layout
    │   └── activity_main.xml
    └── values
        └── styles.xml

So you can see, that under androidTest there's some kind of a separate project with its own manifest that registers Activity and so on :-) I would share more files, but it's just a project, no more and you always can look up the link.

The only thing that I'd like to warn you, that you should be ready that Android Studio will show you that your project contains errors even if that's not true :-) Good luck!

Be ready that AndroidStudio will show you that your project contains error even if that's not true

like image 25
Slava Avatar answered Oct 30 '22 07:10

Slava


Cannot comment, but wanted to further add to @Slava's answer. If someone can add it as a comment, by all means.

Try suppressing the lint errors with the accepted answer from this question. Android Studio Remove lint error

like image 27
Lennin Marte Avatar answered Oct 30 '22 05:10

Lennin Marte