Android packaging can be directly packaged using eclipse, while eclipse packaging is actually the execution of a series of Packaging Commands. Since the command is executed, the command line is also competent, so with the bat script packaging. The packaging process is roughly as follows:
1. Generate R. java
2. Compile *. java
3. Generate classes. dex
4. Package assets and res
5. generate an unsigned apk
6. Sign the apk
Based on these steps, we use bat to complete it step by step. Below is a script file.
1. Generate R. java
Create an AntDemo project and write the first bat script named 1_genR.bat under the root directory of the project. The Code is as follows.
aapt package -f -m -J gen -S res -I D:/android-sdk-windows/platforms/android-16/android.jar -M AndroidManifest.xml
Note: The R. java file is generated after execution. After you directly create an AntDemo project using eclipse, the R. java file already exists in the gen directory. If you execute this script again, the modification time will change. If you are not sure, you can delete R. java and then execute the script to view the effect.
The following figure shows the execution of the script.
2. Compile *. java
The javac command under JDK is called during compilation. Therefore, JDK must be installed. We recommend that you use JDK 1.6 (1.7 may cause problems ). After the installation is complete, set the bin directory in the JDK directory to an environment variable and compile the script 2_compile.bat.
mkdir binjavac -target 1.6 -bootclasspath D:/android-sdk-windows/platforms/android-17/android.jar -d bin gen\com\example\antdemo\*.java src\com\example\antdemo\*.java
Note:
(1) mkdir bin is used to create the bin directory.
(2) target in javac specifies the jdk version.
(3) android. jar must correspond to the corresponding version. Here, android-17 is selected.
(4) gen \ com \ example specifies the directory where the java file is located.
After the script is executed, a series of class files are generated in the bin directory, such.
3. Generate classes. dex
The dx command is a dx. bat script, which is located in android-sdk/plaform-tools. To directly execute the dx command, you need to set the directory where dx. bat is located to the environment variable. The following is the written script 3_dex.bat.
dx --dex --output=G:\Code\Android\Workspace\AntDemo\bin\classes.dex G:\Code\Android\Workspace\AntDemo\bin
Note:
(1) output specifies the position of the output dex.
(2) G: \ Code \ Android \ Workspace \ AntDemo \ bin specifies the directory of the class file.
For the files generated after the script is executed, see.
4. Package assets and res
Compile 4_package.bat with the following content.
aapt package -f -A assets -S res -I D:/android-sdk-windows/platforms/android-17/android.jar -M AndroidManifest.xml -F bin/AntDemo
Note: Package the files in assets and res to generate the AntDemo file. See.
5. generate an unsigned apk
Write 5_unsigned.bat with the following content.
apkbuilder G:\Code\Android\Workspace\AntDemo\bin\AntDemo_unsigned.apk -v -u -z G:\Code\Android\Workspace\AntDemo\bin\AntDemo -f G:\Code\Android\Workspace\AntDemo\bin\classes.dex -rf G:\Code\Android\Workspace\AntDemo\src
Note:
(1) You need to call the apkbuilder command, which is a script. In android-sdk/tools, you need to set this directory to environment change ......
(2) You need to build the apk together with the files in the generated AntDemo, classes. dex and src.
The following figure shows the generated result.
6. Sign the apk
Before signing the apk, You need to generate a signature file *. keystore. The signature file can be generated by using keytool.exe in the binary directory under JDK. You can use the following script:
keytool -genkey -alias ant_test -keyalg RSA -validity 20000 -keystore my.keystore
Run the script and output it as prompted. The password used in the test version is 123456. Press enter (default unknown is used ). For details, refer to this article.
《
Apk file signature for Android
With the keystore, you can sign the apk and write the script 6_signed.bat. The content is as follows:
jarsigner -keystore G:\Code\Android\Workspace\AntDemo\build\my.keystore -storepass 123456 -keypass 123456 -signedjar G:\Code\Android\Workspace\AntDemo\bin\AntDemo_signed.apk G:\Code\Android\Workspace\AntDemo\bin\AntDemo_unsigned.apk ant_test
Note:
(1) jarsigner is in the bin directory of JDK. If this directory is not set to an environment variable, it must be set to an environment variable.
(2) enter the password and alias used in generating my. keystore (the parameter specified by alias when the signature file is generated ).
The following figure shows the execution of the script.
The above process is separated step by step. If you need to execute it together, you can combine the script into a script, or create another script build. bat to call various scripts. The following is build. bat script code.
call 1_genRcall 2_compilecall 3_dexcall 4_packagecall 5_unsignedcall 6_signed
In this way, the bat script packaging is complete. The execution of bat scripts is the execution of a series of tasks, while ant is also the execution of a series of tasks. Therefore, you can convert these scripts into ant scripts, for details, see Android packaging to convert bat script to ant script.
Reprinted, please indicate the source: Android packaging to convert bat script into ant script
Source code download