Configure Flutter development environment on Manjaro/Arch linux.
Tags: flutter, archlinux
Published: 2021-04-13 09:53:36


Linux is an amazing OS for Flutter development, but setting up Java, Android and the Android tool-chain can be really difficult.In this blog post I'll show you how to get Flutter working with Android SDK without installing Android Studio.
I'll be using Neovim for writing code , you can use vscode or any other text editor.

## Let's start setting up Flutter development environment


### Installing Yay package manager

We are going to install almost all of packages from Arch User Repository. For that you need to install `yay` package manager
Open your terminal and run these commands.

  ```bash
      pacman -S --needed git base-devel
      git clone https://aur.archlinux.org/yay.git
      cd yay
      makepkg -si
   ```

### Installing Flutter
   
 ```bash
yay -S flutter
```

Make sure that you have openjdk 8 or 10 by this command

`java -version`

if java version is other than 8 or 10 then install openjdk 8 using 

`sudo pacman -S jre8-openjdk`

And put these lines in your .bashrc or .zshrc
```bash
export JAVA_HOME='/usr/lib/jvm/java-8-openjdk'
export PATH=$JAVA_HOME/bin:$PATH 
```

### Set permissions

Yay installs `Flutter` in `/opt/flutter` directory.Which can only be accessed by Root user, so we need to set appropriate permissions. Run these commands in your terminal.

```bash
  sudo groupadd flutterusers
  sudo gpasswd -a $USER flutterusers
  sudo chown -R :flutterusers /opt/flutter
  sudo chmod -R g+w /opt/flutter/
```
if you get some weird permission denied errors try this

`sudo chown -R $USER /opt/flutter`

### Android SDK and tools

To install Android SDK and other required tools run these commands in your terminal
```bash 
yay -S android-sdk android-sdk-platform-tools android-sdk-build-tools
yay -S android-platform
```

## User permissions
android-sdk is installed in `/opt/android-sdk` directory, So we have to set appropriate permissions

```bash
sudo groupadd android-sdk
sudo gpasswd -a $USER android-sdk
sudo setfacl -R -m g:android-sdk:rwx /opt/android-sdk
sudo setfacl -d -m g:android-sdk:rwX /opt/android-sdk
```
### Android emulator
`sdkmanager --list`
this command will show you the list of available android system images. Install an android image of your choice. For example.

```bash
sdkmanager --install "system-images;android-29;default;x86"
```
Then create an android emulator 

```bash
avdmanager create avd -n <name> -k "system-images;android-29;default;x86"
```

 
Put these lines in your .bashrc/.zshrc

```bash
export ANDROID_SDK_ROOT='/opt/android-sdk'
export PATH=$PATH:$ANDROID_SDK_ROOT/platform-tools/
export PATH=$PATH:$ANDROID_SDK_ROOT/tools/bin/
export PATH=$PATH:$ANDROID_ROOT/emulator
export PATH=$PATH:$ANDROID_SDK_ROOT/tools/
```

Accept all of licences by this command
`flutter doctor --android-licenses`

and run
`flutter doctor`

Everything should work now except the android studio.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9rqkhhzi8hzjh5t15iir.png)

if your licences are not accepted even after running `flutter doctor --android-licences` try these commands and then run `flutter doctor --android-licences` again.

```bash
sudo chown -R $(whoami) $ANDROID_SDK_ROOT
```
if licences are still not accepted (happened to me) then try this.

```sudo flutter doctor --android-licenses```

### Create and run new Flutter app
```bash
flutter create new_app
cd new_app
flutter run --debug
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0hrrw7kjowr71f13malj.png)
To run your app on a your mobile phone you need to enable USB debugging. and connect your device to your laptop using a USB.