GDG Moi University

Flutter with AI

Google Developer Group
Moi University

Setting Up Flutter for Android Development

A step-by-step guide to installing and configuring Flutter and Android Studio

Overview

Before you can start building Flutter apps for Android, you need to set up your development environment. This involves installing the Flutter SDK, Dart (which comes with Flutter), and Android Studio with the necessary plugins and SDKs.

This guide will walk you through the setup process for Windows, macOS, and Linux operating systems.

1. Installing Flutter SDK

The Flutter SDK contains the tools and libraries needed to develop Flutter applications.

Windows

  1. Download the latest stable Flutter SDK from the official Flutter website.
  2. Extract the zip file to a location like C:\flutter (avoid spaces in the path and avoid protected directories like Program Files).
  3. Add Flutter to your PATH:
    • Search for "Environment Variables" in the Windows search bar.
    • Click on "Edit the system environment variables".
    • Click on "Environment Variables".
    • Under "System variables", find the "Path" variable, select it, and click "Edit".
    • Click "New" and add the path to the Flutter bin directory (e.g., C:\flutter\bin).
    • Click "OK" to save.

macOS

  1. Download the latest stable Flutter SDK from the official Flutter website.
  2. Extract the file to a location like ~/flutter.
  3. Add Flutter to your PATH by adding the following line to your shell profile (~/.zshrc for Zsh or ~/.bash_profile for Bash):
export PATH="$PATH:~/flutter/bin"

Then, reload your shell configuration:

source ~/.zshrc  # or source ~/.bash_profile for Bash

Linux

  1. Download the latest stable Flutter SDK from the official Flutter website.
  2. Extract the file to a location like ~/flutter.
  3. Add Flutter to your PATH by adding the following line to your shell profile (~/.bashrc or ~/.zshrc):
export PATH="$PATH:~/flutter/bin"

Then, reload your shell configuration:

source ~/.bashrc  # or source ~/.zshrc for Zsh

Verify Flutter Installation

To verify that Flutter is installed correctly, run:

flutter --version

You should see output showing the Flutter version, channel, and other information.

2. Installing Android Studio

Android Studio is the recommended IDE for Flutter development for Android.

  1. Download Android Studio from the official Android Studio website.
  2. Install Android Studio following the installation wizard.
  3. During setup, make sure to install the Android SDK, Android SDK Platform-Tools, and Android SDK Build-Tools.
  4. After installation, open Android Studio and go through the initial setup wizard.

Installing Flutter and Dart Plugins

  1. Open Android Studio.
  2. Go to File > Settings (on Windows/Linux) or Android Studio > Preferences (on macOS).
  3. Select Plugins from the left sidebar.
  4. Search for "Flutter" in the Marketplace tab.
  5. Click "Install" next to the Flutter plugin.
  6. The Dart plugin will be installed automatically with Flutter.
  7. Restart Android Studio when prompted.

3. Setting Up Android Emulator

To test your Flutter apps without a physical device, you'll need to set up an Android emulator.

  1. Open Android Studio.
  2. Click on "More Actions" or "Configure" from the welcome screen.
  3. Select "AVD Manager" (Android Virtual Device Manager).
  4. Click on "Create Virtual Device".
  5. Select a device definition (e.g., Pixel 6).
  6. Select a system image (e.g., API 35 with Google Play).
  7. Configure the AVD with any additional settings.
  8. Click "Finish" to create the virtual device.

You can start the emulator from the AVD Manager by clicking the play button next to your virtual device.

4. Setting Up Physical Device

If you prefer to test on a physical Android device, you'll need to enable Developer Options and USB Debugging.

  1. On your Android device, go to Settings > About phone.
  2. Tap on "Build number" seven times to enable Developer Options.
  3. Go back to Settings, and you should now see "Developer options" near the bottom.
  4. Open Developer Options and enable "USB debugging".
  5. Connect your device to your computer with a USB cable.
  6. When prompted on your device, allow USB debugging.

5. Verifying Setup with Flutter Doctor

Flutter provides a convenient tool called "flutter doctor" that checks your environment and displays a report of the status of your Flutter installation.

Run the following command in your terminal or command prompt:

flutter doctor

This command checks for the Flutter SDK, connected devices, and required dependencies. If there are any issues, flutter doctor will provide instructions on how to resolve them.

A successful output should look something like this:

[✓] Flutter (Channel stable, 3.19.3, on macOS 14.4.1) [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) [✓] Xcode - develop for iOS and macOS (Xcode 15.3) [✓] Chrome - develop for the web [✓] Android Studio (version 2023.1) [✓] VS Code (version 1.86.2) [✓] Connected device (2 available) [✓] Network resources

6. Common Setup Issues and Solutions

Flutter Not Found in PATH

If you see an error like "flutter: command not found", it means Flutter is not properly added to your PATH.

  • Double-check that you've added Flutter to your PATH as described in step 1.
  • Restart your terminal or command prompt.
  • On Windows, you might need to restart your computer for PATH changes to take effect.

Android SDK License Issues

If flutter doctor shows license issues, run:

flutter doctor --android-licenses

Accept all the license agreements when prompted.

Missing Dependencies

On Linux, you might need to install additional dependencies. Flutter doctor will list the required packages. Install them using your distribution's package manager.

sudo apt-get install <package-names>  # For Ubuntu/Debian

Device Not Detected

If your physical device is not detected:

  • Make sure USB debugging is enabled.
  • Try a different USB cable or port.
  • Install the appropriate USB drivers for your device (especially on Windows).
  • Restart your computer and device.

7. Creating Your First Flutter Project

Now that your environment is set up, let's create a simple Flutter project to verify everything is working correctly.

Using the Command Line

Run the following commands in your terminal or command prompt:

flutter create my_first_app cd my_first_app flutter run

This will create a new Flutter project called "my_first_app", navigate into the project directory, and run the app on your connected device or emulator.

Using Android Studio

  1. Open Android Studio.
  2. Click on "New Flutter Project" from the welcome screen.
  3. Select "Flutter" as the project type.
  4. Verify the Flutter SDK path is correct.
  5. Enter a project name (e.g., "my_first_app").
  6. Choose a project location.
  7. Click "Finish" to create the project.
  8. Once the project is created, click the "Run" button (green triangle) to run the app.

Pro Tip

When working with Flutter, make sure to regularly update your Flutter SDK to get the latest features and bug fixes. You can update Flutter by running flutter upgrade in your terminal.