Generate a release APK/AAB (for publishing) with Flutter and Visual Studio Code

Tiempo de lectura: 2 minutos

Today we are going to learn how we can generate an APK or AAB to distribute or publish our application to the app store using Flutter and VS Code.

We have to create an application signing file with the following command:

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

Indicate the alias you need to specify. Remember to store it securely, as it will be requested.

In Windows, use this:

keytool -genkey -v -keystore upload-keystore.jks ^
        -storetype JKS -keyalg RSA -keysize 2048 -validity 10000 ^
        -alias upload

Now let’s create a file called key.properties in this path: [project]/android/key.properties

storePassword=<password-from-previous-step>
keyPassword=<password-from-previous-step>
keyAlias=upload
storeFile=<keystore-file-location>

Specify the password you have created, the key alias, and where you save it.

And now we have to modify the app/build.gradle ([project]/android/app/build.gradle)

Add the following before android { …

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

Look for buildTypes and find:

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
       }
   }

To replace it with:

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

Finally, execute the following command:

flutter clean

Then open the project and run this command if you want to build an AAB:

flutter build appbundle

Or this if you want to build an APK:

flutter build apk

Remember to increase the corresponding version number within local.properties

Although this file is overwritten, so let’s create a new file named prod.properties where we will add the following:

flutter.versionName=1.0.0
flutter.versionCode=1

And we have to link it in our app/build.gradle by changing this content:

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'
}

With this:

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('prod.properties')

I’ve only changed local.properties to prod.properties

Leave a Comment