Flutter构建Debian包插件flutter_build_debian的使用

发布于 1周前 作者 phonegap100 来自 Flutter

Flutter构建Debian包插件flutter_build_debian的使用

flutter_build_debian是一个简单的命令行工具,帮助你将你的Flutter应用程序打包成一个可以用于生产的Debian包。

Debian包构建概述 📦

这个插件基于debian.yaml文件中的指令构建Debian包。为了更好地理解和掌握核心概念,可以查看Debian文档

flutter_build_debian YAML示例

yaml文件放置在你的Flutter项目的根目录下,路径为<项目根目录>/debian/debian.yaml。例如:

flutter_app: 
  command: mega_cool_app
  arch: x64
  parent: /usr/local/lib
  nonInteractive: false

control:
  Package: mega-cool-app
  Version: 1.0.0
  Architecture: amd64
  Priority: optional
  Depends:
  Maintainer:
  Description: Mega Cool App that does everything!

以下部分解释了YAML文件中的各个部分。

Flutter应用配置

flutter_app: 
  command: mega_cool_app
  arch: x64
  parent: /usr/local/lib
  nonInteractive: false
命令

指向你的Linux发布包中的二进制文件,并在调用Debian包时运行。

架构

你的应用的构建架构。

执行字段代码

Exec键在桌面文件中的字段代码列表,以逗号分隔。

示例: execFieldCodes: f,u,i 支持的代码: f, F, u, U, i, c, k

更多信息请参见FreeDesktop规范

父目录

应用程序将安装在这个目录下的子目录中(如mega_cool_app)。

非交互式

当设置为true时,在包安装过程中不会显示确认提示。

默认值: /opt

示例:

parent: /usr/local/lib

目标目录为/usr/local/lib/mega_cool_app

包维护脚本

如果存在debian/scripts目录,则该目录下的可执行文件(如preinstpostinstprermpostrm)将被复制到包中作为Debian包维护脚本。

注意:任何preinst脚本都会覆盖由此工具生成的默认脚本,从而禁用安装前的交互确认提示(就像设置了nonInteractive: true一样)。

额外文件

如果存在debian/skeleton目录,这些文件将被复制到包中。

这可用于默认配置文件。

示例:

debian/skeleton/etc/megacool/main.conf 将被安装为 /etc/megacool/main.conf

控制信息

这是向APT包管理器或其他软件描述你的应用的信息。

依赖项

列出你的项目所依赖的库,这些库将在你的应用安装到系统之前通过APT包管理器进行安装。

例如:

pub.dev上的flutter_secure_storage Linux版本依赖于libsecret-1-0和libjsoncpp1
注意:多个依赖项用逗号分隔。

桌面文件和图标 🖥️

桌面入口文件用于将应用添加到桌面菜单。这些文件指定了应用的名称和图标、所属类别、相关搜索关键词等。这些文件扩展名为.desktop,遵循XDG桌面入口规范。

Flutter mega-cool-app.desktop示例

.desktop文件放置在你的Flutter项目的根目录下,路径为<项目根目录>/debian/gui/mega-cool-app.desktop

注意: 图标和.desktop文件名必须与yaml文件中的应用名相同。

例如:

[Desktop Entry]
Version=1.0
Name=Mega Cool App
GenericName=Mega Cool App
Comment=A Mega Cool App that does everything
Terminal=false
Type=Application
Categories=Utility
Keywords=Flutter;share;files;

将带有.png扩展名的图标放置在你的Flutter项目的根目录下,路径为<项目根目录>/debian/gui/mega-cool-app.png

构建Debian包 📦

一旦gui/文件夹和debian.yaml文件准备就绪,从项目的根目录运行flutter_build_debian

首先构建你的项目:

$ flutter build linux

如果尚未安装flutter_build_debian,则先安装它:

$ dart pub global activate flutter_build_debian

然后运行flutter_build_debian

$ flutter_build_debian

查找并安装你的.deb应用包 📦

从项目的根目录开始查找:

$ cd debian/packages && ls

安装:

$ sudo dpkg -i [package_name].deb

依赖项查找

flutter_build_debian命令可以用来查找一组库文件的依赖项。

运行flutter_build_debian help可以看到这个描述:

Usage: flutter_build_debian [<mode> [<options>] ]
<mode>:
  help
    Print this usage.
  create
    Creates a folder and template files for the Debian package.
  build
    Build a Debian package file *.deb. This is the default mode.
  dependencies [<opts>] [<file1> [ <file2>...]]
    Detects the dependencies of a amount of library files.
    <fileX> can be a file or a directory.
<opts>:
   --excluded-packages=<comma-separated-list-of-names>
     That packages will be excluded from detection
   --excluded-libraries=<pattern>
     Excludes that library files from detection. <pattern> is a regular expr.
Examples:
flutter_build_debian
  Creates the Debian package described in ./debian/debian.yaml
flutter_build_debian dependencies
  Detects the dependencies of the files in ./build/linux/x64/release/bundle/lib
  and uses the information of debian/debian.yaml
flutter_build_debian dependencies --excl-lib=-dev|^my_lib release/libs
  Detects the dependencies of the files in release/libs without the excluded
  specified by "-dev|^my_lib":
  The file release/libs/our_lib-dev.2.so and release/libs/my_lib-dev.2.so
  will be excluded from detection.
flutter_build_debian dependencies --excluded-packages=lintian,my-project prod/libs
  Detects the dependencies of the files in prod/libs
  The packages lintian and my-project will be excluded from processing.
Note: modes and options can be abbreviated: --ex-pack means --excluded-packages

更多关于Flutter构建Debian包插件flutter_build_debian的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter构建Debian包插件flutter_build_debian的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何使用 flutter_build_debian 插件来构建 Debian 包的示例代码和步骤。请注意,flutter_build_debian 并不是一个官方或广泛认可的 Flutter 插件,因此以下示例假设你有一个自定义的脚本或工具链来处理这个任务。在实际应用中,你可能需要使用类似 fpm(Effing Package Manager)这样的工具来手动打包 Debian 包。

不过,为了展示一个概念性的流程,我们可以假设你有一个自定义的 Flutter 构建脚本,它使用 fpm 来生成 Debian 包。以下是一个基本的流程示例:

  1. 安装必要的工具: 首先,确保你的系统上安装了 Flutter、Dart 以及 fpm。你可以通过以下命令安装 fpm(以 Ubuntu 为例):

    sudo apt-get update
    sudo apt-get install ruby-dev
    sudo gem install fpm
    
  2. 构建 Flutter 应用: 使用 Flutter 命令构建你的应用为 Linux 桌面应用。

    flutter config --enable-linux-desktop
    flutter build linux
    

    这将生成一个位于 build/linux/x64/release/bundle/ 的目录,其中包含你的应用文件。

  3. 创建 Debian 包: 使用 fpm 命令将构建好的应用打包为 Debian 包。以下是一个示例命令:

    fpm -s dir -t deb -n my_flutter_app -v 1.0.0 \
        --architecture amd64 \
        --depends "libc6,libgtk-3-0" \
        --description "My Flutter App for Linux" \
        --maintainer "your-email@example.com" \
        ./build/linux/x64/release/bundle/=/opt/my_flutter_app
    

    这里解释一下各个参数:

    • -s dir:指定源类型为目录。
    • -t deb:指定目标类型为 Debian 包。
    • -n my_flutter_app:设置包名为 my_flutter_app
    • -v 1.0.0:设置包版本为 1.0.0
    • --architecture amd64:指定包架构为 amd64
    • --depends "libc6,libgtk-3-0":指定包依赖。
    • --description "My Flutter App for Linux":设置包描述。
    • --maintainer "your-email@example.com":设置维护者邮箱。
    • ./build/linux/x64/release/bundle/=/opt/my_flutter_app:指定源文件和目标安装路径。
  4. 验证生成的 Debian 包: 你可以使用 dpkg 命令来安装并验证生成的 Debian 包:

    sudo dpkg -i my_flutter_app_1.0.0_amd64.deb
    

    然后,你可以运行你的应用来确保一切正常:

    /opt/my_flutter_app/my_flutter_app
    

请注意,上述示例是一个简化的流程,实际项目中可能需要根据具体需求进行调整。例如,你可能需要添加更多的依赖、设置正确的文件权限、创建启动脚本等。此外,如果你确实在寻找一个官方的或广泛认可的 Flutter Debian 包构建插件,建议查看 Flutter 社区或 Pub.dev 上的相关资源,因为 Flutter 生态系统在不断发展,新的工具和插件可能已经出现。

回到顶部