Never Enough Spacetime


Tales of an astronomer trapped in a programmer's body.


Android Studio Error on 64-bit Ubuntu

I'm currently working on an Android app to replace the need for the $200 Celestron SkySync GPS Accessory. This app, coupled with StellarSocks, will allow us to update our GPS location and time on a NexStar GoTo telescope remotely. My development environment is Ubuntu 14.04 LTS Server, which only comes in 64 bit. Of course, Android Studio is 32-bit. As you can imagine I ran into some problems while trying to build my project.

Android Studio is 32-bit

If you're encountering problems building your project in Android Studio or with gradle, and you're running a 64-bit version of Ubuntu, chances are you're missing 32 bit libraries. I saw the following error while trying to build:

$ ./gradlew build
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeDebugResources'.
>   /path/to/application/app/build/intermediates/exploded-aar/com.android.support/appcompat-v7/19.1.0/res/drawable-hdpi/abc_tab_selected_focused_holo.9.png: Error: Cannot run program "/usr/share/android-studio/data/sdk/build-tools/android-4.4W/aapt": error=2, No such file or directory

The next logical step is to see if that file exists. If you list the files in the directory, you can see the file is actually there and that you have permissions to it.

$ ls -l /usr/share/android-studio/data/sdk/build-tools/android-4.4W/aapt
-rwxrwx--- 1 user user 1123893 Jun 24 00:17 /usr/share/android-studio/data/sdk/build-tools/android-4.4W/aapt

Now we'll check to see what kind of file this is:

$ file /usr/share/android-studio/data/sdk/build-tools/android-4.4W/aapt
/usr/share/android-studio/data/sdk/build-tools/android-4.4W/aapt: ELF 32-bit LSB  shared object, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.8, not stripped

Ahh, now we see the problem. This is an ELF (Executable and Linkable Format) 32-bit object file. Ubuntu 64-bit distributions don't ship with 32-bit libraries.

The Fix - Adding 32-bit Libraries

Luckily, MultiArch has entered with Oneiric Ocelot. We need to enable the 32-bit architecture, and install some 32-bit libraries. We'll need to add the i386 architecture to the Debian package manager, then install some packages with Aptitude. To force Aptitude to install an i386 package, add :i386 as a suffix to the package name. The first three libraries are for Android Studio, and the libsdl library is for the Android emulator.

$ sudo dpkg --add-architecture i386
$ sudo apt-get update
$ sudo apt-get install libc6:i386 libstdc++6:i386 lib32z1 libsdl1.2debian:i386

Now let's try to build again, as we should have all of the necessary 32-bit libraries now.

$ ./gradlew build
...
BUILD SUCCESSFUL
Total time: 35.818 secs

That's it. Happy mobile developing!