Adding layout resources to androidTest

前端 未结 3 554
清酒与你
清酒与你 2021-02-13 01:13

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

3条回答
  •  醉话见心
    2021-02-13 02:05

    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!

提交回复
热议问题