Setting a custom assets directory for unit testing in Android Studio

15,310

Solution 1

I've stumbled upon an answer for my own question, for which I scored the coveted "Tumbleweed" award reserved for questions that inspire a particularly notable lack of interest. Never-the-less it may prove useful to someone.

In the end I'm going to describe this as a bug in Android's gradle build but in the absence of any conclusive documentation that's going to have to remain an opinion.

It would appear that "androidTest/assets" just doesn't work. However "debug/assets" does. So does "debug/java" incidentally so the solution is to put any and all testing resources and code into the debug root, with the exception of the unit tests themselves.

I also added the following modifications to my build.gradle:

    androidTest {
        assets.srcDirs = ['src/main/assets', 'src/androidTest/assets/', 'src/debug/assets/']
        java.srcDirs = ['src/main/java', 'src/androidTest/java', 'src/debug/java']
    }

And now I have functioning unit tests employing assets and mocks that are unknown to the release build.

Solution 2

I don't know how it was before, but I was able to create Android instrumented unit tests where I can read test assets or production assets. See this answer for more info.

Solution 3

I am using Android Studio 1.3 + Gradle 1.2.3 and src/androidTest/assets didn't work for me. I placed everything in src/debug without any changes to build.gradle and it worked. Android seemingly picks up up items from the debug folder automatically while building the apk.

Solution 4

I found this right for me

android{
    sourceSets {
        androidTest {
            assets.srcDirs = ['src/main/assets', 'src/androidTest/assets/', 'src/debug/assets/']
            java.srcDirs = ['src/main/java', 'src/androidTest/java', 'src/debug/java']
        }
    }
}
Share:
15,310
Phillip Fitzsimmons
Author by

Phillip Fitzsimmons

Updated on June 22, 2022

Comments

  • Phillip Fitzsimmons
    Phillip Fitzsimmons almost 2 years

    We have the not uncommon requirement of executing unit tests of our Android application which make use of assets which are only required for unit testing.

    With the Eclipse-based SDK this was simple and obvious - the test project was a separate project with its own assets folder. With Android Studio this is meant to be something that one configures in build.gradle (or by convention).

    Our unit tests are executing, finally, but I've exhausted every suggestion I've found find regarding a custom (and, ideally, merged) assets folder. Here's what I've tried:

    • Adding a test closure to sourceSets within the android closure (by closure I mean that which is between the curly braces in build.gradle).

      androidTest {
          assets.srcDirs = ['src/androidTest/assets/']
      }
      
    • Same thing, but with "test" as the name of the sourceSet (as opposed to "androidTest", above) and also with "instrumentTest".

    • Various combinations of above, plus androidTest.setRoot("test"), androidTest.setRoot("androidTest"), both of which cause our unit tests to not be recognized ("empty suite").

    This question could be more generically stated as "how does one employ different directories for unit tests in Android Studio".

    We've been over the documentation from Android and either we're not understanding it, it's wrong, or there's a bug somewhere.

    Any help would be very much appreciated.

  • weidongxu
    weidongxu over 9 years
    Seems Google fixed it. I am using Android Studio 0.8.6 + gradle 1.12, and "androidTest/assets" folder works without any additional script.
  • Nivaldo Bondança
    Nivaldo Bondança over 7 years
    The link you sent, was EXTREMLY helpful! Thanks a lot. This should be the accepted answer.
  • MahNas92
    MahNas92 over 4 years
    @weidongxu, I am using 1.3.21 (Kotlin), and calling getAssets().open() on a properties file won't work if it is under androidTest/assets. Is it necessary to put the above lines in build.gradle maybe?
  • Hylke
    Hylke almost 4 years
    For those that wonder where to place these lines of gradle code; it is: android -> sourceSets -> androidTest -> ...