How to read a file from src/instrumentTest/resources directory in Android?

后端 未结 1 908
礼貌的吻别
礼貌的吻别 2020-12-18 07:53

I have an Android application using the new standard gradle layout:

  src/main/java
  src/main/resources
  src/instrumentTest/java
  src/instrumentTest/resources
         


        
相关标签:
1条回答
  • 2020-12-18 08:39

    You can use the assets folder of the build type your are testing. If you are testing the debug type (which is the default one):

    src/debug/res/assets
    

    And then you access those files with

    InputStream raw = context.getAssets().open("filename.ext");
    String myJson = new Scanner(raw).useDelimiter("\\A").next();
    

    The trick of using the debug build type prevent your testing resources to be packed in the final apk.

    0 讨论(0)
提交回复
热议问题