Android Studio + Gradle and AndroidTest res/raw












0














I have an Android app. And I want to write instrumentation tests.



I want to put some specific files in the res/rawfolder for the test apk.



I place them in the androidTest/src/res/raw folder and reference them in code with R.raw.file. However the app references some other resource file, which one in my main source set.



How can I ensure that my test apk gets the proper files from its own res/raw folder?










share|improve this question





























    0














    I have an Android app. And I want to write instrumentation tests.



    I want to put some specific files in the res/rawfolder for the test apk.



    I place them in the androidTest/src/res/raw folder and reference them in code with R.raw.file. However the app references some other resource file, which one in my main source set.



    How can I ensure that my test apk gets the proper files from its own res/raw folder?










    share|improve this question



























      0












      0








      0







      I have an Android app. And I want to write instrumentation tests.



      I want to put some specific files in the res/rawfolder for the test apk.



      I place them in the androidTest/src/res/raw folder and reference them in code with R.raw.file. However the app references some other resource file, which one in my main source set.



      How can I ensure that my test apk gets the proper files from its own res/raw folder?










      share|improve this question















      I have an Android app. And I want to write instrumentation tests.



      I want to put some specific files in the res/rawfolder for the test apk.



      I place them in the androidTest/src/res/raw folder and reference them in code with R.raw.file. However the app references some other resource file, which one in my main source set.



      How can I ensure that my test apk gets the proper files from its own res/raw folder?







      android android-instrumentation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 22:54









      ByteArtisan

      2,44533057




      2,44533057










      asked Mar 11 '15 at 17:46









      Tiago Veloso

      3,774165178




      3,774165178
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Your path is wrong. The path you want is src/androidTest/res/raw.



          Files in this directory will replace files in the src/main/res/raw.



          New files in this directory, need to be accessed by the R class in the test package and not the app's R file.



          -- update



          If the problem is that the Android Studio tells you that the R class does not exist in the test package, then that is caused by Android Studio not building the test R class until you try to run the unit tests. Fix this by ignoring the errors on the screen and try to run the test. The R class will be generated and the tests will run (assuming the were no other errors).



          If the problem is that reading the file produces the wrong contents, then you're reading the file from the wrong context. You need to use getContext from Instrumentation.



          public class ResourceLoadTest extends InstrumentationTestCase {

          public void testLoadResource() throws IOException {
          InputStream inputStream = getInstrumentation().getContext().getResources().openRawResource(com.example.app.test.R.raw.hello);
          try {
          Scanner scanner = new Scanner(inputStream, "UTF-8");
          assertEquals("hello", scanner.nextLine());
          } finally {
          inputStream.close();
          }

          }
          }





          share|improve this answer























          • My path was a typo I have it like that. The thing is I Can't seem to access my test package R's class.
            – Tiago Veloso
            Mar 12 '15 at 15:59










          • Your update is worth gold. Was always wondering why it worked on some projects, while it doesn't seem to work on others. Then suddenly, out of nothing it started to work. Never figured out the root cause of this.
            – Rafael T
            May 25 '18 at 14:55











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f28993767%2fandroid-studio-gradle-and-androidtest-res-raw%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Your path is wrong. The path you want is src/androidTest/res/raw.



          Files in this directory will replace files in the src/main/res/raw.



          New files in this directory, need to be accessed by the R class in the test package and not the app's R file.



          -- update



          If the problem is that the Android Studio tells you that the R class does not exist in the test package, then that is caused by Android Studio not building the test R class until you try to run the unit tests. Fix this by ignoring the errors on the screen and try to run the test. The R class will be generated and the tests will run (assuming the were no other errors).



          If the problem is that reading the file produces the wrong contents, then you're reading the file from the wrong context. You need to use getContext from Instrumentation.



          public class ResourceLoadTest extends InstrumentationTestCase {

          public void testLoadResource() throws IOException {
          InputStream inputStream = getInstrumentation().getContext().getResources().openRawResource(com.example.app.test.R.raw.hello);
          try {
          Scanner scanner = new Scanner(inputStream, "UTF-8");
          assertEquals("hello", scanner.nextLine());
          } finally {
          inputStream.close();
          }

          }
          }





          share|improve this answer























          • My path was a typo I have it like that. The thing is I Can't seem to access my test package R's class.
            – Tiago Veloso
            Mar 12 '15 at 15:59










          • Your update is worth gold. Was always wondering why it worked on some projects, while it doesn't seem to work on others. Then suddenly, out of nothing it started to work. Never figured out the root cause of this.
            – Rafael T
            May 25 '18 at 14:55
















          2














          Your path is wrong. The path you want is src/androidTest/res/raw.



          Files in this directory will replace files in the src/main/res/raw.



          New files in this directory, need to be accessed by the R class in the test package and not the app's R file.



          -- update



          If the problem is that the Android Studio tells you that the R class does not exist in the test package, then that is caused by Android Studio not building the test R class until you try to run the unit tests. Fix this by ignoring the errors on the screen and try to run the test. The R class will be generated and the tests will run (assuming the were no other errors).



          If the problem is that reading the file produces the wrong contents, then you're reading the file from the wrong context. You need to use getContext from Instrumentation.



          public class ResourceLoadTest extends InstrumentationTestCase {

          public void testLoadResource() throws IOException {
          InputStream inputStream = getInstrumentation().getContext().getResources().openRawResource(com.example.app.test.R.raw.hello);
          try {
          Scanner scanner = new Scanner(inputStream, "UTF-8");
          assertEquals("hello", scanner.nextLine());
          } finally {
          inputStream.close();
          }

          }
          }





          share|improve this answer























          • My path was a typo I have it like that. The thing is I Can't seem to access my test package R's class.
            – Tiago Veloso
            Mar 12 '15 at 15:59










          • Your update is worth gold. Was always wondering why it worked on some projects, while it doesn't seem to work on others. Then suddenly, out of nothing it started to work. Never figured out the root cause of this.
            – Rafael T
            May 25 '18 at 14:55














          2












          2








          2






          Your path is wrong. The path you want is src/androidTest/res/raw.



          Files in this directory will replace files in the src/main/res/raw.



          New files in this directory, need to be accessed by the R class in the test package and not the app's R file.



          -- update



          If the problem is that the Android Studio tells you that the R class does not exist in the test package, then that is caused by Android Studio not building the test R class until you try to run the unit tests. Fix this by ignoring the errors on the screen and try to run the test. The R class will be generated and the tests will run (assuming the were no other errors).



          If the problem is that reading the file produces the wrong contents, then you're reading the file from the wrong context. You need to use getContext from Instrumentation.



          public class ResourceLoadTest extends InstrumentationTestCase {

          public void testLoadResource() throws IOException {
          InputStream inputStream = getInstrumentation().getContext().getResources().openRawResource(com.example.app.test.R.raw.hello);
          try {
          Scanner scanner = new Scanner(inputStream, "UTF-8");
          assertEquals("hello", scanner.nextLine());
          } finally {
          inputStream.close();
          }

          }
          }





          share|improve this answer














          Your path is wrong. The path you want is src/androidTest/res/raw.



          Files in this directory will replace files in the src/main/res/raw.



          New files in this directory, need to be accessed by the R class in the test package and not the app's R file.



          -- update



          If the problem is that the Android Studio tells you that the R class does not exist in the test package, then that is caused by Android Studio not building the test R class until you try to run the unit tests. Fix this by ignoring the errors on the screen and try to run the test. The R class will be generated and the tests will run (assuming the were no other errors).



          If the problem is that reading the file produces the wrong contents, then you're reading the file from the wrong context. You need to use getContext from Instrumentation.



          public class ResourceLoadTest extends InstrumentationTestCase {

          public void testLoadResource() throws IOException {
          InputStream inputStream = getInstrumentation().getContext().getResources().openRawResource(com.example.app.test.R.raw.hello);
          try {
          Scanner scanner = new Scanner(inputStream, "UTF-8");
          assertEquals("hello", scanner.nextLine());
          } finally {
          inputStream.close();
          }

          }
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 22:56









          ByteArtisan

          2,44533057




          2,44533057










          answered Mar 11 '15 at 21:46









          Michael Krussel

          2,2331814




          2,2331814












          • My path was a typo I have it like that. The thing is I Can't seem to access my test package R's class.
            – Tiago Veloso
            Mar 12 '15 at 15:59










          • Your update is worth gold. Was always wondering why it worked on some projects, while it doesn't seem to work on others. Then suddenly, out of nothing it started to work. Never figured out the root cause of this.
            – Rafael T
            May 25 '18 at 14:55


















          • My path was a typo I have it like that. The thing is I Can't seem to access my test package R's class.
            – Tiago Veloso
            Mar 12 '15 at 15:59










          • Your update is worth gold. Was always wondering why it worked on some projects, while it doesn't seem to work on others. Then suddenly, out of nothing it started to work. Never figured out the root cause of this.
            – Rafael T
            May 25 '18 at 14:55
















          My path was a typo I have it like that. The thing is I Can't seem to access my test package R's class.
          – Tiago Veloso
          Mar 12 '15 at 15:59




          My path was a typo I have it like that. The thing is I Can't seem to access my test package R's class.
          – Tiago Veloso
          Mar 12 '15 at 15:59












          Your update is worth gold. Was always wondering why it worked on some projects, while it doesn't seem to work on others. Then suddenly, out of nothing it started to work. Never figured out the root cause of this.
          – Rafael T
          May 25 '18 at 14:55




          Your update is worth gold. Was always wondering why it worked on some projects, while it doesn't seem to work on others. Then suddenly, out of nothing it started to work. Never figured out the root cause of this.
          – Rafael T
          May 25 '18 at 14:55


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f28993767%2fandroid-studio-gradle-and-androidtest-res-raw%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin