How to generate a keystore file in Flutter

How to generate a keystore file in Flutter

While implementing the Google sign-in or getting your app ready for publishing. Adding a key store involves running a command on the terminal.

But for that command, there must be Java installed on the Pc, This is because the JAVA contains this key tool feature in it. if we look at the Java\jdk-22\bin directory (In my case I am using jdk-22) this keytool application file exists.

Add the Path to the environment variable of the bin to access the Java program along with this keytool feature. Adding it to environment variable is very easy just search for Edit the system variable and open it.

Click the environment variable from the bottom right corner of this window. Click on New from system variable, add the variable name as JAVA and variable value as the path of your bin folder of java (in my case: C:\Program Files\Java\jdk-22\bin) Save that by clicking ok twice.

Now open the terminal and check of java version just to see if we have set the Java correctly or not by running the Java -version command.

Generate Keystore file in Flutter

Open your terminal and run the command below.

Change the location where you want to save your key here in the example below key will be saved at the root of my F: Disk.

keytool -genkeypair -alias myalias -keyalg RSA -keysize 2048 -validity 365 -keystore F:\key.jks

Hit enter it will ask for a password add the password and make sure to remember that password or write it down somewhere.

It will ask you to enter the password twice and add the same password for confirmation. Next add some details like name, Organizational unit, city, etc. Provide all the info and at the end type YES and hit enter. This will create the Key at your given location.

In this case, I saved this key at the root of F: disk. copy the key and paste it inside the android/app folder of your project.

Now in the App level gradle file add the lines below for keystore properties.

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

Also, Add the signingConfigs code below above the buildTypes Code block, this lets the app sign with the provided key.

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

Add the new file for the key properties inside the Android directory. Right-click on Android folder>New>File name the file key.properties

Add the details password, key location, and keyAlias.

storePassword = com@example@mytest
keyPassword = com@example@mytest
keyAlias = key
storeFile = F:\FlutterWork\mytest\android\app\key.jks

For location right-click on the key inside the app folder and select Copy Path/Reference

Click on the copy absolute path, this provides you with the path of the key which can be added to the sroteFile property inside the key.properties file.

So this is how to generate keystore file in flutter.


For more Flutter tutorials, Tips, Tricks, Free code, Questions, and Error Solving.

Remember FlutterDecode.com

Leave a Comment