Flutter Android Build Process, Flavor Configuration, and SDK Bug Analysis
This article explains how to configure product flavors in a Flutter Android project, create multiple entry files for each flavor, detail the underlying Gradle and Flutter build commands, and analyze a case‑sensitive SDK bug with practical solutions for successful APK generation.
Flutter is a cross‑platform UI framework promoted by Google, but when building Android apps you still need native support such as Gradle flavor configuration and automatic generation of BuildConfig files.
The article demonstrates how to define three environments (development, test, production) in productFlavors { ... } and how to create separate entry files ( main_android_dev.dart, main_android_test.dart, main_android_online.dart) that initialize different AppConfig values before calling runApp().
Flavor‑specific APKs are built with the command:
flutter build apk --[debug|release] --flavor [flavor] -t lib/main_android_[flavor].dartThe Flutter build process starts from flutter_tools/bin/flutter_tools.dart, which invokes BuildCommand(). For Android, BuildApkCommand() eventually calls androidBuilder.buildApk(), which runs buildGradleApp() in flutter_tools/lib/src/android/gradle.dart. This method prepares the Gradle task, executes it, and checks the generated APK file.
The Gradle script reads local.properties to obtain the Flutter SDK path, version code, and version name, then applies flutter.gradle. The flutter.gradle file defines a FlutterTask that performs the actual compilation.
A bug in Flutter SDK 1.22.4 caused Gradle to look for an APK with a case‑sensitive name (e.g., app-flavorDev-debug.apk) while the generated file was lower‑cased ( app-flavordev-debug.apk). The SDK’s gradle.dart used apkFilesPaths.first without converting to lower case, leading to a “Gradle build failed to produce an .apk file” error on case‑sensitive file systems like Ubuntu.
Three work‑arounds are provided: (1) install the APK manually via adb install, (2) ensure all flavor names are lower‑case, or (3) modify the SDK source to use apkFilesPaths.first.toLowerCase() and clear the Flutter tool cache ( flutter/bin/cache/flutter_tools.*) to recompile the SDK.
The bug has been fixed in the master branch by using androidBuildInfo.buildInfo.lowerCasedFlavor when constructing the APK name.
In summary, the article walks through flavor configuration, multiple entry files, the full Flutter‑Android build pipeline, identifies a case‑sensitivity bug, and offers practical solutions without altering the SDK source.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
