技术小黑屋

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





快来解锁最新版 Typora,新用户券后仅需 84 元!
如何便宜的购买 office 365 ?
新版赤友 NTFS 助手来袭,正版超低价