Release APK In Flutter

Introduction

 
This article is an explanation of how to generate a release apk for Android in Flutter. We are going to generate a certificate to sign an unsigned apk as well and all other necessary steps to generate a release apk.
 
Release APK In Flutter

Steps

  • If you are a beginner in Flutter then you can check my blog Create a first app in Flutter. I have created app named as “flutter_release_apk”
  • You can specify some app related setting in build.gradle under app folder (android/app/build.gradle). I have mentioned important settings below:

    • applicationId
      This must be unique all around all apps in Google Play so wisely choose the name. Your app will be identified by this name and this can’t be changed.

    • versionCode
      This is the app version code and needs to be updated each time you update the app on Google Play. Updating  with the same version of code will not be accepted in Google Play Store.

      For example purposes: 
      applicationId com.example.flutter_release_apk
      versionCode 1

  • Now, we need to generate signing certificate to sign android apk. Following is the command to generate a signing certificate. You need to execute this command in the terminal and this will generate flutter_release_apk.jks file in the same folder where terminal opened.

    keytool -genkey -v -keystore flutter_release_apk.jks -alias alias_name -keyalg RSA -keysize 2048 -validity 10000

    Enter keystore password: abcd1234
    Re-enter new password: abcd1234

    What is your first and last name?
    [Unknown]: demo demo

    What is the name of your organizational unit?
    [Unknown]: demo

    What is the name of your organization?
    [Unknown]: demo

    What is the name of your City or Locality?
    [Unknown]: demo

    What is the name of your State or Province?
    [Unknown]: demo

    What is the two-letter country code for this unit?
    [Unknown]: 91

    Is CN=demo demo, OU=demo, O=demo, L=demo, ST=demo, C=91 correct?
    [no]: yes

    Generating 2,048 bit RSA key pair and self-signed certificate (SHA256withRSA) with a validity of 10,000 days
    for: CN=demo demo, OU=demo, O=demo, L=demo, ST=demo, C=91

    Enter key password for <alias_name>
    (RETURN if same as keystore password): abcd1234

    Re-enter new password: abcd1234
    [Storing flutter_release_apk.jks]

    So, now I have my flutter_release_apk.jksin directory where my terminal is open.

    NOTE
    Copy and Save this file safely because it is required every time you need to update your app. If you lose this file then there is no way to update your app in future

  • Now, we need to create one file under android folder named as “key.properties”. This file stores the credentials for signing the release apk. Put the following code in this file and update as per your jsk file

    storePassword=abcd1234
    keyPassword=abcd1234
    keyAlias=alias_name
    storeFile=/Applications/AMPPS/www/Flutter_Demos/flutter_release_apk/flutter_release_apk.jks


    Save the file.

  • Now, go back to build.gradle file under app directory (android/app/build.gradle). And now we need to define some settings for release apk generation. I have made bold some text that needs to be added or updated.

    1. def localProperties = new Properties()  
    2. def localPropertiesFile = rootProject.file('local.properties')  
    3. if (localPropertiesFile.exists()) {  
    4.     localPropertiesFile.withReader('UTF-8') {  
    5.         reader - > localProperties.load(reader)  
    6.     }  
    7. }  
    8. def flutterRoot = localProperties.getProperty('flutter.sdk')  
    9. if (flutterRoot == null) {  
    10.     throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")  
    11. }  
    12. def flutterVersionCode = localProperties.getProperty('flutter.versionCode')  
    13. if (flutterVersionCode == null) {  
    14.     flutterVersionCode = '1'  
    15. }  
    16. def flutterVersionName = localProperties.getProperty('flutter.versionName')  
    17. if (flutterVersionName == null) {  
    18.     flutterVersionName = '1.0'  
    19. }  
    20. apply plugin: 'com.android.application'  
    21. apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"  
    22. def keystoreProperiesFile = rootProject.file("key.properties")  
    23. def keystoreProperies = new Properties()  
    24. keystoreProperies.load(new FileInputStream(keystoreProperiesFile))  
    25. android {  
    26.     compileSdkVersion 28  
    27.     lintOptions {  
    28.         disable 'InvalidPackage'  
    29.     }  
    30.     defaultConfig {  
    31.         applicationId "com.example.flutter_release_apk"  
    32.         minSdkVersion 16  
    33.         targetSdkVersion 28  
    34.         versionCode flutterVersionCode.toInteger()  
    35.         versionName flutterVersionName  
    36.         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"  
    37.     }  
    38.     signingConfigs {  
    39.         release {  
    40.             keyAlias keystoreProperies["keyAlias"]  
    41.             keyPassword keystoreProperies["keyPassword"]  
    42.             storeFile file(keystoreProperies["storeFile"])  
    43.             storePassword keystoreProperies["storePassword"]  
    44.         }  
    45.     }  
    46.     buildTypes {  
    47.         release {  
    48.             // signingConfig signingConfigs.debug  
    49.             signingConfig signingConfigs.release  
    50.         }  
    51.     }  
    52. }  
    53. flutter {  
    54.     source '../..'  
    55. }  
    56. dependencies {  
    57.     testImplementation 'junit:junit:4.12'  
    58.     androidTestImplementation 'com.android.support.test:runner:1.0.2'  
    59.     androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'  
    60. }  

     

    Save the file.

  • Now, Run the command flutter build apk --release in terminal. Following is my terminal log and you can see that my release apk is generated

    MyVss-Mac-mini:flutter_release_apk myvs $ flutter build apk --release
    Initializing gradle... 41.4s
    Resolving dependencies... 46.5s
    Calling mockable JAR artifact transform to create file: /Users/myvs/.gradle/caches/transforms-1/files-1.1/android.jar/fe6e28b8b8fb0fe581763b573c73df6a/android.jar with input /Users/myvs/Library/Android/sdk/platforms/android-28/android.jar
    Running Gradle task 'assembleRelease'...
    Running Gradle task 'assembleRelease'... Done 117.6s
    Built build/app/outputs/apk/release/app-release.apk (4.9MB).

  • Now this generated apk can be uploaded on Google Play Store as release APK.

  • Congratulations you can now upload as many apps as you need to upload on Google Play Store.

  • Best of luck, I hope you will make awesome apps in the future :)))

Conclusion

 
In this article we have learned how we can generate release apk. This way of generating release apk in flutter is very secure as we have defined separate files to store our keystores credential and this is the recommended way.


Similar Articles