When I was asked if I could write an application of Android, one of the first things I did was taking a look at the SDK download page, where I was disappointed: There are binary SDKs for Linux i386, Mac OS and Windows, but the most important thing was missing: A source download. It took me a rather long time to find what I was looking for: “How to build an Android SDK.” I wonder why this document seems to exist in the git tree only and isn’t mentioned anywhere on the Android developer website.
The first thing to do is get the Android source code. There are some strange requirements listed:
- A 32-bit system or a 32-bit compatible environment on a 64-bit system
- JDK 5.0, update 12 or higher, but not Java 6
The first requirement is annoying because I usually use pure 64-bit systems. However, my development system supports KVM, so I decided to set up a dedicated 32-bit virtual Xubuntu. The “Java 5 only” is really weird – in a worst case scenario javac’s
-source option should still make it possible to compile old code. I chose to just ignore that and use Ubuntu’s
openjdk-6-jdk. The process to actually download everything and start the build is described in the documentation, so I won’t repeat that here.
After a while, the build failed with an error about
gnu/stubs-64.h not found from gcc and an incompatible libstdc++ error from the linker. I guess that the second one was caused by the first, but that one surprised me quite a bit.
gnu/stubs-64.h is included from
/usr/include/gnu/stubs.h, with an
#if construct that chooses
stubs-32.h depending on the
__WORDSIZE macro. As recommended by the official docs I’m using a 32-bit system for the build. So why is the build system looking for a header used on 64-bit systems?
stubs-32.h is where it should be. I tried
CFLAGS="-m32" make -j4
to force a 32-bit build, but it didn’t help. I searched and found the forum thread linked above, and from there the cause of the problem. For reasons unknown to me, someone thought it would be a good idea to force a 64-bit build if Java 6 is in use, even on a 32-bit system. It is not!
Well, the next step was simple: Edit the files in question and remove the “
-m64.” The files:
The code to remove (or uncomment):
# This forces a 64-bit build for Java6 ifneq ($(filter 1.6%,$(java_version)),) LOCAL_CFLAGS += -m64 LOCAL_LDFLAGS += -m64 endif
After that, “
make -j4 sdk” worked (modify the number of jobs as appropriate for your system). After a long time, I had my own SDK in
out/host/linux-x86/sdk/. The finished SDK is a zip file, after unpacking it took 456MB – not much compared to the 9.3GB in the build directory (sources, temporary files, …). Of course, I immediately tried the emulator:
As you can see, it worked. 🙂 And the official documentation is clearly wrong: You do not need Java 5 to build Android. 😛
The toolchain problem
Sounds good? Well, there’s a problem: While my new SDK is locally built, not everything used to build it is, and I’m not talking about distro packages here. When you download the Android source, you’ll also get a directory called “
prebuilt.” This contains a bunch of binaries, for example the ARM toolchain used for cross-compiling. So I’ll have to look into replacing that as well to get a really independent SDK build…