How to build apk flutter
How to build apk flutter
Step-by-Step Guide for Flutter Android APK Signing and Release
Table of Contents
To publish your app on Play Store, you need to give your app a digital signature.
Common errors while signing the app
step 2 : Reference the keystore from the app
Write the following lines inside the newly created file
step 3 : Configure signing in gradle
Navigate to /android/app/build.gradle file.
1. Replace the following
2. Replace the following
with the signing config info
Now, every release build of your app will be signed automatically
Running flutter build defaults to a release build
Note : release bundle for your app is created at /build/app/outputs/bundle/release/app.aab
Note : the above command “flutter build apk” builds a fat apk
OR
Note : the above command generated two apk files
Bonus Tip : Do not forget to :
Review App Manifest File, AndroidManifest.xml, located in /android/app/src/main
Review the build configuration, Gradle build file, build.gradle, located in /android/app
Navigate to defaultConfig code block and verify
Useful Resources
Share This Post
Join our newsletter!
Enter your email to receive our latest newsletter.
Build and release an Android app
During a typical development cycle, you test an app using flutter run at the command line, or by using the Run and Debug options in your IDE. By default, Flutter builds a debug version of your app.
When you’re ready to prepare a release version of your app, for example to publish to the Google Play Store, this page can help. Before publishing, you might want to put some finishing touches on your app. This page covers the following topics:
Note: Throughout this page, [project] refers to the directory that your application is in. While following these instructions, substitute [project] with your app’s directory.
Adding a launcher icon
When a new Flutter app is created, it has a default launcher icon. To customize this icon, you might want to check out the flutter_launcher_icons package.
Alternatively, you can do it manually using the following steps:
Review the Material Design product icons guidelines for icon design.
In the [project]/android/app/src/main/res/ directory, place your icon files in folders named using configuration qualifiers. The default mipmap- folders demonstrate the correct naming convention.
To verify that the icon has been replaced, run your app and inspect the app icon in the Launcher.
Enabling Material Components
If your app uses Platform Views, you may want to enable Material Components by following the steps described in the Getting Started guide for Android.
To find out the latest version, visit Google Maven.
Signing the app
To publish on the Play Store, you need to give your app a digital signature. Use the following instructions to sign your app.
Create an upload keystore
If you have an existing keystore, skip to the next step. If not, create one by either:
Running the following at the command line:
On Mac/Linux, use the following command:
On Windows, use the following command:
Note:
Reference the keystore from the app
Create a file named [project]/android/key.properties that contains a reference to your keystore:
Warning: Keep the key.properties file private; don’t check it into public source control.
Configure signing in gradle
Configure gradle to use your upload key when building your app in release mode by editing the [project]/android/app/build.gradle file.
Add the keystore information from your properties file before the android block:
Load the key.properties file into the keystoreProperties object.
Find the buildTypes block:
And replace it with the following signing configuration info:
Release builds of your app will now be signed automatically.
Note: You may need to run flutter clean after changing the gradle file. This prevents cached builds from affecting the signing process.
For more information on signing your app, see Sign your app on developer.android.com.
Shrinking your code with R8
Note: Obfuscation and minification can considerably extend compile time of the Android application.
Enabling multidex support
When writing large apps or making use of large plugins, you may encounter Android’s dex limit of 64k methods when targeting a minimum API of 20 or below. This may also be encountered when running debug versions of your app via flutter run that does not have shrinking enabled.
Flutter tool supports easily enabling multidex. The simplest way is to opt into multidex support when prompted. The tool detects multidex build errors and will ask before making changes to your Android project. Opting in allows Flutter to automatically depend on androidx.multidex:multidex and use a generated FlutterMultiDexApplication as the project’s application.
Note: Multidex support is natively included when targeting Android SDK 21 or later. However, it isn’t recommended to target API 21+ purely to resolve the multidex issue as this might inadvertently exclude users running older devices.
You might also choose to manually support multidex by following Android’s guides and modifying your project’s Android directory configuration. A multidex keep file must be specified to include:
Also, include any other classes used in app startup. See the official Android documentation for more detailed guidance on adding multidex support manually.
Reviewing the app manifest
application Edit the android:label in the application tag to reflect the final name of the app. uses-permission Add the android.permission.INTERNET permission if your application code needs Internet access. The standard template does not include this tag but allows Internet access during development to enable communication between Flutter tools and a running app.
Reviewing the Gradle build configuration
Review the default Gradle build file ( build.gradle ) located in [project]/android/app to verify the values are correct:
Under the defaultConfig block
Under the android block
For more information, see the module-level build section in the Gradle build file.
Building the app for release
You have two possible release formats when publishing to the Play Store.
Note: The Google Play Store prefers the app bundle format. For more information, see Android App Bundle and About Android App Bundles.
Warning: Recently, the Flutter team has received several reports from developers indicating they are experiencing app crashes on certain devices on Android 6.0. If you are targeting Android 6.0, use the following steps:
If you build an APK Make sure android/app/src/AndroidManifest.xml doesn’t set android:extractNativeLibs=false in the tag.
For more information, see the public issue.
Build an app bundle
This section describes how to build a release app bundle. If you completed the signing steps, the app bundle will be signed. At this point, you might consider obfuscating your Dart code to make it more difficult to reverse engineer. Obfuscating your code involves adding a couple flags to your build command, and maintaining additional files to de-obfuscate stack traces.
From the command line:
By default, the app bundle contains your Dart code and the Flutter runtime compiled for armeabi-v7a (ARM 32-bit), arm64-v8a (ARM 64-bit), and x86-64 (x86 64-bit).
Test the app bundle
An app bundle can be tested in multiple ways—this section describes two.
Offline using the bundle tool
Online using Google Play
Build an APK
Although app bundles are preferred over APKs, there are stores that don’t yet support app bundles. In this case, build a release APK for each target ABI (Application Binary Interface).
If you completed the signing steps, the APK will be signed. At this point, you might consider obfuscating your Dart code to make it more difficult to reverse engineer. Obfuscating your code involves adding a couple flags to your build command.
From the command line:
This command results in three APK files:
Install an APK on a device
Follow these steps to install the APK on a connected Android device.
From the command line:
Publishing to the Google Play Store
For detailed instructions on publishing your app to the Google Play Store, see the Google Play launch documentation.
Updating the app’s version number
After updating the version number in the pubspec file, run flutter pub get from the top of the project, or use the Pub get button in your IDE. This updates the versionName and versionCode in the local.properties file, which are later updated in the build.gradle file when you rebuild the Flutter app.
Android release FAQ
Here are some commonly asked questions about deployment for Android apps.
When should I build app bundles versus APKs?
The Google Play Store recommends that you deploy app bundles over APKs because they allow a more efficient delivery of the application to your users. However, if you’re distributing your application by means other than the Play Store, an APK may be your only option.
What is a fat APK?
What are the supported target architectures?
When building your application in release mode, Flutter apps can be compiled for armeabi-v7a (ARM 32-bit), arm64-v8a (ARM 64-bit), and x86-64 (x86 64-bit). Flutter does not currently support building for x86 Android (See Issue 9253).
How do I build a release from within Android Studio?
In Android Studio, open the existing android/ folder under your app’s folder. Then, select build.gradle (Module: app) in the project panel:
Next, select the build variant. Click Build > Select Build Variant in the main menu. Select any of the variants in the Build Variants panel (debug is the default):
The resulting app bundle or APK files are located in build/app/outputs within your app’s folder.
Except as otherwise noted, this work is licensed under a Creative Commons Attribution 4.0 International License, and code samples are licensed under the BSD License.
This tutorial shows you how to build APK files for Android for applications developed using Flutter.
APK (Application Package File) is a format used by Android operating systems for distribution and installation. After you build an application, it’s quite common to build APK files to be tested across different devices. If the application is developed using Flutter, you can easily build APK files by using flutter build apk command. In this tutorial, I am going to show you how to build APK files for Android using Flutter. This includes how to set the mode (release, debug, or profile), set the main entry point, split per ABI, set target platforms, set the build name and version, build flavor APK, and obfuscate the code.
Set Mode
Build Release APK
By default, if not specified, the generated APK files use release mode.
The above command is the same as the below command which explicitly uses release mode.
Build Debug APK
Build Profile APK
Set Main Entry-Point File
Split per ABI and Set Target Platforms
Android devices use various CPUs. A large portion of devices uses ARM-based CPUs, while some devices use Intel x86 CPUs. In addition, old devices use 32-bit CPUs, while the majority of devices released in recent years already use 64-bit CPUs. Those different CPUs support different instruction sets. By default, Flutter only generates an APK that works for all platforms. But that causes a problem regarding the build size of the APK. It becomes very big in order to support those different instruction sets.
The result of the above command will generate the below platform-specific APK files.
At the time this post was written, Flutter doesn’t support to build release version for x86 ABI. You can only build the debug version if you want to create an APK that supports x86 devices.
Set Build Name and Version
The build name and build number of a Flutter application is defined in the version property of pubspec.yaml file. The value of version is a three numbers separated by dots which specifies the build name, followed by an optional build number separated by a ‘+’. For example, version: 1.0.1+2 means the build name is 1.0.1, while the build number is 2. The build name is the one that’s shown to the users, while the build version is used as an internal version number.
Build Flavor APK
Split Debug Info
Code Obfuscation
Disable pub get Command
Analyze Size
Below is the output example of running the above command.
Summary
How to build signed apk from Android Studio for Flutter
Is there a way to build an apk for flutter in Android Studio?
I have seen this guideline: https://flutter.dev/docs/deployment/android
But here flutter console is used to build apk.
10 Answers 10
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
You can build the Apk/AppBundle using IDE and command line.
Building APK/AppBundle through IDE:
Step-1
In Android Studio’s tab bar, click on Tools and then Flutter and then Open Android module in Android Studio :
Step-2
Open Project it in New Window:
Step-3
Building APK/AppBundle through command:
Step-1:
Modify your build.gradle(app) file and include your key information there:
Step-2:
Build AppBundle:
Build APK:
Step 1 :
Then Select New Window option as shown below
Step 2 :
Step 3 :
Select Android App Bundle or APK Option as per your need. (Android App Bundle is best solution) and click Next button.
Step 4 :
Select Create new.. option to generate new Signed key (When you release your app First Time)
Step 5 :
Fill all options as follow
Path where your key store file i.e .jks file stored (as shown in above image). [Best way Select the path within your project directory.]
Enter password e.g. 123456
Enter Key password (Choose different password than Key store password) e.g. key123456
Fill certificate information (Not all fields are mandatory)
Then click OK and you will get following screen.
Click Next button and you will get following screen.
Signature versions both V1 and V2 respectively as shown above screen shot and click Finish button.
Wait for a while until Gradle Build Running process complete as shown below.
That’s it you generated Signed APK successfully for your Flutter project.
IMPORTANT:
Please DO NOT lose the key and its all information i.e. Key store path,Key store password, Key alias and Key password (Best way write down it in to the note book or make text file and store on your drive while generating it.). Without this, you won’t be able to update your application because the new release will need to be signed with the same key.
I hope you will get detailed information.
Instead of using Android Studios UI you could easily also just use a command prompt. This way was way easier for me, cause I had many issues creating a signed apk by using the ui.
Before you create the signed APK you must prepare your project.
1. Add Flutter automation dependencies to your pubspec.yaml
Currently ^1.1.4 is the newest dependency, this will eventually change.
2. Get the new dependency
Run flutter packages get in your command prompt in your projects directoy
3. Setup the signing
4. Generate your signed apk or bundle
For a FULL Solution that helps everyone, (including those that cant find «open for editing in android studio» option in the sdk) follow steps below..
Lets say you have a project called «YourProjectName». Copy your project name in flutter as i have censored in my picture below, and right-click the android folder to create a new file.. The name on the new file must be «YourProjectName_android.iml»
After creating the file in the format ive described in step 1, you will now fully see «Open for editing in android studio» option..
Now let it build after clicking the option in step 2 above, then you click the usual option of «Generate signed apk» as you see in the picture below
As per DanyPata For flutter you have to set the signing file and then run flutter build APK. It’s quite important to run the flutter command so that all the changes/libs added in flutter are also added in the Android project.
Step 1
For a release build, you will want to select the Signing Config you previously created and leave the rest as default.
Keep a close eye on the Event Log to see when its complete and the path where APK was saved to.
Step 2
Update the gradle.build file. Make sure your project visibility is set to something like Project so you can see all the files in the file tree.
Under, android add:
IMPORTANT: As a quick reminder and precaution, DO NOT lose the key it creates. Without this, you won’t be able to update your application because the new release will need to be signed with the same key.
How to create a Flutter App on both iOS and Android.
In this article, we are going to go over the steps to be followed while creating a Flutter Application on both iOS and Android platforms.
To learn about why we chose Flutter for our Mobile App Development, click on the link below:
Why choose Flutter for Mobile App Development?
This article draws comparisons between Native vs. React Native vs. Flutter for your Mobile App Development.
Table of Contents:
Prerequisites
Setting up the Flutter Environment
Follow these steps to install Flutter and Dart extensions:
From the picture:
1 → Click on the Extension symbol.
2 → Search for the Flutter extension.
3 → Click on the Flutter version.
4 → Click on the install button.
The same thing applies to install Dart or any extension. Now you’re ready to create a Flutter project.
Creating a Flutter Application: Android
From the picture:
1 → In our case, type
and press Enter to create an App.
2 → Flutter provides you with all the dependencies you need.
Creating an App in the Mobile device
Flutter will automatically search for the external device which you have connected through the USB cable. You can see that in action in the below figure:
Remove the USB cable and you can see the created app in an external device.
Creating an APK file
1 → Go to the build folder in your project folder where you can see the app.apk file. It is used for testing purposes.
2 → In the Release folder, you can see the releaseapp.apk file which is a full version of the app where we can push that releaseapp.apk to the play store or the app store.
Creating a Flutter Application: iOS
To create your first Flutter app and test your setup, follow these steps:
$ flutter create demo_app
2. A demo_app directory is created that contains all the files and dependencies needed for the app to run.
3. Ensure that an iOS simulator is running to launch the app. Open the simulator by running the following:
4. Now that the setup is ready to launch the app, run the following commands to change the directory to demo_app and run the app:
5. To launch the app on an iOS device you need the third-party CocoaPods dependency manager. Run the following to install and setup CocoaPods:
6. When the app is created in step 1, flutter creates a folder called ios. This folder all relevant file structures to build and run an iOS app. Ensure that you are still in demo_app folder and run the following command to open the workspace project in XCode:
7. The runner project opens up in your XCode editor, which might look similar to the screenshot below. Click on the Runner folder to go to the project settings page. Click on the Runner from TARGETS and go to Signing & Capabilities section as shown in the image:
8. You also need an Apple Developer account to deploy the app on your iOS device. So, choose your account from Team dropdown. Alternatively, you can also create a new account from the same dropdown by selecting Add an Account… option.
9. The next step is to specify a unique name for your app bundle. Replace the default bundle identifier com.example.demoApp to your own name (should be unique). In our case, we have replaced it with ai.fiolabs.demoApp
10. Ensure that Status message disappears and a new Signing Certificate is created for the app.
11. Ensure that your device is connected to your Mac via USB and run the following to launch the app.
12. Now type the following command:
13. Run the following command to build a deployment package for your app on iOS.
$ flutter build ios
Please note that you might run into version conflicts particularly if you are using XCode version 11.4. If you notice one of the following errors while running flutter run command, please refer to this article on flutter.dev:
Building for iOS, but the linked and embedded framework ‘App.framework’ was built for iOS Simulator. Building for iOS Simulator, but the linked and embedded framework ‘App.framework’ was built for iOS.
What are you waiting for?
We believe FIO Labs never fails to keep their promise when it comes to providing quality services. Our enterprise expertise and industry leadership mean you’re in safe hands.
Источники информации:
- http://docs.flutter.dev/deployment/android?source=post_page—————————
- http://www.woolha.com/tutorials/flutter-build-android-apk
- http://stackoverflow.com/questions/55536637/how-to-build-signed-apk-from-android-studio-for-flutter
- http://medium.com/fiolabs-datascience/how-to-create-a-flutter-application-and-apk-59f0c3e0700c