How to compile APK from command line?

I am interested in making Android apps on demand. Depending on the clients request, my web site would send me a JSON file direct to a Windows application that I have created in Delphi. This one would save the file inside the Android app source folder and then, execute a command line telling the Android compiler to generate the APK file and send it to my client, all that without my presence.

The Android project was made with MotoDev. And it uses the Android SDK that is in my root.

How should I configure the command line to achieve this from inside my Delphi program?

I will also need to change the manifest to put a new version number so it does not conflict with other clients version.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Android uses the Ant build system, so you can create a build.xml and file for your project.

You’ll need to create the build.xml file first though:

android update project -p .

This will generate a build.xml file. You should probably customize the build steps and targets for your project. A good idea in your case would be to have the file generated by your website for the specific build… Then include it via the build.xml file. In particular, you will need to specify in the file where the signing keys are, and what the password is:


The build process using ant also allows you to do variable replacements in Java files, which might be another idea. It would allow you to customize the build process further on a client by client basis.

By default, the build is triggered by:

ant clean
ant release

Another neat idea: Have Ant copy the resulting APK file to a network share accessible by the website by placing a < copy ... /> line in the < target name="release" > section.

Solution 2

Create build.xml at project creation time

If you start a new project with:

android create project \
    --target 1 \
    --name MyName \
    --path . \
    --activity MyActivity \
    --package com.yourdomain.yourproject

the build.xml file used by ant will be generated.

The android tool is present in the tools/ directory of the SDK which you downloaded.

Create debug releases


ant release

for final releases, you can also create debug releases with:

ant debug

Location of generated apk

Generated apk are placed under bin/.

The most important outputs are:


but some intermediate apks are also generated, in particular unaligned and unsigned versions.

But most of the time you can forget where they were created and just run:

ant debug install
ant release install

to get them installed. But make sure it is working with adb first: adb devices command not working

Tested on Ubuntu 15.10, Android 23.

Solution 3

Android doesn’t directly use ANT build systems now. It uses Gradle, which is based on Groovy. You can learn more about build systems here.

You can see the list of available tasks you can run on using this command

gradlew tasks

To initiate a debug build, invoke the assembleDebug task

gradlew assembleDebug

You can install your app using the adb tool via this command

adb install path/to/your_app.apk

To learn more about building on command line, follow this comprehensive article.

Also you can read this article on “Why Build Your Java Projects with Gradle Rather than Ant or Maven?

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply