Sunday, April 26, 2020

Flutter - How to generate Android apk.

As we know, Flutter SDK can develop mobile apps for Android and iOS. So, if you have a developed Flutter application and you want to publish or distribute the Android app. You have to create a signed Android APK(Android Package Kit) file format that is used by the Android operating system. 

In this post, we going to explain how we can create a signed Flutter Android apk and publish it on google play store.

Create signed Android APK

1. Once you have developed a Flutter application with all the features that you want in your application. Now, open the build.gradle file that is located in <app dir>/android/app of your Flutter project:
  • applicationId: Verify your application package ID.
  • versionCode & versionName: Verify application version code and the version number. You can update it from the root level pubspec.yaml file.
  • minSdkVersion & targetSdkVersion: After that check the minimum API level and the target API level on which the app is designed to run on Android devices.
2. You should change the application launcher icon that 'll be visible on mobile. You have to update the app launcher in res and AndroidManifest.xml.
  • Place app launcher icons in the <app dir>/android/app/src/main/res/mipmap directories. 
  • After that update same icon name in the AndroidManifest.xml file tag’s android:icon attribute like: android:icon="@mipmap/ic_developerlibs"
3. To publish an Android application on the Play store, you must have a digital signature key. You can create it two ways:

1. Keystore Command
  • Mac command:
    keytool -genkey -v -keystore ~/key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias key

  • Windows command:
    keytool -genkey -v -keystore c:/Users/USER_NAME/key.jks -storetype JKS -keyalg RSA -keysize 2048 -validity 10000 -alias key

    here, key.jks is the key that we need to create a signed APK.
2. Android Studio: You can create a key from the Android Studio build/Generate Signed Apk/Next/Create new. This option is not available with the Flutter project. So, you have to create a native Android project and open it in Android Studio. After that you will find the option to create a new key:
  1. In the next window, locate the path of the key in your system where you want to save the key. 
  2. After that give a name with .jks extension.
  3. Fill the other details and click ok.
in the above window, the Alias and passwords are important. So, keep it safe and secure because if you lost it then you won’t be able to send future updates to your Application. 

4. Now, we need to create a file in the <app dir>/android/  directory to store the detail of keystore:

5. After that update the <app dir>/android/app/build.gradle file and replace the name of your key.

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

6. Now replace the below lines:

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
with the following lines:

signingConfigs {
    release {
        keyAlias keystoreProperties['keyAlias']
        keyPassword keystoreProperties['keyPassword']
        storeFile file(keystoreProperties['storeFile'])
        storePassword keystoreProperties['storePassword']
buildTypes {
    release {
        signingConfig signingConfigs.release
 minifyEnabled true
        useProguard true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), ''
in the above file, we have enabled the minifiyEnabled and useProguard flags to true and also point ProGuard to the file. It'll apply proguard and used all the details in the signing process.

7. By default, Flutter SDK does not obfuscate or minify the Android host. If you may want to reduce the size of the Android APK or protect the source code from reverse engineering. Create /android/app/ file and write the following rules:

#Flutter Wrapper
-keep class** { *; }
-keep class io.flutter.plugin.**  { *; }
-keep class io.flutter.util.**  { *; }
-keep class io.flutter.view.**  { *; }
-keep class io.flutter.**  { *; }
-keep class io.flutter.plugins.**  { *; }

8. Now, you are ready to generate sign APK  of your Flutter Android application.

  • Using the command line: You can change the directory where you want to store signed APK before hitting the below command:
    flutter build apk
  • From the Android Studiobuild/Flutter/build APK
By default, the signed Android APK for your app is created at <app dir>/build/app/outputs/apk/release/app-release.apk.


Get it on Google Play

React Native - Start Development with Typescript

React Native is a popular framework for building mobile apps for both Android and iOS. It allows developers to write JavaScript code that ca...