Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Gradle, how can I ensure that a file exists at a certain location?

I am starting to use Gradle for an Android application. I would like the build to fail if the developer fails to create a file at a specific location such as ./src/res/values/specialfile.xml

A bit of searching led me to believe that a .doFirst would work

android.doFirst {      assert file("./src/res/values/specialfile.txt").exists()  }  

However, Gradle says "unsupported Gradle DSL method found: 'doFirst()'!"

What can I do to assert a file's existence?

like image 407
Mendhak Avatar asked Mar 30 '14 22:03

Mendhak


People also ask

What is Ziptree in gradle?

A FileTree represents a hierarchy of files. It extends FileCollection to add hierarchy query and manipulation methods. You typically use a FileTree to represent files to copy or the contents of an archive. You can obtain a FileTree instance using Project.

What is the location of gradle?

gradle file, located in the root project directory, defines dependencies that apply to all modules in your project. By default, the top-level build file uses the plugins block to define the Gradle dependencies that are common to all modules in the project.

What is Gradle build directory?

The build directory of this project into which Gradle generates all build artifacts. 4. Contains the JAR file and configuration of the Gradle Wrapper. 5. Project-specific Gradle configuration properties.

Where do Gradle build files go?

gradle file is located inside your project folder under app/build.


1 Answers

doFirst only exists on tasks object. android is not a task.

If would want this test to always be done even if the developer doesn't try to build (for example when running the tasks task), you should simply put in your build.gradle

assert file("./src/res/values/specialfile.txt").exists()  

However this is really not recommended as this would be executed even for non build tasks, or even when the model is built for IDE integration.

There is a task called preBuild that is executed before anything in the android build, so you can hook your test to it, either through another task or through doFirst:

preBuild.doFirst {      assert file("./src/res/values/specialfile.txt").exists()  }  
like image 112
Xavier Ducrohet Avatar answered Sep 20 '22 16:09

Xavier Ducrohet