Nowadays, many people want to automate the compilation and packaging of Android projects, such as building a daily system and generating release files automatically. We need to have an in-depth understanding of the compilation and packaging of the android project. At least we need to know what each step of the android project has done and what environment and tools are needed, what is input and output. Then we can explore the details in the android compilation process.
First of all, let's assume that your system (which can be used on any system, but not limited to Linux or Windows systems). Of course, here I will use the Linux system as an example by default, but there is almost no difference in Windows) JDK and Android SDK have been installed. Assume that the path of your android SDK is android_sdk_home. The Android OS version you want to compile is android_ OS _version (for example, Android-1.6, Android-8, and Android-10 ).
What we focus on is(1) What is input in this process? (2) What is the output of this process? (3) what tools are used in this process?As for the parameters used, you can view the help files of the corresponding commands or search for them online. This is not the focus of this article.
The tools mentioned in the steps are as follows:
Name |
Features |
Path in the operating system |
Aapt |
Android resource packaging Tool |
$ {Android_sdk_home}/platform-tools/APPT |
Aidl |
Android interface description language conversion to. Java file Tool |
$ {Android_sdk_home}/platform-tools/aidl |
Javac |
Java compiler |
$ {Jdk_home}/javac or/usr/bin/javac |
Dex |
Convert the. Class file to a. Dex file that can be recognized by davik VM. |
$ {Android_sdk_home}/platform-tools/dx |
Apkbuilder |
Generate an APK package |
$ {Android_sdk_home}/tools/opkbuilder |
Jarsigner |
. Jar file signature Tool |
$ {Jdk_home}/jarsigner or/usr/bin/jarsigner |
Zipalign |
Bytecode alignment tool |
$ {Android_sdk_home}/tools/zipalign |
Step 1: Package the resource file and generate the R. Java File
[Input] resource file (the file in res in the project), assets file (equivalent to another resource, this resource Android system does not optimize it like the file in RES), and androidmanifest. XML file (the package name is read from here, because the R. java file requires package name), Android basic library (Android. JAR file)
[Output] package resources (generally, a package named resources is displayed in the bin directory of the android project. AP _ file), R. java file (in the gen directory, you should be familiar with it)
[Tool] aapt tool. Its path is $ {android_sdk_home}/platform-tools/aapt. (If you are using a Windows system, you should write the path as follows: % android_sdk_home % \ platform-tools \ aapt.exe, the same below ).
Step 2: process the aidl file and generate the corresponding. Java file (of course, this process can be saved if aidl is not used in many projects)
[Input] source code file, aidl file, and framework. aidl File
[Output] corresponding. Java File
[Tool] aidl Tool
Step 3: Compile the Java file and generate the corresponding. Class file.
[Input] source code file (including the. Java file generated by R. Java and aidl) and library file (. jar file)
[Output]. Class File
[Tool] javac Tool
Step 4: Convert the. Class file to the. Dex file supported by davik VM.
[Input]. Class file (including. Class file generated by aidl,. Class file generated by R,. Class file generated by source file), library file (. jar file)
[Output]. Dex File
[Tool] javac Tool
Step 5: Package the unsigned .apk File
[Input] The package resource file and the package class file (. dex files), Libs files (including. so files, of course, many projects do not have such files, if you do not use C/C ++ for development)
Export the unsigned .apk File
[Tool] apkbuilder Tool
Step 6: sign the unsigned .apk File
Enter the unsigned .apk File
Export the .apk file with the signature
[Tool] jarsigner
Step 7: Align the .apk file after the signature (the file cannot be published to Google market without alignment)
Enter the .apk file behind the token.
The. APK file behind the outputs
[Tool] zipalign Tool
After knowing these details, we can implement many things we want to implement, such:Automation, We can use some script, such as Windows batch processing, Linux bash, Java ant, Python, Perl, and other scripting languages, or even directly use Java ,. net.If you really understand the above steps and understand the nature of the compilation and packaging process, you can automate it in any way you want, ".For example,Simplified Android SDKAs you know, now android
The SDK works a few hundred megabytes. We can apply the above knowledge and only keep the necessary tools to streamline the SDK to less than 10 MB. Of course, you can do a lot of things on the premise that you really understand it.