How to Setup Flutter for Building Android without Android Studio
Tags: flutter, android
Published: 2024-04-08 20:41:13

If you follow the official tutorial to install Android tools for Flutter development, it starts with downloading Android Studio. While this is the best way for beginners to get started quickly and avoid some common pitfalls, some advanced users may find it insufficient for their requirements. For example, when trying to set up a Continuous Integration environment on a headless remote server, the environment often times does not have a desktop environment and Android Studio cannot be started.

In this guide, we will walk through the steps needed to install Android build tools for Flutter to successfully build an Android application package without Android Studio.

*Note: Flutter can be used to target many different platforms such as iOS, Windows, Linux, macOS, web. This guide only deals with setting up for building Android.*

The steps can be roughly divided into:

1. Download Java and set up environment variables
2. Download Android SDK command line tools
3. Download Android SDK using Android SDK command line tools and set up environment variables
4. Download Flutter and set up environment variables

## Download Java and set up environment variables

_Note: There are many versions of Java you can download. To get proper compilation with Android, you must pick the correct version according to the project Gradle version, Android Gradle Plugin version, and Kotlin version. There are many intricacies, but you should **always** use an LTS version of Java, and **often times** the highest LTS version that has been released for at least 18 months._

If you already have Java installed, or can easily install it through the OS package manager (using APT, DNF, or RPM), you can skip this step. Make sure to point the `JAVA_HOME` variable to the **base** directory.

Get started by downloading your favorite Java distribution. There are many options to choose from. A most unique one is Eclipse OpenJ9 [[download here](https://developer.ibm.com/languages/java/semeru-runtimes/downloads)] because it has an alternative Java library and JVM implementation. I recommend avoiding the OpenJDK.org distribution (a.k.a. reference implementation) – because they do not release security updates past the .2 release; and Oracle JDK, due to the non-free license. Other options include:

* [Adoptium’s build of OpenJDK](https://www.adoptium.net/) - a compilation of OpenJDK by Eclipse foundation
* [Microsoft’s build of OpenJDK](https://learn.microsoft.com/en-us/java/openjdk/download) – a compilation of OpenJDK signed by Microsoft
* [Jetbrains Runtime](https://github.com/JetBrains/JetBrainsRuntime) – this is bundled with IntelliJ and Android Studio, but with patches focused on optimization for use within an Interactive Development Environment context – doing incremental builds, hot reload of class definition, and embedded Chromium in the runtime. I do not believe the added features are useful for building and archiving Android Applications for distribution, but it will work non-the-less.

If you have an archive file (.zip or .tar.xz), extract it with your favorite commandline program onto your preferred location. This can vary by your environment. For example:

`tar -xf ibm-semeru-open-jdk_x64_windows.zip -C C:\DeveloperTools\Java` or `tar -xJf ibm-semeru-open-jdk_x64_linux.tar.gz -C /opt/java17/`

Now set the `PATH` and `JAVA_HOME` environment variables. `PATH` should be appended with the path to the Java’s bin directory. `JAVA_HOME` should be the root (not bin) directory of the Java location. This depends on your OS. Please look that up if you do not know how to persist it. For Windows:

```
set PATH=%PATH%;C:\DeveloperTools\Java\bin
set JAVA_HOME=C:\DeveloperTools\Java
```

For Linux/macOS:

```
export PATH=$PATH;/opt/java17/bin
export JAVA_HOME=/opt/java17
```

## Download Android SDK Command Line Tools

Next, we will download Android SDK’s command line tools. Go to [this webpage](https://developer.android.com/studio#command-line-tools-only) and visit the “Command line tools only” section. 

The Android SDK command line tool is the tool that allows downloading and updating other Android SDK components such as the build tool and source code through the command line. When installing without Android Studio, the command line tools must become part of the SDK installation.

Android SDK uses a specific directory structure to recognize whether its components are installed. Within the Android SDK root, each component must reside in its specificly named subfolder. The command line tools content must reside in `<android sdk root>/cmdline-tools/latest`.

First, pick a preferred Android SDK location, such as `C:\DeveloperTools\Android\SDK`. Extract the tool into a your preferred location. Then, **move** and **rename** the command line tools folder into the expected subfolder.

```
mkdir C:\DeveloperTools\Android\SDK
mkdir C:\DeveloperTools\Android\SDK\cmdline-tools
tar -xf commandlinetools-win-11076708_latest.zip -C C:\DeveloperTools\Android\SDK\cmdline-tools
ren C:\DeveloperTools\Android\SDK\cmdline-tools\cmdline-tools C:\DeveloperTools\Android\SDK\cmdline-tools\latest
```

## Download Android SDK using Android SDK Command Line Tools and Set up Environment Variables

First, we will set up the environment variables. `ANDROID_HOME` should point to the **root** of the Android SDK directory. In my previous example, that would be:

`set ANDROID_HOME=C:\DeveloperTools\Android\SDK`

For Linux/macOS:

`export ANDROID_HOME=/opt/Android/sdk`

Next, we will invoke Android SDK command line tools to download the platform tools component to test our installation. Platform tools contains ADB which is needed for detecting connected Android devices. You can alternatively install Android build tools on a CI server where platform tools are not needed (replace with `build-tools;30.0.3`).

`
%ANDROID_HOME%\cmdline-tools\latest\bin\sdkmanager platform-tools
`

For Linux/macOS:

`
$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager platform-tools
`

This should install the platform tools, and if successful, then installation is sound.

## Download Flutter and set up environment variables

This is the last step, but a lot of people probably started with this step first before looking for a tutorial. Head to [Flutter SDK archive](https://docs.flutter.dev/release/archive) and download Flutter SDK for your OS. 

Extract the SDK to a preferred location, for example 

`tar -xf flutter.zip -C C:\DeveloperTools\flutter` or `tar -xf flutter.tar.xz -C /opt/flutter`

Now, set the `PATH` variable to the **bin** of flutter directory and `FLUTTER_ROOT` variable to the **root** of the flutter directory. For Windows:

```
set PATH=%PATH%;C:\DeveloperTools\flutter\bin
set FLUTTER_ROOT=C:\DeveloperTools\flutter
```

For Linux/macOS:

```
export PATH=$PATH;/opt/flutter/bin
export FLUTTER_ROOT=/opt/flutter
```

You’re not done here. You will need to execute `flutter` for the first time as a **non-admin** **non-root** user. You may need to restart your command line if it is using administrator mode. Now simply execute `flutter --doctor`. Flutter should detect Android SDK and Java using the environmental variables.

And we are done. Simply try compiling your project, or the example counter app.