Flutter Environment Installation && Running#
Background#
Memo record of Flutter environment configuration and running, including issues encountered such as "Android sdkmanager tool not found" and "Running Gradle task 'assembleDebug'..."
Flutter Installation#
Super Minimal Version#
Install directly through homebrew
, internet connection may be required.
brew install flutter
Manual, Flutter Environment Setup#
-
First, download the Flutter installation package from the following link: https://flutter.dev/docs/development/tools/sdk/releases?tab=macos
-
Next, extract the downloaded files and place them in a specified directory. It is recommended to place them in the user's root directory as the bin file address needs to be specified later. This directory should preferably remain unchanged.
-
Then, configure the environment variables.
Open the terminal and if using zsh, enteropen .zshrc
; (if using bash, enteropen .baseprofile
). Add the following code at the end of the file, wherepwd
represents the address of the previously extracted flutter/bin files:export PATH="$PATH:`pwd`/flutter/bin"
Additionally, due to reasons in China, the following two environment variables can also be added:
export PUB_HOSTED_URL=https://pub.flutter-io.cn export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
As shown in the image below:
After editing the
.zshrc
(or.bashprofile
) file, save and close it, then run:source .zshrc // If editing .bashprofile, enter source .bashprofile
Finally, run
flutter doctor
to check if Flutter is installed correctly.
Editor Installation#
-
Android Studio
After downloading and installing Android Studio, there are three additional installations required:- SDK Manager: Install SDK Platforms and add the necessary Android versions based on your needs.
- AVD Manager: Add Android emulators.
- Plugins: Add Flutter and Dart plugins.
As shown in the images below:
-
Xcode: Download and install Xcode, then open it once.
-
VSCode: Open the plugin interface of VSCode, search for Flutter, and install it.
Verify Environment#
Enter flutter doctor
to check if the environment is properly installed. You may encounter issues, which can be referred to in the problem record below.
Enter flutter doctor --android-licenses
and select y/N when prompted. Choose y for all prompts.
Create Project#
Create a Flutter project using VSCode. Open VSCode (make sure to install the Flutter plugin) and press CMD+Shift+P
to bring up the quick command menu. Enter Flutter and select Flutter: New Application Project, as shown below:
Then, choose the project directory and name.
Run Emulator#
List supported emulators:
flutter emulator
The result will be as follows:
Open a specific emulator:
flutter emulators --launch xxx
Run the emulator:
flutter run
Then, select the desired emulator to run, as shown below:
Problem Record#
-
When running
flutter doctor
, the error "Android sdkmanager tool not found" is encountered.Solution: Open Android Studio, click Config -> SDK Manager, select SDK Tools, uncheck "Hide Obsolete Packages" at the bottom, then you will be able to see "Android SDK Tools (Obsolete)". Check Apply and wait for the download to complete.
-
When running
flutter doctor --android-licenses
, the error "Failed to install android-sdk: “java.lang.NoClassDefFoundError:`" is encountered.Solution: Open Android Studio, click Config -> SDK Manager, select SDK Tools, uncheck "Hide Obsolete Packages", then you will be able to see "Android SDK Command-line Tools (latest)". Check Apply and wait for the download to complete.
-
Stuck at "Running Gradle task 'assembleDebug'" when starting the emulator.
Background: After configuring the Flutter environment and selecting an Android emulator, it gets stuck at "Running Gradle task 'assembleDebug'".
Cause: Gradle's Maven repository is located overseas, so the Alibaba Cloud mirror address needs to be used.
Solution:
Modify the
andriod/build.gradle
file, change the repositories to:maven { url 'https://maven.aliyun.com/repository/google' } maven { url 'https://maven.aliyun.com/repository/jcenter' } maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
As shown below: