Install the Android SDK
4 minute readFlutter uses the Android SDK and related tools, so we'll get that installed next.
Getting the Android SDK set up manually can be tricky, I think the expectation is that most people will use Android Studio so not a lot of work has been put in to making manual installation user-friendly.
Pick a directory where you’ll keep the SDK
This is an important directory that will always store all the Android-SDK specific tools, platforms, build tools etc. Various bits and pieces need to know where these things are, so we’ll set some environment variables for them later.
I’m using $HOME/android_sdk/
in these examples.
mkdir $HOME/android_sdk
Install the SDK Manager
This is a set of command line tools used to install different versions of the SDK (for when you want to target different Android devices).
Based on Google’s instructions at https://developer.android.com/tools/sdkmanager.
- From the downloads page (I had to scroll down a long way), download the
cmdline-tools
zip and unzip it to the directory you created above eg$HOME/android_sdk/
- Inside the unzipped directory
cmdline-tools
make a ’latest’ directory and move all of the other contents of ‘cmdline-tools’ inside it (so you end up withcmdline-tools/latest/bin
You should end up with something like the following (I’m using tree
to list the directory contents but don’t worry about that, just make sure your directories are in the right places):
$ tree -L 2 cmdline-tools/
cmdline-tools/
└── latest
├── NOTICE.txt
├── bin
├── lib
└── source.properties
Add some environment variables to ~/.profile
Make sure these make sense based on what we’ve done so far if you’ve used different directories to me.
# ~/.profile
export ANDROID_SDK_ROOT=$HOME/android_sdk
export PATH=$PATH:$ANDROID_SDK_ROOT/cmdline-tools/latest/bin
export PATH=$PATH:$ANDROID_SDK_ROOT/platform-tools
Source the profile with source ~/.profile
. Then run sdkmanager
and see if it does something. If you get command not found
double-check everything and try rebooting.
My sdkmanager binary name clash (this won’t affect you but click to expand if you’re interested)
In my case I already had a binary installed with the name sdkmanager
, as that’s the name Garmin use for their ConnectIQ SDK Manager. Apparently neither Garmin nor Google could foresee that anyone other than themselves might call their SDK manager software ‘sdkmanager’.
So I had to do some hacky stuff with bash aliases in ~/.profile
to let the two live alongside each other, and remind me of the situation:
alias sdkmanager='echo "Use either *sdkmanager-garmin* or *sdkmanager-android*"'
alias sdkmanager-android=$ANDROID_SDK_ROOT/cmdline-tools/latest/bin/sdkmanager
alias sdkmanager-garmin=$HOME/.local/bin/sdkmanager
Accept SDK licences
The SDK licences must be accepted for everything to work.
sdkmanager --licenses
Then type Y and press Enter for each prompt
Install the Android toolchain
To pass the ‘Android toolchain’ step of flutter doctor
(in a bit) we need 3 things:
- Platform tools (generic)
- A valid platform
- Build tools to match the platform
Various instructions online will tell you to do things like sdkmanager "platform-tools" "platforms;android-33"
(ie pass multiple arguments to sdkmanager
), but as observed in this StackOverflow answer if using the command line tools, the platform tools need to be installed via separate commands ie:
sdkmanager 'platform-tools'
sdkmanager 'platforms;android-33'
sdkmanager 'build-tools;33.0.2'
If the installation has worked correctly you should end up with something like:
$ tree -L 1 android_sdk/
android_sdk/
├── build-tools
├── cmdline-tools
├── licenses
├── platform-tools
└── platforms
Another weird, me-specific problem I encountered…
At first, my installations weren’t appearing in my android_sdk
directory and Flutter tools were complaining about not being able to find the Android SDK.
Forcing the SDK path like this worked:
sdkmanager --verbose "platform-tools" --sdk_root=$HOME/android_sdk/
But something smelt wrong, as the original sdkmanager
install commands seemed to be completing without error. I investigated this by running the following:
sdkmanager 'platform-tools' --verbose
Which revealed that they were going into:
/data/data/com.termux/files/usr/share/android-sdk/
This is because I was on Android using Termux, in a proot container, and I had previously installed the SDK in the main (non proot) Termux environment. This was not needed any more so I deleted the other installation.
This is an exotic situation and you’re probably not as stupid as I am to get into this situation, but maybe this note will help in some other situation.