Flutter(Andorid) Keystore path on different OS

Ayush P Gupta
CodeChai
Published in
2 min readOct 23, 2018

Since the start of our project, we were having a constant pain in debugging/releasing android apk on different OS like Windows, Linux. The pain was nothing but to change the gradle file everytime after we pull/push project.

For instance, i am using LINUX, while my other teammate uses WINDOWS. Flutter docs about releasing an android apk asks in one step to Reference the keystore from the app i.e. create a file named ‘key.properties’ whose contents in our case looks like this:

storePassword=**123
keyPassword=**3
keyAlias=key
storeFile=C:\\Users\\Varun\\Desktop\\***.jks

Now because my teammate uses Windows, hence the path of keystore is different, even though it is in same directory inside our project. Therefore, I wasn’t able to run the app on my Linux because gradle couldnt find this win path on linux.

So how to manage different paths based on the build OS?

I tried to find some code with which we can check the OS that developer is using in gradle script.
I then came to this question onstackoverflow that exactly satisfies my requirement:

Great, the fun begins now.

Step 1:

Define three new paths for keystore for different OS in key.properties :

storePassword=**123
keyPassword=**3
keyAlias=key
storeFileWindows=C:\\Users\\Varun\\Desktop\\***.jks
storeFileLinux=/home/master/StudioProjects/***/***.jks
storeFileMac=/home/abc/.../***.jks

Step 2:

Edit build.gradle file, where you replace the following code:

signingConfigs {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['storePassword']
}
}

with this code:

signingConfigs {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storePassword keystoreProperties['storePassword']

if (Os.isFamily(Os.FAMILY_WINDOWS)) {
storeFile file(keystoreProperties['storeFileWindows'])
} else if (Os.isFamily(Os.FAMILY_UNIX)) {
storeFile file(keystoreProperties['storeFileLinux'])
} else if (Os.isFamily(Os.FAMILY_MAC)) {
storeFile file(keystoreProperties['storeFileMac'])
}
}
}

So you can see above, that i added condition for OS check and set keystore file path accordingly. The release process automatically picks the correct file path then.
The issue and pain finally resolves… 😁

Whola! Both you and I learnt something new today. Congrats
Clap! Clap! Clap!

Further reading:

  1. https://flutter.io/android-release/
  2. https://stackoverflow.com/questions/11235614/how-to-detect-the-current-os-from-gradle
  3. https://www.oreilly.com/library/view/gradle-recipes-for/9781491947272/ch04.html

The Flutter Pub is a medium publication to bring you the latest and amazing resources such as articles, videos, codes, podcasts etc. about this great technology to teach you how to build beautiful apps with it. You can find us on Facebook, Twitter, and Medium or learn more about us here. We’d love to connect! And if you are a writer interested in writing for us, then you can do so through these guidelines.

--

--

Ayush P Gupta
CodeChai

NodeJs | VueJs | Kubernetes | Flutter | Linux | DIY person