Home » Android » android – Referencing the user's home directory in a Gradle script

android – Referencing the user's home directory in a Gradle script

Posted by: admin April 23, 2020 Leave a comment

Questions:

Is there a cleaner way to reference a file in the user’s home directory than doing the following in a gradle script? (referencing an Android keystore in this example)

homeDir = System.getenv('HOMEDRIVE') + System.getenv('HOMEPATH');
...
signingConfigs {
    release {
        storeFile file(homeDir + "\.android\releaseKeystore.jks")
    }
}
...
How to&Answers:

more generic (read: “groovy” & not using “ant”)

def homePath = System.properties['user.home']

Answer:

Untested code, but how about something like this (might need parentheses around the “X as File” bit):

signingConfigs {
  release {
    storeFile "${System.properties['user.home']}${File.separator}.android${File.separator}releaseKeystore.jks" as File
  }
}

Answer:

You can use ant for access user.home property. Then, you can use Java File API, which is clearer, than path string concatenation.

task hello << {
    def homePath = ant.properties['user.home']
    println homePath
    println new File(homePath, "relative/file/path.txt")
}

Answer:

Gradle knows the property gradleUserHomeDir which references the directory .gradle in user’s home directory (e.g. on Unixes: ~/.gradle):

println project.gradle.gradleUserHomeDir

As it’s a file object you can simply use .parent on it to get user’s home directory:

signingConfigs {
    release {
        storeFile file(project.gradle.gradleUserHomeDir.parent + "/.android/releaseKeystore.jks")
    }
}