How to pass flutter build arguments to snapcraft command when building flutter app using snapcraft?

How to pass flutter build arguments to snapcraft command when building flutter app using snapcraft?

name: app_name
version: 1.0.0
summary: My App description.
description: My App description.
icon: snap/gui/app_name.png
title: AppName
contact: support@mail.com
license: MIT
architectures:
  - build-on: [ amd64 ]
  - build-on: [ arm64 ]

confinement: strict
base: core22
grade: stable

slots:
  dbus-app_name: # adjust accordingly to your app name
    interface: dbus
    bus: session
    name: app.app_name # adjust accordingly to your app name and

apps:
  app_name:
    command: app_name
    extensions: [gnome] # gnome includes the libraries required by flutter
    plugs:
      - network
    slots:
      - dbus-app_name
parts:
  app_name:
    source: .
    plugin: flutter
    flutter-target: lib/main.dart # The main entry-point file of the application

Hi ! @kimmy214 .

What’s the goal ?

Thanks for the reply. Also a big thanks to the ubuntu-flutter-community channel on discord. The purpose is to customize the parameters of flutter build linux.

This problem has been solved, the solution is as follows.

1、Update snapcraft.yaml

parts:
  app_name:
    source: .
    plugin: flutter
    flutter-target: lib/main.dart # The main entry-point file of the application
    override-build: |
      cp -r build/linux/*/release/bundle/* $CRAFT_PART_INSTALL/

2、I run flutter build linux --release --dart-define-from-file=.env --verbose --target lib/main.dart in the root directory of the flutter project, and then run snapcraft --verbose --output app_name.snap

2 Likes

Well ! @kimmy214 .

So, you can close this topic . :wink:

Sorry, I can’t find the close button. If possible, could you help close it?

@kimmy214 Click on the three dotted lines next to the solution you shared with me, then press solution. And voila. :wink:

Is it this button? After clicking, an additional delete button will be displayed.

1 Like

@kimmy214 It’s already done !

The Solution box is checked.

This needs a little bit update, if you just run override-build without using the flutter build commands, the app will not build, it’ll just execute the cp command, which in turn would find empty files.

I’m a newbie in snapcraft and I don’t understand what you mean, can you elaborate? Is there something wrong with this solution of mine?

as I explained in the discord channel, override-build is used only to use some different commands. A quick example

override-build: |
  echo "hello"

output:

hello

override-build: |
  craftctl default
  echo "hello"

output:

# the built script set  by the plugin runs
hello

override-build: |
  echo "hello"
  craftctl default

output:

hello

# build script set  by the plugin runs

I understand what you mean is that it is best to execute the flutter build linux command in the override-build block, otherwise execute this line of code cp -r build/linux/*/release/bundle/* $CRAFT_PART_INSTALL/ , it is possible to copy an empty file to the directory corresponding to $CRAFT_PART_INSTALL

That folder will not be there if it’s not built. So, the copy will fail eventually.

I also want to execute the flutter build linux command in the override-build block, but I face two problems:

  1. As you said in the discord channel, First it builds flutter in the pull stage, but I don’t know about the built-in environment variables(such as the installation directory of flutter) and how to get the value of the plug-in’s configuration parameters (such as flutter-target) in the build stage, and I didn’t find the documentation so I can’t run the flutter script

  2. I need to get the environment variables from the CI/CD system and then pass them to the flutter build linux command. How do I get the environment variables in CI/CD in the override-build block? Taking GitHub Actions as an example, does it look like this?

1,

jobs:
   snap:
     name: Create snap
     runs-on: ubuntu-latest
     steps:
       - name: Build snap
         run: |
           # https://snapcraft.io/docs/flutter-applications
           snapcraft --output app_name.snap
         env:
           TEST_ENV_BASE64: ${{ secrets.TEST_ENV_BASE64 }}

2,

parts:
   app_name:
     source: .
     plugin: flutter
     flutter-target: lib/main.dart # The main entry-point file of the application
     override-build: |
       echo "$TEST_ENV_BASE64" | base64 --decode > .env
       flutter build linux --dart-define-from-file=.env
       cp -r build/linux/*/release/bundle/* $CRAFT_PART_INSTALL/

https://snapcraft.io/docs/flutter-plugin

You can have build-environment which you can use. eg:

parts:
  app:
    build-environment:
      - TEST_ENV_BASE64: #set it here if it's static

Thank you very much for your help.

1.1、In the ‘override-build’ block, I can get all the environment variables by executing the ‘env’ command. I also found that the flutter installation directory has been added to the path, but when executing ‘flutter build linux’, the error ‘flutter: command not found’ is reported.

1.2、I configured ‘parts.app_name.flutter-target’ in ‘snapcraft.yaml’, so how can I get this value in the ‘override-build’ block? I didn’t find a solution in the document link you gave me.

2、This can’t be written in plain text in a config file, because my project is open source. it’s not static, so ‘build-environment’ doesn’t work, I’ve tried that.

1.1 - Does flutter installation is set in user .profile/.bashrc? If so, you need to source it within override-build 2. It can be generated on the fly with some simple scripting eventually.

Thank you very much. It works for me.

parts:
  app_name:
    source: .
    plugin: flutter
    # flutter-target: lib/main.dart # The main entry-point file of the application
    build-environment:
      - FLUTTER_REPO: https://github.com/flutter/flutter.git
      - FLUTTER_CHANNEL: stable
      - FLUTTER_TARGET: lib/main.dart
      - TARGET_PLATFORM: linux-x64
    override-build: |
      # https://github.com/canonical/snapcraft/blob/main/snapcraft/parts/plugins/flutter_plugin.py
      PG_FLUTTER_BUILD_DIR="$CRAFT_PART_BUILD/flutter-distro"
      if [ -d "$PG_FLUTTER_BUILD_DIR" ]; then
        echo "$PG_FLUTTER_BUILD_DIR"
        rm -rf $PG_FLUTTER_BUILD_DIR
      fi
      git clone --depth 1 -b $FLUTTER_CHANNEL $FLUTTER_REPO $PG_FLUTTER_BUILD_DIR
      flutter doctor -v
      flutter precache --linux
      flutter pub get
      flutter build linux --release --dart-define-from-file=.env --target-platform $TARGET_PLATFORM --target $FLUTTER_TARGET
      cp -r build/linux/*/release/bundle/* $CRAFT_PART_INSTALL/
1 Like