I am a native Android developer and I started using flutter SDK. I developed a simple app by following official flutter doc. But I found that the debug app size is 46 MB which is too large for this simple app. Is there any way to optimize the app size? because Flutter app size is larger than the native Android App.
Flutter debug app is very big, to optimize the app you should build it as release version by using
flutter build apk (flutter build defaults to --release).
Preparing an Android App for Release
Preparing an iOS App for Release
There are many possibilities:
First , build your application in release mode by using :
In your terminal :
flutter build --release
or just specify the target :
For Android Apk : flutter build apk --release
For Android App Bundle: flutter build app bundle --release
For IOS : flutter build ios --release
By default, flutter run compiles to debug mode .This explains the
large size of the application . Debug mode (Hot reload , Dart Devtools etc ..) vs Release Mode (Simple Application)
By default flutter build build for release mode . So you can just do flutter build
Using --split-debug-info flag can dramatically reduce code size. For an example of using this flag, see Obfuscating Dart code.
Some of the other things you can do to make your app smaller are:
Remove unused resources
Minimize resource imported from libraries
Compress PNG and JPEG files
Your can learn more about flutter app size here
To Decrease/Optimise the App Size
Step 1: Compress assets all assets (Example: using tinypng or any other option)
Step 2: Delete unused resource
You can easily search for unused resources from Android Studio. Just press Ctrl + Alt +Shift + i and type "unused resources" (without quotes). That will execute lint. Super easy way to run lint commands (and other stuff from IDE).
OR
In Android Studio Menu > Refactor > Remove Unused Resources...
Select the resources you want to remove. You can exclude resources you want to keep by right-clicking on the resource item.
Use Do Refactor to remove all Resources at once.
Update: use ⌘ +Option +Shift + i for mac
Step 3: Put this code in app/build.gradle
Refer to this link to know more about android ABIS
(https://developer.android.com/ndk/guides/abis)
///Note: only working build release app ( cause debug run issue so comment it while run in debug mode )
android {
defaultConfig {
minSdkVersion 19
.....
//comment this code while app is in debug mode
ndk {
abiFilters "armeabi", "armeabi-v7a","arm64-v8a"
}
}
}
Step 4: Remove unused plugins from pubspec.yaml
Step 5: Build Your App Bundle using the following command
flutter build appbundle
flutter build appbundle --target-platform android-arm,android-
arm64,android-x64
For More
You can Use Proguard Rules
buildTypes {
release {
minifyEnabled true // add this
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' // add this
signingConfig signingConfigs.release // this is default for release
}
}
in the android main directory, you are able to find proguard-rules.pro if not then new-> file -> proguard-rules.pro create this file
Put this code in that file and also appropriate rules of plugins you have added in your pubspec.yaml
Note: Add all proguards which is required by plugin otherwise android app will not work properly
## Flutter wrapper
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.** { *; }
-keep class io.flutter.util.** { *; }
-keep class io.flutter.view.** { *; }
-keep class io.flutter.** { *; }
-keep class io.flutter.plugins.** { *; }
# -keep class com.google.firebase.** { *; } // uncomment this if you are
using firebase in the project
-dontwarn io.flutter.embedding.**
-ignorewarnings
Also go to your gradle.properties and add :
extra-gen-snapshot-options=--obfuscate
in your app/build.gradle
buildTypes {
release {
minifyEnabled true // added previously
shrinkResources true // add this
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro' // added previously
signingConfig signingConfigs.release // added previously
}
}
Another Command for build app
Old Apk with
flutter build apk --release
New Apk With
flutter build apk --split-per-abi
TO REDUCE APK SIZE:
If you are building android apk or bundle, then make sure you are considering
Minify
Proguard
Some common things to keep in mind
clean codes
remove unused plugins
remove unused assets
remove unused fonts etc.
Try this,
flutter build apk --split-per-abi
Follow the official doc - https://flutter.dev/docs/perf/app-size
Some of the obvious things you can do to make your app smaller are:
Remove unused resources
Minimize resource imported from libraries
Support a limited number of screen densities
Compress PNG and JPEG files
Android Studio includes an APK Analyzer that provides immediate insight into the composition of your APK or Android App Bundle after the build process completes. Using the APK Analyzer can reduce the time you spend debugging issues with DEX files and resources within your app, and help reduce your APK size. It's also available from the command line with apkanalyzer.
Open the project:
Drag an APK or app bundle into the Editor window of Android Studio.
Switch to the Project perspective in the Project window and then double-click the APK in the default build/output/apks/ directory.
Select Build > Analyze APK in the menu bar and then select your APK or app bundle
There's an explanation for this: https://developer.android.com/studio/command-line/apkanalyzer
Related
i want to generate an .aab file from flutter project using this command
flutter build appbundle
💪 Building with sound null safety 💪
Removed unused resources: Binary resource data reduced from 1224KB to 1154KB: Removed 5%
Running Gradle task 'bundleRelease'... 47.8s
✓ Built build/app/outputs/bundle/release/app-release.aab (21.7MB).
to put it in play.google.com but i get this problem
You uploaded an APK or Android App Bundle that was signed in debug
mode. You need to sign your APK or Android App Bundle in release mode
so in the file android/app/build.gradle i did some modification
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.release
}
}
but i get the same problem the .aab is in debug mode and not in release mode how to do it to upload the application in play store ?
You need to sign the app and use the release version, not the debug.
Their web page explains how: https://docs.flutter.dev/deployment/android#signing-the-app
I was creating APK for my flutter project so I followed the below steps to generate a signed APK
Tools -> Flutter -> Open for editing in android studio
Then opened the model in a new window
I got the Gradle sync failed error, After my further investigation I run the command gradlew --warning-mode all in the terminal & I got the below message from gradle
The AbstractArchiveTask.destinationDir property has been deprecated. This is scheduled to be removed in Gradle 7.0. Please use the destinationDirectory property instead.
I also tried org.grdle.wranning=(all,summary,fail,none)
In order to generate a signed APK you don't necessarily have to open your Android project as you described in your issue.
The steps you have to follow are :
1- Create your keystore.
2- Plug this keystore with the android/app/build.gradle file
3- Enable the signing in your release buildType in the android/app/build.gradle file
buildTypes {
release {
signingConfig signingConfigs.release
}
}
This is just an overview of the steps you have to follow.
The full procedure with examples in available on the official docs.flutter.dev website
Don't worry too much about the warning. It only means that you some of the methods used in your build.gradle will be deprecated soon.
I have apk of Flutter having size around 700MB (app has local videos, assets). I have uploaded testflight easily as Apple allows 4GB IPA file. Now to handle this issue on Google play console flutter recommend Deferred components
I followed the official documentation but on last step when I run flutter build appbundle
command with signing configuration
buildTypes {
release {
signingConfig signingConfigs.debug
}
}
differed components are separated from base apk , thats good.
but with
buildTypes {
release {
signingConfig signingConfigs.release
}
}
resulting in a deferred library being compiled as part of the base loading unit.
Any help would be appreciated.
I have and android project, which I added flutter on it as module,
In my original android app, multidex is enabled, but this do not help me, when running the app in debug it's working normally, but when creating a release apk or even debug apk the FlutterFragment is appearing as white, like the engine did not attach.
Things I notice, 'flutter run --release' will ask me to support multidex, and will not run, updating the minSdkVersion to 21 and enabling multidex in .android/app/build.gradle then again 'flutter run --release' works perfect, but this have no effect on the host app because it's under .android which automatically build, and ignored by the host app.
Also I tried to search for how to update minSdkVersion to 21 instead of 16, but references lead to change in the .android which have no effect.
Hey you can do two thing
Update your flutter SDK version by running command
flutter upgrade
The new version of flutter(2.10) multidex support automatically.
simply pass the --multidex flag to flutter build appbundle or flutter build apk and your app will support multidex.
2.In your app folder inside android [project_folder]/app/build.gradle and add following lines.
defaultConfig {
...
multiDexEnabled true
}
and
dependencies {
...
implementation 'com.android.support:multidex:1.0.3'
}
After a lot of investigation, the reason was related to something in the entry point, which needed to add project name, referring to this link
I am developing Flutter app. I have build my app as the following:
flutter build appbundle --obfuscate --split-debug-info=./debug -v
I'm facing the above warning on play store console:
There is no deobfuscation file associated with this App Bundle. If you
use obfuscated code (R8/proguard), uploading a deobfuscation file will
make crashes and ANRs easier to analyse and debug.
From android studio decode an obfuscated stack trace documentation it seems that I need to upload a mapping.txt file.
I cannot locate the file in my Flutter project. Where is it?
How to upload the file to play store console?
try enabling minify
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
mapping.txt file location
./app/build/outputs/mapping/release/mapping.txt