
// Source - https://github.com/nenick/AndroidStudioAndRobolectric/blob/master/app/build.workaround-missing-resource.gradle
// Workaround for missing test resources when running unit tests within android studio.
// This copies the test resources next to the test classes for each variant.
// Tracked at https://github.com/nenick/AndroidStudioAndRobolectric/issues/7
// Original solution comes from https://code.google.com/p/android/issues/detail?id=136013#c10
// See also https://code.google.com/p/android/issues/detail?id=64887
gradle.projectsEvaluated {
  // Base path which is recognized by android studio.
  def testClassesPath = "${buildDir}/intermediates/classes/test/"
  // Copy must be done for each variant.
  def variants = android.libraryVariants.collect()

  variants.each { variant ->
    def variationName = variant.name.capitalize()
    def variationPath = variant.buildType.name

    // Specific copy task for each variant
    def copyTestResourcesTask = project.tasks.create("copyTest${variationName}Resources", Copy)
    copyTestResourcesTask.from("${projectDir}/src/test/resources")
    copyTestResourcesTask.into("${testClassesPath}/${variationPath}")
    copyTestResourcesTask.execute()
  }
}
