Writing Your First Android App, in Assembly

Have You Ever Stopped to Wonder How The Internals of Your Phone Work?

In this post I am going to share with you a different perspective about getting started with writing code for Android. The standard approach is installing Android Studio and building a simple “Hello World” app in Java or Kotlin. But it can also be done differently, as you will shortly see. But first, a little background story —

How Does my Android Phone Work?

One evening my life partner Ariella Eliassaf asked me: How does my smartphone work? What’s inside it? How does it electricity, ones and zeroes, make it all work?

Hello, Android!

At this point, I felt she had enough background to explore how the CPU of her smartphone worked. She had a Galaxy S6 Edge device, which is based on the ARM architecture (like the majority of the smartphones do). It was time to write “Hello, World”, her first Android app ever, but in assembly:

#define message "Hello, World\n"
write(1, message, sizeof(message));

Building Your Program

In order to compile your assembly program, you will need the Android NDK, the Native Development Kit, which contains a set of compilers and build tools for the ARM platform. You can get download it directly from the official site, or install it through Android Studio:

Go to “SDK Tools” and check “NDK”, then click “OK”. Also note the Android SDK Location
ndk-bundle\toolchains\arm-linux-androideabi-4.9\prebuilt\windows-x86_64\bin
arm-linux-androideabi-as -o hello.o hello.s
arm-linux-androideabi-ld -o hello hello.o

Running The Program on Your Device

Android programs are usually distributed in APK format. This is a special kind of ZIP file, which Android expect to be constructed in a specific way, and should include Java classes (you can write parts of your app using native C/C++ code, but the entry point still needs to be Java).

adb push hello /data/local/tmp/hello
adb shell chmod +x /data/local/tmp/hello
adb shell /data/local/tmp/hello
Hooray, it works!

What Will You Build?

At this point you should have a working development environment, similar to what Ariella had. She spent a few days learning ARM assembly and came up with a simple project she wanted to code: a simple Seven-Boom game. Seven-boom is an Israeli variation of Fizz Buzz. Players take turns counting incrementally, and whenever a number is divisible by seven or contains the digit 7, they need to say “Boom”.

Google Developer Expert for Web Technologies, Maker and Public Speaker