English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Compilation and Packaging Process of Android Applications (APK)

Flowchart:

 

We focus on the following:(1What is the input of this process?2What is the output of this process?3What tools are used in this process?As for which parameters to use, you can refer to the help file of the corresponding command or search online, which is not the focus of this article.

aapt->

aidl -> javac-> dx(dex)-> apkbuilder-> jarsigner-> zipalign 

The tools mentioned in the steps are as follows in the table:

name function introduction path in the operating system
aapt Android resource packaging tool ${ANDROID_SDK_HOME}/platform-tools/appt
aidl tool to convert Android interface description language to .java files ${ANDROID_SDK_HOME}/platform-tools/aidl
javac Java Compiler ${JDK_HOME}/javac or/usr/bin/javac
dex convert .class files to .dex files recognizable by Davik VM ${ANDROID_SDK_HOME}/platform-tools/dx
apkbuilder generate apk package ${ANDROID_SDK_HOME}/tools/opkbuilder
jarsigner tool for signing .jar files ${JDK_HOME}/jarsigner or/usr/bin/jarsigner
zipalign bytecode alignment tool ${ANDROID_SDK_HOME}/tools/zipalign

Step 1: Package resource files, generate R.java file

The Android SDK's aapt tool is required to compile the R.java class, aapt has many parameters, the following are the main parameters:

-d one or more device assets to include, separated by commas 
 -f force overwrite of existing files 
 -g specify a pixel tolerance to force images to grayscale, default 0 
 -j specify a jar or zip file containing classes to include 
 -k junk path of file(s) added 
 -m make package directories under location specified by -J 
 -u update existing packages (add new, replace older, remove deleted files) 
 -v verbose output 
 -x create extending (non-application) resource IDs 
 -z require localization of resource attributes marked with 
 localization="suggested" 
 -A additional directory in which to find raw asset files 
 -G A file to output proguard options into. 
 -F specify the apk file to output 
 -I add an existing package to the base include set 
 -J specify where to output R.java resource constant definitions 
 -M specify the full path to AndroidManifest.xml to include in zip 
 -P specify where to output public resource definitions 
 -S directory in which to find resources. Multiple directories will be scanned 

The specific compilation of R.java file by aapt is as follows:

You need to enter the application directory, create a new gen directory. If there is no gen directory, the command will report an error that the file cannot be found!

After the command is successfully executed, a directory tree with package structure and R.java file will be generated in the gen directory!

Example:

Step 2: Process the AIDL file to generate the corresponding .java file (of course, many projects do not use AIDL, so this process can be skipped).

The generation of .java files from .aidl files requires the aidl tool bundled with AndroidSDK, and the specific parameters of this tool are as follows:

-I<DIR> search path for import statements. 
-d<FILE> generate dependency file. 
-p<FILE> file created by --preprocess to import. 
-o<FOLDER> base output folder for generated files. 
-b fail when trying to compile a parcelable. 

It is worth noting that there should be no spaces between the parameters and parameter values for this tool, and Google also has engineers who are not satisfied with the salary!

Example:

Third step: compile Java files to generate corresponding .class files

Usage of javac command is as follows:

Possible options include:

 -Generate all debugging information with g 
 -Do not generate any debugging information with g:none 
 -Generate only certain debugging information with g:{lines,vars,source} 
 -Do not generate any warnings 
 -Output messages about the operations being performed by the compiler 
 -Output the location of source files using deprecated APIs 
 -Specify the location to search for user class files and annotation processing programs with classpath <path> 
 -Specify the location to search for user class files and annotation processing programs with cp <path> 
 -Specify the location to search for input source files with sourcepath <path> 
 -Cover the location of the boot class files with bootclasspath <path> 
 -Cover the location of the installed extension directories with extdirs <directory> 
 -Cover the location of the standard path for signed directories with endorseddirs <directory> 
 -Control whether to execute annotation processing and proc:{none,only}/or compile. 
 -processor <class1>[,<class2>,<class3The name of the annotation processing program to run; bypass the default search process 
 -Specify the location to search for annotation processing programs with processorpath <path> 
 -Specify the location to store generated class files with d <directory> 
 -Specify the location to store generated source files with s <directory> 
 -Specify whether to generate class files for implicit references as {none,class} 
 -Specify the character encoding used by the source file with encoding <encoding> 
 -Provide source compatibility with the specified version with source <version> 
 -Generate class files for a specific VM version with target <version> 
 -Version information 
 -Output the summary of standard options 
 -Pass options to the annotation processing program as Akey[=value] 
 -Output the summary of non-standard options 
 -Directly pass <flag> to the runtime system 

Example:

javac -encoding utf-8 -target 1.5 -bootclasspath E:\Androiddev\android-sdk-windows2.2\platforms\android-3\android.jar -d bin src\com\byread\reader\*.java gen\com\byread\reader\R.java 

Step 4: Convert the .class files into .dex files supported by the Davik VM

Compile the class files in the project's bin directory into classes.dex, because the Android virtual machine can only execute dex files!

Example:

Step 5: Package to generate an unsigned .apk file

【Input】Packaged resource files, packaged class files (.dex files), and libs files (including .so files, although many projects do not have such files, if you do not use C/C++For development purposes)

【Output】Unsigned .apk file

【Tool】apkbuilder tool

The usage of apkbuilder tool is as follows:

-v Verbose. 
-d Debug Mode: Includes debug files in the APK file. 
-u Creates an unsigned package. 
-storetype Forces the KeyStore type. If omitted the default is used. 
-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. 
-rj Followed by the path to a jar file or a folder containing 
 jar files. 
 Adds the java resources found in the jar file(s) to the application 
 package. 
-nf Followed by the root folder containing native libraries to 
 include in the application package.<span style="color: rgb(0, 0, 255); font-family:楷体; line-height: 20px;font-size:18px; ">I:Final step, sign the unsigned APK file using the certificate file with the jarsigner command</span> 

Example:

apkbuilder ${output.apk.file} -u -z ${packagedresource.file} -f ${dex.file} -rf ${source.dir} -rj ${libraries.dir} 

Step 6: Sign the unsigned .apk file

[Input] Unsigned .apk file

[Output] Signed .apk file

[Tool] jarsigner

Usage: jarsigner [options] jar file alias 
jarsigner -verify [options] jar file 
[-keystore [url]      Location of the keystore 
[-storepass [password]     Password for the integrity of the keystore 
[-storetype [type]     Type of the keystore 
[-keypass [password]      Password for the private key (if different) 
[-sigfile [file]      .SF/.DSA file name 
[-signedjar [file]     Name of the signed JAR file 
[-digestalg [algorithm]  Name of the digest algorithm 
[-sigalg [algorithm]    Name of the signature algorithm 
[-verify]          Verify the signed JAR file 
[-verbose]         Signature/Output detailed information during verification 
[-certs]          Output detailed information and display certificates during verification 
[-tsa [url]        Location of the timestamp authority 
[-tsacert [alias]      Public key certificate of the timestamp authority 
[-altsigner <class>]      Class name of the alternative signature mechanism 
[-altsignerpath <path list>] Alternative signature mechanism location 
[-internalsf]        Include .SF file within the signature block 
[-sectionsonly]       Do not calculate the hash of the entire manifest 
[-protected]        Protected key store verification path 
[-providerName <name>]   Provider name 
[-providerClass <class>    Name of the encrypted service provider 
[-providerArg <parameter> ... Main class file and constructor parameter  

Step 7: Align the signed .apk file (alignment is not performed and cannot be published on Google Market)

[Input] Signed .apk file

[Output] Aligned .apk file

[Tool] zipalign tool

After knowing these details, we can realize many things we want to realize, such as:AutomationWe can use some scripts, such as batch processing under Windows, Bash under Linux, Ant under Java, and script languages like Python, Perl, and even directly use strongly typed languages like Java and .net.

That's all for the content of this article. I hope the content of this article can bring a certain amount of help to everyone's learning or work, and also hope to support the Yell Tutorial more!

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously, and this website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting. Provide relevant evidence, and once verified, this site will immediately delete the suspected infringing content.)

You May Also Like