Android Studio + Gradle and AndroidTest res/raw


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?


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(); } } }


  • Pointer vs Reference difference when passing Eigen objects as arguments
  • LNK1104: cannot open file 'kernel32.lib'
  • Can my PDF ping my server when it is opened?
  • In Java, how can I construct a File from a resource?
  • Activation Function choice for Neural network
  • Pycharm: Marking a folder as 'sources root' is not recursive for subfolders
  • Android Studio Can't Find tools.jar
  • UWP/C# - Issue with AQS and USB Devices
  • JBoss External Properties Files in Classpath
  • Ensure fsync did its job
  • Meteor: Do Something On Email Verification Confirmation
  • Launch Runnable Jar from Web Start
  • Counter field in MS Access, how to generate?
  • output of program is not same as passed argument
  • script to move all files from one location to another location
  • Javascript Callbacks with Object constructor
  • ILMerge & Keep Assembly Name
  • Where to put my custom functions in Wordpress?
  • In LanguageTool, how do you create a dictionary and use it for spell checking?
  • Javascript + PHP Encryption with pidCrypt
  • Symfony2: How to get request parameter
  • Websockets service method fails during R startup
  • Why winpcap requires both .lib and .dll to run?
  • Akka Routing: Reply's send to router ends up as dead letters
  • RestKit - RKRequestDelegate does not exist
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Proper folder structure for lots of source files
  • WPF Applying a trigger on binding failure
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • How to set the response of a form post action to a iframe source?
  • coudnt use logback because of log4j
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • unknown Exception android
  • Is there any way to bind data to data.frame by some index?
  • Checking variable from a different class in C#
  • reshape alternating columns in less time and using less memory
  • How can I use `wmic` in a Windows PE script?