Ubuntu24.04 搭建 Flutter 開發環境

demodeom發表於2024-11-16

Ubuntu24.04 搭建 Flutter 開發環境

安裝 Flutter

Flutter 中文社群下載 https://docs.flutter.cn/get-started/install/linux/android

建立目錄 DevTools, 用來存放開發軟體

mkdir ~/DevTools

下載 Flutter

wget https://storage.flutter-io.cn/flutter_infra_release/releases/stable/linux/flutter_linux_3.24.5-stable.tar.xz

解壓

tar -xvf flutter_linux_3.24.5-stable.tar.xz

將 flutter 軟體複製到 DevTools 目錄

mv flutter_linux_3.24.4-stable/flutter ~/DevTools

配置系統環境變數

echo 'export PATH=$PATH:~/DevTools/flutter/bin' >> ~/.zshrc

配置 Flutter 映象

echo 'export PUB_HOSTED_URL=https://mirrors.tuna.tsinghua.edu.cn/dart-pub'  >> ~/.zshrc
echo 'export FLUTTER_STORAGE_BASE_URL=https://mirrors.tuna.tsinghua.edu.cn/flutter'  >> ~/.zshrc

安裝 Android Studio

推薦使用 Jetbrain ToolBox 安裝 Android Studio

配置 ADB 環境變數

export 'PATH=$PATH:~/Android/Sdk/platform-tools' >> ~/.zshrc

依賴

使用 Android Studio 安裝 cmdline

安裝系統依賴

sudo apt install clang  cmake ninja-build libgtk-3-dev -y

同意安卓協議,看到提示 **Accept? (y/N): **, 輸入 y, 按 Enter 鍵, 直到看到提示 All SDK package licenses accepted

flutter doctor --android-licenses

檢查依賴是否完全解決

flutter doctor
Flutter assets will be downloaded from https://mirrors.tuna.tsinghua.edu.cn/flutter. Make sure you trust this source!
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.24.4, on Ubuntu 24.04.1 LTS 6.8.0-48-generic, locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Chrome - develop for the web
[✓] Linux toolchain - develop for Linux desktop
[✓] Android Studio (version 2024.2)
[✓] Proxy Configuration
[✓] Connected device (2 available)
[✓] Network resources

• No issues found!

問題修復

錯誤1

[✗] Linux toolchain - develop for Linux desktop
    ✗ clang++ is required for Linux development.
      It is likely available from your distribution (e.g.: apt install clang), or can be downloaded from
      https://releases.llvm.org/
    ✗ CMake is required for Linux development.
      It is likely available from your distribution (e.g.: apt install cmake), or can be downloaded from
      https://cmake.org/download/
    ✗ ninja is required for Linux development.
      It is likely available from your distribution (e.g.: apt install ninja-build), or can be downloaded from
      https://github.com/ninja-build/ninja/releases
    ✗ GTK 3.0 development libraries are required for Linux development.
      They are likely available from your distribution (e.g.: apt install libgtk-3-dev)

解決方法

sudo apt install clang  cmake ninja-build libgtk-3-dev -y

錯誤2

[!] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    ! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses

使用以下命令, 同意協議

flutter doctor --android-licenses

看到提示 **Accept? (y/N): **, 輸入 y, 按 Enter 鍵, 直到看到提示 All SDK package licenses accepted

錯誤3

[✗] Chrome - develop for the web (Cannot find Chrome executable at google-chrome)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

Flutter 開發 Web 專案,需要安裝 Google Chrome 瀏覽器

方式1: 使用 flatpak 安裝

flatpak install flathub com.google.Chrome

方式2: 使用 deb 包安裝

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

sudo dpkg -i google-chrome-stable_current_amd64.deb

相關文章