Building an Android App from the Command Line

Wed, 04 Jan 2017 00:00:00 GMT

Update 2024-03-01: Updated the article to use newer SDK, NDK, etc. and target Android API level 34.

I have been learning a bit of Android programming recently (late to the party, I know). One thing that frustrated me was figuring out how to actually build an Android application.

Things are simple enough with C. As explained in the first section of K&R, one might write this as a first program:

#include <stdio.h>

int main()
{
        printf("Hello, world!\n");
        return 0;
}

and turn it into an executable by simply invoking the system compiler as below.

$ cc hello.c
$ ./a.out
Hello, world!

This diagram illustrates the full process:

For Android, however, the official way to write Hello World is to fire up Android Studio, use its wizard to create a new project, and the application will then be generated and built automagically in a few minutes.

This is of course intended as a convenience for the developer, but for someone who wants to know what's going on, it makes things difficult instead. What actually happened? Which of all these files are really necessary for Hello World?

Others have expressed similar concerns:

(For me, it generated a 50 MB directory containing 1,348 files spread across 630 subdirectories.)

Perhaps it is the control freak in me speaking (a good trait for programmers about their programs), but I simply don't feel comfortable not understanding how to build my own program.

Below are my notes on how to build an Android application by hand from the command line. The instructions are for Linux, but they should be easy to adapt to Mac or Windows. The full source code and a build script is available in command_line_android.tar.gz.