If you don’t want to install a number of programs for building your Android project, this article is for you. You will need only JDK, the Android SDK platform tools and minimum Android platform in order to build apk from command line.
In this article, I will describe how to build Android app from command line, i.e. how to build the .apk file using Android tools and command line. As a result, you can use the batch file for building your Android application. I use Windows commands in this article. But you can use the same Android commands and tools and the same program if you are working on Linux OS. The example of the script for Linux OS is included in the sample project, attached to this article.
Before we build Android .apk from command line, you must install Java JDK and Android SDK on the system where you will build your application. The private keystore also must be present.
2.1 Java JDK
You can use Java JDK from Eclipse or download it from this link: http://www.oracle.com/technetwork/java/javase/downloads/index.html and then install it.
2.2 Android SDK
Android SDK and its HOWTO document are available here: http://developer.android.com/sdk/index.html.
The Android SDK Platform-tools, SDK Platform Android 2.2, and API 8 are the minimum that is needed for the test project work.
2.3 Environment Variables
If you want to have a readable batch script and a program without very long file path definitions, you should use the environment variables. I recommend you to define the following ones:
JAVABINpath to the Java JDK bin folder. For example: C:\Program Files\Java\jdk\bin. This folder must contain the javac.exe and jarsigner.exe files.
ANDROID-SDKpath to the Android SDK folder. For example: C:\Program Files\Andoroid\android-sdk-windows.
2.4 Private Key for Signing
You can read everything about signing, creating private key, and other operations here: http://developer.android.com/guide/publishing/app-signing.html
In this article, I describe only one important command that will generate my-release-key.keystore:
Specify your info and you will receive the my-release-key.keystore file. You must remember the password. You will need it later. Also, I recommend you to put the keystore file in the same folder as the project. In the example,It is in the keystore folder in the project directory.
2.5 Example Files Structure
You must remember that the dx Android tool requires the full path, which MUST not contain any spaces. So, check the fact, that your project path satisfies the requirement.
The file structure of the example project, which is built using the build.bat file, is the following:
Let's see how to create an apk file. The following commands are for the Windows OS, but you can create apk from command line for the Linux OS by using the script in the attached sample.
First of all, we must save current path. Then we must change the CD variable to the path to the build.bat file:
Then, bin and gen old folders should be recreated:
I add some definitions. They make the batch file readable and easy to update and thus simplify Android apk development. So, I recommend you the following definitions:
- minimum Android revision;
- the path to aapt Android tool and its arguments for adding files into the existing archive;
- the path to aapt Android tool and its arguments for packing and generating resources;
- the path to dx Android tool;
- the path to javac utility of JDK.
This list of independent definitions can be used for building the majority of Android projects (of course, with the changed Android revision value). For example:
Also I need the defined variables for my project:
APP_NAMEis the name of application that will be used for the output APK file.
JAVACare the same commands, but there are path to the sources, generated R class, and output folder in
These variables let us change the project name and paths to the sources easier.
And now, all preparations are finished and the application can be built. The R file will be generated using aapt tool. All resources will be packed into the resources.ap_ file:
Android manifest file, res and assets folder are the input data. Aapt will generate the R class and put it into the gen folder. Then, aapt will pack the resource files into the resourses.ap_ file.
Every folder that contains *.java file must be called with javac. In my example, there is only one folder with javac. So, I have only one command line for building sources:
As you remember, the
JAVAC_BUILD command has already contained the arguments that specify the bin folder. The bin folder is an output folder for the compiled sources.
At this moment, the sources have been compiled successfully and they can be packed in the special dex file:
For the application signing, the resources file must be copied to another file, which will be used for the signing:
The classes.dex file must be added to the new file that has an ap_ extension
Now, the ap_ file is a correct apk file. But it is not signed yet and it cannot be installed to the Android device.
The creation of the signed Android application from the *.ap_ file is the following (Output file name must differ from the input file name – in this case the file extension is changed to the *.apk):
And delete temp ap_ file:
Finally, let’s return to the start folder and clear local variables:
To test the example and generate apk from command line yourself, you must:
- unpack it
- do the steps 2.1-2.5 from the Preparations paragraph
- run the build.bat file
Android Developer reference: http://developer.android.com
Download Sample Project Sources (ZIP, 19 KB)