How to build .apk file from command line

Created on Wednesday, 29 June 2011 14:32

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 for building the project using the batch file.

  • Introduction
  • Preparation
    • Java JDK
    • Android SDK
    • Environment Variables
    • Private Key for Signing
    • Example Files Structure
  • Command Sequence
  • Resources

1. Introduction

In this article, I will describe how to use the Android tools for building the .apk file using the 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.

2. Preparation

You must install Java JDK and Android SDK on the system where you will build your Android application using the Windows command line. 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.

Do you like our specialists' work? Make them work for you!

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:

  • JAVABIN path 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-SDK path 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:

%JAVABIN%\keytool  -genkey -v -keystore  my-release-key.keystore -alias alias_name  -keyalg RSA -keysize 2048  -validity 10000

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:

SecureMessages/
assets/
keystore/
my-release-key.keystore
res/
drawable-hdpi/
icon.png
drawable-ldpi/
icon.png
drawable-mdpi/
icon.png
layout/
main.xml
values/
strings.xml
src/
org/
secure/
sms/
SecureMessagesActivity.java
StringCryptor.java
SmsReceiver.java
AndroidManifest.xml
build.bat // Windows build script
build.sh // Linux build script

3. Command Sequence

The following commands are for the Windows OS, but you can find the script for the Linux OS project building 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:

SET PREV_PATH=%CD%
cd /d %0\..

Then, bin and gen old folders should be recreated:

rmdir "bin" /S /Q
rmdir "gen" /S /Q
mkdir "bin"
mkdir "gen"

I add some definitions. They make the batch file readable and easy to update. 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:

SET ANDROID_REV=android-8
SET ANDROID_AAPT_ADD="%ANDROID-SDK%\platforms\%ANDROID_REV%\tools\aapt.exe" add
SET ANDROID_AAPT_PACK="%ANDROID-SDK%\platforms\%ANDROID_REV%\tools\aapt.exe" package -v -f -I "%ANDROID-SDK%\platforms\%ANDROID_REV%\android.jar"
SET ANDROID_DX="%ANDROID-SDK%\platform-tools\dx.bat" --dex
SET JAVAC="%JAVABIN%\javac.exe" -classpath "%ANDROID-SDK%\platforms\%ANDROID_REV%\android.jar"

Also I need the defined variables for my project:

  • APP_NAME is the name of application that will be used for the output APK file.
  • JAVAC_BUILD and JAVAC are the same commands, but there are path to the sources, generated R class, and output folder in JAVAC_BUILD.

These variables let us change the project name and paths to the sources easier.

SET APP_NAME=SecureSms
SET JAVAC_BUILD=%JAVAC% -sourcepath "src;gen" -d "bin"

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:

call %ANDROID_AAPT_PACK% -M "AndroidManifest.xml" -A "assets" -S "res" -m -J "gen" -F "bin\resources.ap_"

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:

call %JAVAC_BUILD% src\org\secure\sms\*.java

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:

call %ANDROID_DX% --output="%CD%\bin\classes.dex" %CD%\bin

For the application signing, the resources file must be copied to another file, which will be used for the signing:

copy "%CD%\bin\resources.ap_" "%CD%\bin\%APP_NAME%.ap_"

The classes.dex file must be added to the new file that has an ap_ extension

call %ANDROID_AAPT_ADD% "%CD%\bin\%APP_NAME%.ap_" "%CD%\bin\classes.dex"

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):

call "%JAVABIN%\jarsigner" -keystore "%CD%\keystore\my-release-key.keystore" -storepass "password" -keypass "password" -signedjar "%CD%\bin\%APP_NAME%.apk" "%CD%\bin\%APP_NAME%.ap_" "alias_name"

And delete temp ap_ file:

del "bin\%APP_NAME%.ap_"

Finally, let’s return to the start folder and clear local variables:

:EXIT
cd "%PREV_PATH%"
ENDLOCAL
exit /b %ERRORLEVEL%

To test the example, you must:

  • unpack it
  • do the steps 2.1-2.5 from the Preparations paragraph
  • run the build.bat file

4. Resources

Android Developer reference: http://developer.android.com

Download Sample Project Sources (ZIP, 19 KB)

Do you like our specialists' work? Make them work for you!

How to build .apk file from command line(转)的更多相关文章

  1. IAR Build from the command line 环境变量设置

    http://supp.iar.com/Support/?Note=47884 Technical Note 47884 Build from the command line The alterna ...

  2. Building Xcode iOS projects and creating *.ipa file from the command line

    For our development process of iOS applications, we are using Jenkins set up on the Mac Mini Server, ...

  3. How to output the target message in dotnet build command line

    How can I output my target message when I using dotnet build in command line. I use command line to ...

  4. Yandex Big Data Essentials Week1 Unix Command Line Interface File Content exploration

    cat displays the contents of a file at the command line copies or apppend text file into a document ...

  5. how to import a SQL file in MySQL command line

    how to import a SQL file in MySQL command line execute .sql file, macOS $mysql> source \home\user ...

  6. How to Use Android ADB Command Line Tool

    Android Debug Bridge (adb) is a tool that lets you manage the state of an emulator instance or Andro ...

  7. [笔记]The Linux command line

    Notes on The Linux Command Line (by W. E. Shotts Jr.) edited by Gopher 感觉博客园是不是搞了什么CSS在里头--在博客园显示效果挺 ...

  8. An annotation based command line parser

    Java命令行选项解析之Commons-CLI & Args4J & JCommander http://rensanning.iteye.com/blog/2161201 JComm ...

  9. Creating Node.js Command Line Utilities to Improve Your Workflow

    转自:https://developer.telerik.com/featured/creating-node-js-command-line-utilities-improve-workflow/ ...

随机推荐

  1. 部署JProfiler监控tomcat

    下载JProfiler包 wget http://download-keycdn.ej-technologies.com/jprofiler/jprofiler_linux_9_2.rpm 安装JPr ...

  2. OpenWRT镜像爬虫搭建本地源

    网上的爬虫不能用,还是先表达谢意,不过我比较懒不喜欢重复写别人写的教程,只贴出修改,怎么用自己看教程吧. 我自己改了一版可以正常爬: #!/usr/bin/env python #coding=utf ...

  3. AD域撤销域用户管理员权限方案

    一.简介 公司大部分主机加入域已有一段时间了,由于某软件没管理员权限不能执行,所以管理员权限一直没撤销,不能完全实现域的管理效果.但起码实现了域用户脱离不了域的控制:http://www.cnblog ...

  4. SQL必知必会 14-22(完)

    博主依然不想打字,又向你仍来了一堆代码... 13(续) 在SELECT中用COUNT()以及联合 mysql> SELECT customers.cust_id,COUNT(orders.or ...

  5. HDOJ 2393. Higher Math

    Higher Math Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  6. 深度优先搜索(DFS)

    [算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...

  7. [No0000AD]7z源码完全移植至Visual Studio 2015

    今天在上次的基础上(原文地址:[No0000AB]用Visual Studio 2015在 WIN10 64bit 上编译7-zip (32 bit)),将7Z的源码完全移植到了vs2015开发环境下 ...

  8. [No00008B]远程桌面发送“Ctrl+Alt+Delete”组合键调用任务管理器

    向远程桌面发送"Ctrl+Alt+Delete"组合键的两种方法 1.在本地按下Ctrl+Alt+End,可以成功发送"Ctrl+Alt+Delete"组合键! ...

  9. HTML5 & 三年二班周杰伦

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. 抱歉!15:44-16:39阿里云RDS故障造成全站不能正常访问

    非常非常抱歉!2016年3月7日15:44-16:39,由于阿里云RDS(云数据库)故障,造成全站不能正常访问,给您带来了很大很大的麻烦,恳请您的谅解! 故障是在15:44开始出现的,应用日志中出现大 ...