Flutter - How to sign your android app


I recently published my first Flutter app, and one of the hard parts as a newbie was signing and  creating the app bundle from Android studio. There is a good link here walking you through it but it didn't help with the details if you were having any issues. Here is my walkthrough on how to do create your app bundle and what to do if you had issues like me. This walkthrough is applicable to Windows.

Once you're happy with your app and created a nice app icon, you're ready to sign it. 

Copy and paste this command into the terminal, replacing USER_NAME with your particular username. This will create a key file. You do not need to store the key in that particular folder but just for ease of use and for finding it later.
keytool -genkey -v -keystore c:\Users\USER_NAME\key.jks -storetype JKS -keyalg RSA -keysize 2048 -validity 10000 -alias key  
Now for me, when I input that command, it did not recognise keytool as a command. The way I fixed it was to download the new Java JDK from Oracle found here and install it. 

Then in the Windows start menu, search for 'environment variables' and click edit environment variables, then click the button below.


In there you have to specify the PATH and JAVA_HOME. Create new variable called JAVA_HOME and point it to your Java JDK folder.



Next, edit the Path variable and insert a pathway to your Java JDK's bin folder, like below:


Press OK and save the new settings. I would restart your PC at this point.

Upon restart, try input the keytool command into your terminal in Android studio. This should work and then you'll be asked to create a new password and give some details about yourself. Once you're done with that, a new file called key.jks will be made in the folder you specified. 

New in Android studio, in the Android folder of your project, create a new file called key.properties. Then put these details in your new file:

storePassword=<password from previous step>
keyPassword=<password from previous step>
keyAlias=key (or any other name you assigned)
storeFile=<location of the key store file, such as /Users/<user name>/key.jks>


Then in your <project app dir>/android/app/build.gradle file, copy and paste the following code above the Android block of code:

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

   android {
         ...
   }
Next in the same build.gradle file, copy and paste this bit of code above the 'buildTypes' bit of code, also within the buildTypes code, change 'signingConfigs.debug' to 'signingConfigs.release'.

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

In the AndroidManifest.xml, found in <project app dir>/android/app/src/main folder, make sure the android:label property reflected the final name of the app you would like. Also that the app requests internet permission if it needs it. Put this code before the application section.

<uses-permission android:name="android.permission.INTERNET" />
Finally, check the default build.gradle that you edited before file found in <project app dir>/android/app/ and check the defaultConfig settings. 

The applicationId will be used as a unique identifier whenever you update the app. If this is changed then if you try to update an app on the Google Play store then it will create a new app rather than update the existing one. It is a good idea to note it down somewhere. The version code is found in the pubspec.yaml file, and is customary to make the first release 1.0.0. Google play store will want a minimum of targetSDKVersion of 29, so change it to 29 if it not there already.

defaultConfig {

applicationId "com.gptechlead.urgent_referral_guidelines_ap"
minSdkVersion 16
targetSdkVersion 29
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
You're ready to create your app bundle to upload to your google play console. In the terminal type:

flutter build appbundle

And you're done! Upload this to your google play console after doing filling out all your app details and after the review process, your app should be downloadable by everyone!

If you found this useful, please share.

Comments

Popular posts from this blog

EMIS Quick codes

How to set up Office 365 for NHS workers

How to set up EMIS Web on your own PC