Field of green wheat

Installing Android Studio on Pop!_OS (Or Ubuntu)

Android Studio provides easy downloads for Linux users, but installing that way loses the benefit of being managed by your package manager. I always try to find .deb or PPA methods of installing software (On Debian-derived distros) so that I can manage all of my basic software with one tool. I’ve found that manually installing binaries makes me forget to update them and forget to uninstall them after I no longer need them.

How to install

Fortunately, in the case of Android Studio, there’s a PPA for Ubuntu-derived Distributions. You can add it by running:

sudo add-apt-repository ppa:maarten-fonville/android-studio
sudo apt-get update

Please note that this allows your package manager to install software from Maarten Fonville’s personal package archive. If he were malicious (or his PPA was hacked), this creates a way for an attacker to install malware on your system. Please don’t install PPAs unless you’re familiar with their security implications.

This PPA is well-updated and maintained by a long-time Ubuntu Contributor, so I’m willing to install it.

Once you install the PPA, you can get android studio by running:

sudo apt install android-studio

Now, Android Studio will likely show up as an application within your desktop environment. If it doesn’t, try restarting. For me (in GNOME) it appeared instantly.

First-time Configuration Troubles

When running the setup for Android Studio, I had a problem downloading the configuration. I reached a page in the setup wizard called “Downloading Components”, and it never seemed to make any progress on the progress bar. When I clicked the “Show Details” button, I saw the same URL over and over:

https://dl.google.com/android/repository/android_m2repository_r47.zip

This is what it looked like:
android-downloading-components-error

Following this link in my browser initiates a download, so I’m not sure why Android Studio is apparently choking on it. Once the download was complete, I wasn’t sure what to do with it. Fortunately, this StackOverflow answer gave me a way forward.

I extracted the contents of that zip file to ~/Android/Sdk/extras/android/ and restarted Android Studio. This time, it found that component and didn’t try to download it again. It again tried to download something over and over, so I assume that there’s something misconfigured to prevent it from performing these downloads properly.

Since Android Studio might log the problem to stderr, I decided to invoke it from the command line. To look up the name of the script that launches it, I looked at the desktop file for the Android Studio Application:

$ cat /usr/share/applications/android-studio.desktop 
[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Name=Android Studio
Exec=/opt/android-studio/bin/studio.sh
Comment=Integrated Android developer tools for development and debugging.
Icon=androidstudio
Categories=GNOME;GTK;Development;IDE;

Alas, the shell output wasn’t any more help:

$ /opt/android-studio/bin/studio.sh
Preparing "Install Android SDK Tools (revision: 26.1.1)".
Downloading https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip

Or so I thought… I let it run for a while, and suddenly more things started happening. Apparently, the “show details” view just prints the URL of the file that’s currently being downloaded over and over again. Doesn’t make much sense to me. It took a long time for the progress bar to move at all. I guess this is mostly just a UX problem. I didn’t get any feedback indicating that the download was working, so I assumed a problem that didn’t exist.

Emulator Woes

Once everything installed, I was able to open a project without difficulty. In the past (on an Ubuntu machine), I had to monkey with some files to make the emulator work properly, so I was anxious to see whether that would be necessary again.

Alas, once I downloaded and configured a virtual device, it would crash immediately when I tried to run it (process exited with status 1). To attempt to find the problem, I needed to run the emulator from the shell.

The path for my android emulator is $HOME/Android/Sdk/tools/emulator. This Android Documentation told me where to look. Once I found the executable, I still needed to figure out how to launch the same virtual device that was crashing so that I could see the error message. The emulator supports a -list-advs flag that tells you the names of available Android Virtual Devices. Running that yielded:

$ ./Android/Sdk/tools/emulator -list-avds                                 
Nexus_5_API_24  

I could then run the following to attempt to launch the virtual device:

$ ./Android/Sdk/tools/emulator -avd Nexus_5_API_24 -netdelay none -netspeed full
libGL error: unable to load driver: i965_dri.so
libGL error: driver pointer missing
libGL error: failed to load driver: i965
libGL error: unable to load driver: swrast_dri.so
libGL error: failed to load driver: swrast
X Error of failed request:  BadValue (integer parameter out of range for operation)
  Major opcode of failed request:  152 (GLX)
  Minor opcode of failed request:  24 (X_GLXCreateNewContext)
  Value in failed request:  0x0
  Serial number of failed request:  31
  Current serial number in output stream:  32

Google-ing this issue pulls up this Arch Linux Forum Thread in which user lordnaikon explains:

Ok looks like in the recent android studio update, google used a libstdc++ incompatible with the intel driver installed on the system. […] so basically i renamed the “android-studio/sdk/tools/lib64/libstdc++/libstdc++.so.6” file so it has to use the system ones … that basically does the trick – for me.

So we need to rename that shared object so that the dynamic linker uses our system-wide C++ standard library instead of the one that ships with the emulator. I recursively searched the android installation for the C++ standard library. The double-star glob pattern (**) that I use works by default in fish (my current shell), but may not work in bash or zsh by default.

$ ls ~/Android/Sdk/**/libstdc++*
/home/chris/Android/Sdk/emulator/lib64/libstdc++/libstdc++.so.6*
/home/chris/Android/Sdk/emulator/lib64/libstdc++/libstdc++.so.6.0.18*
/home/chris/Android/Sdk/emulator/lib/libstdc++/libstdc++.so.6*
/home/chris/Android/Sdk/emulator/lib/libstdc++/libstdc++.so.6.0.18*

/home/chris/Android/Sdk/emulator/lib64/libstdc++:
libstdc++.so.6*  libstdc++.so.6.0.18*

/home/chris/Android/Sdk/emulator/lib/libstdc++:
libstdc++.so.6*  libstdc++.so.6.0.18*

To fix the problem, I only needed to rename libstdc++.so.6, not the one with the longer version number. I renamed it like so:

$ mv /home/chris/Android/Sdk/emulator/lib64/libstdc++/libstdc++.so.6{,-backup}

Now starting the emulator works fine. I imagine that I’ll need to repeat this trick when I update Android Studio or the emulator, but I can live with that.

We’re Done

With those fixes in place, Android Studio runs perfectly. Hopefully nobody else has as many issues installing it as I did.

Image Credit: Ram Yoga
Image License: CC BY-NC 2.0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s