Android ant command Line compilation and APK signature detailed

Source: Internet
Author: User
Tags dname zip

Recently, when doing Android development, you need to refer to a third party code into the project, in general, directly under Eclipse to set the code to be imported to the library can be compiled, but a lot of code in eclipse to compile a lot of inexplicable errors. Therefore, you can only compile the code using command-line methods. The specific methods are as follows:

1. Install Java for compilation, install Android Platform-tools, install ant, set up all kinds of environment variables. Some Linux has been integrated by default, and Windows needs to be installed on its own.
2. Use the Android tool to update the compiled configuration of your code.
3. Using the Ant tool for compiling, you can compile the debug and release versions.
4. Generate the private key for code signing and sign the APK.
5. apk file alignment, for large file apk has a good effect.


Here's a reference to the bash code that you need to change, when executed, sudo bash xxx.sh can

The code is as follows Copy Code

# Setup Java Home
Java_home = "/system/library/java/javavirtualmachines/1.6.0.jdk/contents/home"

# Setup Android Home
Android_home= "~/android-sdk-macosx"

# Setup Path
PATH = "$ANDROID _home/tools: $JAVA _home/bin: $PATH"

# Update Current Project
Android Update Project--path.
#android Update Project--path./LIBRARY/LIB1
#android Update Project--path./library/lib2

# Remove KeyStore
Rm-f Product.keystore

# Make Keystone
Keytool-genkey-alias Product.keystore-keyalg rsa-validity 40000
-keystore product.keystore-storepass password-keypass Password
-dname "Cn=product ou=android team o=organization l=hongkong St=hongkong"

# clean
Ant Clean

# Remove Ant.properties
Rm-f ant.properties

# Make Ant.properties
echo "Key.store=product.keystore" > Ant.properties
echo "Key.alias=product.keystore" >> ant.properties

# Build APK
Ant Release

# Sign APK
# Jarsigner-verbose-keystore Product.keystore-signedjar
# bin/product_signed.apk bin/product-release-unsigned.apk Product.keystore

# Zip Align apk
# zipalign-v 4 bin/product_signed.apk bin/product_final.apk

Description
Update your Android ant compilation configuration, and if the structure of your code changes, execute, and if your code contains referenced third-party library code, you need to update the code separately.

The code is as follows Copy Code

# Update Current Project
Android Update Project--path.
#android Update Project--path./LIBRARY/LIB1
#android Update Project--path./library/lib2

KeyStore file for signature generation, description of several parameters
-genkey Generate secret Key certificate
-alias Alias
Computational algorithm of-KEYALG secret key
-validity Validity Period
-keystore secret key Library file path
-storepass secret key Library password
-keypass secret key Certificate password
-dname publisher Information
cn= Publisher name (personal name or product name)
ou= organizational unit name (team name)
o= Organization name (company name)
L= City/region
st= State/Province
C= Country Code

The code is as follows Copy Code

# Make Keystone
Keytool-genkey-alias Product.keystore-keyalg rsa-validity 40000
-keystore product.keystore-storepass password-keypass Password
-dname "Cn=product ou=android team o=organization l=hongkong St=hongkong"

Generate the Ant.properties file for Ant automatic compilation, set Key.store and Key.alias two variables, and automatically sign and align the generated apk when ant release

The code is as follows Copy Code

echo "Key.store=product.keystore" > Ant.properties
echo "Key.alias=product.keystore" >> ant.properties

Manual code signing, description of several parameters
-verbose Display Output
-keystore KeyStore Alias
-signedjar signature apk file, the first parameter is the target file, the second is unsigned apk

The code is as follows Copy Code

# Sign APK
Jarsigner-verbose-keystore Product.keystore-signedjar bin/product_signed.apk bin/product-release-unsigned.apk Product.keystore

To align apk manually

  code is as follows copy code

# Zip Align APK
Zipalign-v 4 bin/product_signed.apk bin/product_final.apk

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.