Installing Android Studio and Emulator on Google Cloud Ubuntu 20.04.1 LTS (Focal Fossa).
If you have ever built an android project in Android Studio you have no doubt been delighted while you wait minutes for the process to complete. Likewise the emulator takes time to start up. There is the option to pay for online emulator services and you can also do it yourself on a remote box on the Google Cloud Platform.
There is a need to do an extra step with the remote Ubuntu machine to get the emulators running in containers within the containers.
To follow this example you will need to have a Google Cloud account and the Google Cloud SDK on your local machines. If you don’t have these already you can follow the instruction on these links.
There is a tutorial for older versions Ubuntu but I like to work with the latest. If you are good with Ubuntu 18 or less you can follow the tutorial at. https://cloud.google.com/community/tutorials/setting-up-an-android-development-environment-on-compute-engine
The windows manager for the older versions lxde won’t work on Ubuntu 20. We will be using a newer version xfce4. Likewise there is a simpler way to install the Studio on Ubuntu 20.
The first thing we need to do is hookup a compute engine machine instance for our install to run on. We do this by running the following three commands in the command shell. You will probably want to remove carriage return characters in note pad when you paste these or you might get errors.
gcloud compute disks create disk1 — image-project ubuntu-os-cloud — image-family ubuntu-2004-lts — size 100 — zone us-west1-b
gcloud compute images create ubuntu20 — source-disk disk1 — source-disk-zone us-west1-b — licenses “https://compute.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx"
gcloud compute instances create myandroidstudio — zone us-west1-b — min-cpu-platform “Intel Haswell” — image ubuntu20 — custom-cpu=4 — custom-memory=25
If you already know how GCP works you will understand that we created a disk and then an image and finally our machine instance.
If this is new to you it would be a good idea to read up and learn about your GCP account . I recommend Qwiklabs tutorials.
Qwiklabs - Hands-On Cloud Training
We give you temporary credentials to Google Cloud Platform, so you can learn the cloud using the real thing - no…
Next we need to add in the VNC and a windows manager for our instance. In the Google Cloud Console view click on the SSH icon for the instance.
This will bring up a shell window in a new browser window .
Type in sudo apt-get update and enter to update this machine.
Next we need to install a windows manager and a vnc server.
sudo apt install xfce4 xfce4-goodies
When asked if you want to continue type y.
A dialogue will open asking you to choose your display manager. Go with the default gdm3 that is already selected. Just hit enter. Get a cup of tea while the install does its thing.
Now hookup the vnc server.
sudo apt install tightvncserver
Add a password with
Type n for no and enter.
Now we need to edit and change the permissions on the startup script.
The nano editor will open.
Copy and paste in.
Then save with CONTROL x and then y.
Now change the permissions on the file.
chmod +x ~/.vnc/xstartup
I found that in the next step I had to stop and restart the VM instance for this to work properly ! So do that first and then launch a new ssh shell.
Finally the last linux command is.
If you don’t have Real VNC viewer download it here.
Copy over the ip for your vm and add in port 5901 in vnc to connect. You will need to have the ports open in the google cloud project. This is explained at the end if you need it.
You will see the desktop for the ubuntu 20 machine in your VNC window.
Now we just need to install Android Studio. Easy as snap with this command in the ssh window.
sudo snap install android-studio --classic
Start the studio here.
Go through the install selecting all the defaults.
There are two more commands required to avoid having permission problems when you launch the emulator.
sudo apt install qemu-kvm
sudo adduser $USER kvm
In the Android Studio I installed the Sunflower Sample.
Running this you will get recommendations and an error. I went ahead and did the updates. One for heap size another for Gradle.
The command line sdk needs to be added. Do so from the Tools > Sdk manager menu.
You would think that all of this is enough but it is necessary to restart the server again to enable the kvm user permissions we set a few steps back.
Remember to run vncserver in the ssh shell to start the vnc server !
Now we are able to run everything fine.
You can even copy over the apk to the google drive and install it from there on your phone.
Here is a screen shot from my phone.
The remote VNC screen works without to much latency if you run it in full window mode.
As a todo add the remote ssh plugin to VSCODE and see if you can edit your code from you local code editor.
Adding a firewall rule. Navigate in the Google Console to.
From there do these things as described in https://cloud.google.com/community/tutorials/setting-up-an-android-development-environment-on-compute-engine
I enabled port 5901 as well to let the traffic in.
Set up a firewall rule to allow RDP in your instance
- Navigate to VPC networks > Firewall.
- Click Create a firewall rule.
- Choose a name and target tag for the firewall rule, and set allowed protocols and ports to
- Save the firewall rule.
- Navigate to VM instances > Edit VM > Networking.