Build Android Packages From Command Line

A few months ago,I dealed with a task:To build a large amount of apk files. The trick I came up with is to build apk file from the command so that I could use Python to glue all the works. Eventually I made it.And so this post is to make some description about the trick.

Requirements

  • Setup JDK
  • Setup Android SDK Steps
  • Generate R class file
  • Compile Java codes(.java files) into classes(.class) files
  • Convert .class files into .dex files
  • Package Resouces
  • Build Unsigned APK File
  • Sign Apk with Jarsigner
  • The Extra One:Use zipalign for optimization

Generate R class File

In Android,We use R class to refer resources instead of hard-coding the resouces. For a better understanding,pleae have a look at http://www.satyakomatineni.com/akc/display?url=displaynoteimpurl&ownerUserId=satya&reportId=2883

1

aapt package -f -m -J /home/androidyue/temp/ubuntu/workspace/MxDataProvider/gen/ -S /home/androidyue/temp/ubuntu/workspace/MxDataProvider/res/ -I /home/androidyue/dev_tools/android-sdk-linux_86_backup/platforms/android-17/android.jar -M /home/androidyue/temp/ubuntu/workspace/MxDataProvider/AndroidManifest.xml

Some descriptions

  • -f force overwrite of existing files
  • -m make package directories under location specified by -J
  • -J specify where to output R.java resource constant definitions
  • -S directory in which to find resources. Multiple directories will be scanned and the first match found (left to right) will take precedence.
  • -I add an existing package to base include set
  • -M specify full path to AndroidManifest.xml to include in zip

Comiple .java into .class files

1

javac -encoding UTF-8 -source 1.6 -target 1.6 -bootclasspath /home/androidyue/dev_tools/android-sdk-linux_86_backup/platforms/android-17/android.jar -d /home/androidyue/temp/ubuntu/workspace/MxDataProvider/bin/ /home/androidyue/temp/ubuntu/workspace/MxDataProvider/src//coop/channel/provider/*.java /home/androidyue/temp/ubuntu/workspace/MxDataProvider/gen//coop/channel/provider/R.java

Some descriptions

  • -encoding encoding Set the source file encoding name, such as EUC-JP and UTF-8. If -encoding is not specified, the platform default converter is used.
  • -source release Specifies the version of source code accepted, Please Do NOT use Java 7(1.7)
  • -target version Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier versions of the VM. Valid targets are 1.1, 1.2, 1.3, 1.4, 1.5 (also 5), 1.6(also 6), and 1.7 (also 7).
  • -bootclasspath bootclasspath Cross-compile against the specified set of boot classes. As with the user class path, boot class path entries are separated by colons (:) and can be directories, JAR archives, or ZIP archives.
  • -d directory Set the destination directory for class files. The directory must already exist; javac will not create it. As I have suffered a lot using Java 7, It’s recomended to use Java 6

Convert .class into .dex files

1

/home/androidyue/dev_tools/android-sdk-linux_86_backup/build-tools/17.0.0/dx --dex --output=/home/androidyue/temp/ubuntu/workspace/MxDataProvider/bin//class.dex /home/androidyue/temp/ubuntu/workspace/MxDataProvider/bin/

To dive into Dalvik, please visit http://source.android.com/devices/tech/dalvik/index.html

Package Resouces

1

aapt package -f -M /home/androidyue/temp/ubuntu/workspace/MxDataProvider/AndroidManifest.xml -S /home/androidyue/temp/ubuntu/workspace/MxDataProvider/res/ -A /home/androidyue/temp/ubuntu/workspace/MxDataProvider//assets/ -I /home/androidyue/dev_tools/android-sdk-linux_86_backup/platforms/android-17/android.jar -F /home/androidyue/temp/ubuntu/workspace/MxDataProvider/bin//resources.ap_

Some descriptions

  • -F specify the apk file to output
  • -A additional directory in which to find raw asset files

Build Unsigned APK File

1

/home/androidyue/temp/ubuntu/dev_tools/adt-bundle-linux_backup/sdk/tools/apkbuilder /tmp/unsignedApkFile.apk -v -u -z /home/androidyue/temp/ubuntu/workspace/MxDataProvider/bin//resources.ap_ -f /home/androidyue/temp/ubuntu/workspace/MxDataProvider/bin//class.dex -rf /home/androidyue/temp/ubuntu/workspace/MxDataProvider/src/

Some descriptions

  • -u Creates an unsigned package.
  • -z Followed by the path to a zip archive. Adds the content of the application package.
  • -f Followed by the path to a file. Adds the file to the application package.
  • -rf Followed by the path to a source folder. Adds the java resources found in that folder to the application package, while keeping their path relative to the source folder.

Sign Apk with Jarsigner

1

jarsigner -keystore /home/androidyue/temp/ubuntu/myKeystore -storepass storepassValue -keypass keypassValue -signedjar /home/androidyue/Desktop/output/max1111111.apk /tmp/unsignedApkFile.apk maxthon -digestalg SHA1 -sigalg MD5withRSA

Some descriptions

  • [-keystore ] keystore location
  • [-storepass ] password for keystore integrity
  • [-keypass ] password for private key (if different)
  • [-signedjar ] name of signed JAR file
  • [-digestalg ] name of digest algorithm
  • [-sigalg ] name of signature algorithm

Use Zipalign for optimization

1

zipalign -f -v 4 /home/androidyue/Desktop/output/max1111111.apk /home/androidyue/Desktop/output/max222222.apk

Some descriptions

Others

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏张善友的专栏

Building a Middle Tier Component using NHibernate and Spring.NET

http://www.codeproject.com/csharp/SpringPlusHibernate.asp Download demo project ...

22770
来自专栏全栈架构

Spring Boot 与 Kotlin 上传文件

如果我们做一个小型的web站,而且刚好选择的kotlin 和Spring Boot技术栈,那么上传文件的必不可少了,当然,如果你做一个中大型的web站,那建议你...

12430
来自专栏大数据学习笔记

Hadoop基础教程-第9章 HA高可用(9.3 HDFS 高可用运行)(草稿)

第9章 HA高可用 9.3 HDFS 高可用运行 9.3.1 HA节点规划 节点 IP Zookeeper NameNode JournalNode Da...

28950
来自专栏

Spring MVC基于注解的Junit测试

INFO : org.springframework.test.context.TestContextManager - @TestExecutionList...

18450
来自专栏FD的专栏

Effective Testing with RSpec 3 (英文版)(序言)

Early praise for Effective Testing with RSpec 3

21940
来自专栏西二旗一哥

Clang - Use Xcode to make first Clang plugin

27750
来自专栏Golang语言社区

Go调试简单的内存泄漏

Memory leaks are a class of bugs where memory is not released even after it is n...

56730
来自专栏一个会写诗的程序员的博客

Caused by: android.os.NetworkOnMainThreadException

Caused by: android.os.NetworkOnMainThreadException at android.os.StrictMode$And...

16420
来自专栏Kubernetes

kube-controller-manager最佳配置

kubernetes version: 1.6.2 Kube-controller-manager Configuration kube-controller-...

53590
来自专栏李成熙heyli

webpack Performance: The Comprehensive Guide

Introduction For beginners, webpack building performance is sometimes annoying. ...

23570

扫码关注云+社区

领取腾讯云代金券