How to build a Flutter app with different icons depending on --dart-define parameter

Viewed 618

I am using a --dart-define=myEnv=production parameter to define the app environment for build. (this is not the same as --release or --debug flutter build arguments).

When building with the --dart-define=myEnv=prod parameter, I need to have one application icon, and for build with a different value of myEnv variable or without this variable, I need to have another app icon.

I am not using any third-party libraries for the icon.

My Android icons are stored in the path: android/app/src/main/res

And iOS icons are stored in the path: ios/Runner/Assets.xcassets/AppIcon.appiconset

I just want to put the second icons on different paths, and in the Info.plist and AndroidManifest.xml files indicate something like:

...
<key>CFBundleIconFile</key>
<string>$myEnv/icon.png</string>
...
    <application
        tools:replace="android:label,android:theme"
        android:name=".Application"
        android:label="MyCoolApp"
        android:theme="@style/Theme.AppCompat"
        android:icon=$myEnv/"@mipmap/launcher_icon">
...
1 Answers

there are some steps to do that:

Prescription:

  • I will provide example when I use 3 env (I use variable like that: --dart-define=ENV_FLUTTER_IMG_SUFFIX=_dev to make that little bit laconic but you could use your own variables
  • don't use . (dot) in name, android has some problem with that
  1. get your variables inside native code if you haven't made that yet (Flutter --dart-define)
  2. create 3 sets of ios icons with names where second part is value of your variable (example for _dev, _stage and empty (for prod)) Ios icons
  3. create 3 sets of android icons with names where second part is value of your variable (example for _dev, _stage and empty (for prod)) Android Icons
  4. go to you project in Xcode, go to target -> Build Settings -> find Asset Catalog App Icon Set Name
  5. change AppIcon to AppIcon$(ENV_FLUTTER_IMG_SUFFIX)
  6. got to ./android/app/src/build.gradle
  7. add to your defaultConfig 2 (if you want to use round icon too) or 1 new variable:
resValue "string", "img_name", "@mipmap/ic_launcher$dartEnvironmentVariables.ENV_FLUTTER_IMG_SUFFIX"
resValue "string", "img_round_name", "@mipmap/ic_launcher_round$dartEnvironmentVariables.ENV_FLUTTER_IMG_SUFFIX"
  1. go to ./android/app/src/main/AndroidManifest.xml
  2. change your icons defines to
android:icon="@string/img_name"    
android:roundIcon="@string/img_round_name"
Related