Step-by-step to publish your flutter project as Android App Bundle
Tags: tutorial, flutter, android
Published: 2022-04-28 16:28:46

The Flutter's [official instruction](https://docs.flutter.dev/deployment/android#signing-the-app) is suitable for a start. But I want to note my step-by-step here. So you might not need to guess about the file's path.

## 1. Create your Keystore

Keystore is the file you should have to sign your AAB file. 

Run the following command in Terminal at your project's root directory.

`keytool -genkey -v -keystore upload-keystore.jks -keyalg RSA -keysize 2048 -validity 10000 -alias upload`

After entering this command, you will be asked to fill key's password and some information.

Please pay attention to the following options:

- **-keystore** is the file's name and where it will be created. **upload-keystore.jks** will be made at your project's root directory. The original instruction: **~/upload-keystore.jks** will put your Keystore file in the user's root directory. 
- **-alias** will be your key's alias name. You will need to use it later.



## 2. Create a reference file to Keystore

Create a file named **key.properties** at **[project root]/android/**

```
storePassword=<password from previous step>
keyPassword=<password from previous step>
keyAlias=upload
storeFile=../../upload-keystore.jks
```

- if you create with different key alias, please specify it in **keyAlias**.
- For **storeFile** if you choose to put Keystore file at the root of your Flutter's project directory. This file's path would work.

## 3. Modify app/build.gradle

Now, we have to modify build.gradle. So Flutter can use it to sign and generate an AAB file.

### Load keystore's properties

Open **[project]/android/app/build.gradle**, look for **android** block, and put following code at the top of its.

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

   android {
         ...
   }

```

### Replace buildTypes block

find **buildTypes** block. it should look like the below:

```
   buildTypes {
       release {
           // TODO: Add your own signing config for the release build.
           // Signing with the debug keys for now,
           // so `flutter run --release` works.
           signingConfig signingConfigs.debug
       }
   }
```

you have to **replace** it with following code:

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

## 4. Let's sign and build the release version

After you modified **build.gradle** file, you should run:

```bash
flutter clean
```

So you can try to build the AAB file:

```
flutter build appbundle
```
If the build has been successful, you will see the AAB's file path on the Terminal's console.