Reputation: 5979
I am using Android Studio & Gradle build script.
Under the root of my Android project, there is a folder named 'other', inside 'other' folder, there is a properties file my.properties.
-MyApp/
…
-other/
-my.properties
-build.gradle
In my build.gradle, I try to access my.properties :
task accessMyProperties {
def folderName = 'other';
//Gradle complains this line
def file = new File("$folderName/my.properties");
...
}
But gradle complains that:
* What went wrong:
A problem occurred evaluating project ':MyApp'.
> other/my.properties (No such file or directory)
Why gradle arises the above error? How to get rid of it?
Upvotes: 12
Views: 15612
Reputation: 80010
Be careful about using pure relative paths in Gradle build scripts; those rely on the working directory at the time the build is invoked, which isn't something you should depend on. It's much better to ground your paths against $projectDir
or $rootDir
. Try something like this:
def file = new File("$projectDir/$folderName/my.properties");
See http://www.gradle.org/docs/current/dsl/org.gradle.api.Project.html for a list of variables you can use.
Upvotes: 16