gradle set absolute path value for a keystore file

Viewed 7821

I'd like to save my keystore outside the project directory. I don't want to store filepaths inside the repository so I delegated the values to appropriate gradle variables in ~/.gradle/gradle.properties I can't get gradle to accept an absolute path like: /Users/username/.gradle/keystores/project/release.key or ~/.gradle/keystores/project/release.key

I tried: storeFile file(RELEASE_STORE_FILE) and storeFile new File(RELEASE_STORE_FILE)

none of them seems to work, however.

How can I pass an absolute path value to the keystore file through RELEASE_STORE_FILE variable?

android {
    signingConfigs {
        release {
            storeFile file(RELEASE_STORE_FILE)
            storePassword RELEASE_STORE_PASS
            keyAlias RELEASE_ALIAS
            keyPassword RELEASE_KEY_PASS
        }
    }
}

and the ~/.gradle/gradle.properties file:

RELEASE_STORE_FILE=/Users/username/.gradle/keystores/project/release.key
RELEASE_STORE_PASS=******
RELEASE_ALIAS=******
RELEASE_KEY_PASS=******

In short: I want to pass an absolute path value to gradle.

4 Answers

Found solution there: https://gist.github.com/gabrielemariotti/6856974

Briefly, you should parse file that contains path to keystore properly. Modify yours module's gradle with next lines. First, this is how to create signingConfigs based on keystore.properties file content:

signingConfigs
    {
        release
        {
            def Properties props = new Properties()
            def propFile = new File('path/to/your/keystore.properties') //absolute path to keystore.properties
            if (propFile.canRead())
            {
                props.load(new FileInputStream(propFile))

            if (props != null && props.containsKey('STORE_FILE') && props.containsKey('STORE_PASSWORD') &&
                    props.containsKey('KEY_ALIAS') && props.containsKey('KEY_PASSWORD'))
            {
                android.signingConfigs.release.storeFile = file(props['STORE_FILE']) 
                android.signingConfigs.release.storePassword = props['STORE_PASSWORD']
                android.signingConfigs.release.keyAlias = props['KEY_ALIAS']
                android.signingConfigs.release.keyPassword = props['KEY_PASSWORD']
            }
            else
            {
                println 'keystore.properties found but some entries are missing'
                android.buildTypes.release.signingConfig = null
            }
        }
        else
        {
            println 'keystore.properties not found'
            android.buildTypes.release.signingConfig = null
        }
    }
}

And then add signingConfig to yours release buildType:

buildTypes 
{
    ...
    release 
    {
        ...
        signingConfig signingConfigs.release            
    }
}

Example of keystore.properties file for this solution:

STORE_FILE=absolute//path//to//store
STORE_PASSWORD=yourPass
KEY_PASSWORD=keysPass
KEY_ALIAS=aliasName

This has worked for me (Android Studio 3.0.1, Gradle 4.1, Windows 10).

In my case (macOS) with my local_store.keystore saved in Users/<username> and the path to the keystore configured in the gradle.properties file as:

RELEASE_STORE_FILE=Users/<username>/local_store.keystore

I was getting the following error:

> Keystore file 
'/Users/<username>/Documents/MyProject/android/app/Users/<username>/local_store.keystore' 
not found for signing config 'release'.

so the path set in the gradle.properties was added to the relative path of the build.gradle file (notice duplicated /Users/<username>).

I fixed that by specifying the path to the keystore with a few ../ to point to the right location in the Users/<username> folder.

This is how it should look in the gradle.properties file:

RELEASE_STORE_FILE=../../../../../local_store.keystore

No modifications are required to the build.gradle file.

Related