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?
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.
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.
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.
gradle file is located inside your project folder under app/build.
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() }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With