Gradle: Copy one specific file from main project directory into subproject/build/libs

Viewed 2324

Searching in vain for examples on this humble task, none worked, I need to learn how to use gradle to copy one specific file from the main directory of a gradle project (containing subprojects) and into the /build/libs folder of one of the subprojects. Structure is this:

mainProject folder
  file.txt
  Subproject folder
      build folder
          libs folder
            (I want to copy file.txt here)

This copy process shall be in the build.gradle (for the Subproject) file. I am using Android Studio and the subproject is a pure java application.

Thanks for any help.

1 Answers

You need to implement a custom task of type Copy to copy the file to the target directory. To access the file in the root project, you may use the rootProject property of the subproject.

task copyFileFromRootProject(type: Copy) {
    from "${rootProject.projectDir}/file.txt"
    into 'build/libs'
}
Related