SeeNoWeevil
SeeNoWeevil

Reputation: 2619

Referencing the user's home directory in a Gradle script

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")
    }
}
...

Upvotes: 39

Views: 18730

Answers (5)

dankito
dankito

Reputation: 1108

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")
    }
}

Upvotes: 10

Tony
Tony

Reputation: 4591

In order to setup system-debug-keystore-path, you can use the following code lines.

This works on both MacOS and Windows.

signingConfigs {
        release {
            storeFile "${System.properties['user.home']}${File.separator}.android${File.separator}debug.keystore" as File
            keyAlias 'androiddebugkey'
            storePassword 'android'
            keyPassword 'android'
        }
    }
buildTypes {
        ...
        release {
            signingConfig signingConfigs.release
            ...
        }
    }

Upvotes: 1

Shorn
Shorn

Reputation: 21524

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
  }
}

Upvotes: 29

roomsg
roomsg

Reputation: 1857

more generic (read: "groovy" & not using "ant")

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

Upvotes: 37

Seagull
Seagull

Reputation: 13859

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")
}

Upvotes: 2

Related Questions