Develop, compile, and package Android applications using command lines

Source: Internet
Author: User

The android SDK provides a series of command line tools for Android Application Development, debugging, and packaging! Eclipse is able to develop Android applications. It also calls these tools.

It is easier to work only when you have mastered the working methods under commands. Next, let's move away from the eclipse integrated development tool and go through the android development, compilation, and packaging process with your hands!

 

Note: This article is for reference only, because the lbsdemo project contains code, third-party jar packages, so files, and so files! Therefore, following the instructions in this article step by step will not work!

 

1. Create an Android Application

The android SDK provides an android. bat batch processing tool that can be used to process the following tasks. Here we only introduce its sub-command Create Project for Program Creation.

D:\Android>android --helpUsage:  android [global options] action [action options]Global options:  -v --verbose  Verbose mode: errors, warnings and informational messages are printed.  -h --help     Help on a specific command.  -s --silent   Silent mode: only errors are printed out.Valid actions are composed of a verb and an optional direct object:-   list               : Lists existing targets or virtual devices.-   list avd           : Lists existing Android Virtual Devices.-   list target        : Lists existing targets.- create avd           : Creates a new Android Virtual Device.-   move avd           : Moves or renames an Android Virtual Device.- delete avd           : Deletes an Android Virtual Device.- update avd           : Updates an Android Virtual Device to match the foldersof a new SDK.- create project       : Creates a new Android project.- update project       : Updates an Android project (must already have an AndroidManifest.xml).- create test-project  : Creates a new Android project for a test package.- update test-project  : Updates the Android project for a test package (must already have an AndroidManifest.xml).- create lib-project   : Creates a new Android library project.- update lib-project   : Updates an Android library project (must already have an AndroidManifest.xml).- update adb           : Updates adb to support the USB devices declared in theSDK add-ons.- update sdk           : Updates the SDK by suggesting new platforms to installif available.

 

The usage of the -- Create Project subcommand is as follows:

Action "Create Project": Creates a new Android project. options: -N -- name: Application name-t -- target SDK target ID-p -- path: application's working directory-k -- package application package name-a -- default created by activity activity name

 

After learning about the commands and their usage, you can use them to create an application: Create an lbsdemo program in the D: \ Android working directory.

The command automatically creates the following content:

2. Compile the Android Application

A: generate the R. Java file.

The android program code references a large number of resource files. These resource files are not referenced manually. androidsdk provides an R. java class to manage resources, so that programmers can easily reference and manage resource files. Therefore, to compile a program, you must first generate R. java class.

To compile the R. Java class, you need to use the aapt tool provided by androidsdk. There are many aapt 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 base include set  -J  specify where to output R.java resource constant definitions  -M  specify 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 scannd

 

Aapt:

You need to enter the lbsdemo application directory and create a new gen directory without the gen directory. The command will fail to find the file!

After the command is successfully executed, the directory tree of the Package Structure and the R. Java file will be generated under the gen directory!

 

Parameter description

-F-m-J Gen: generate the R. java-s Res: Specifies the resource file-I E: \ Android \ SDK \ android-sdk_r09-windows \ platforms \ Android-7 \ android. jar: Use the android jar package of the specified version-M androidmanifest. XML: configuration file of the specified program
 

B: generate a Java File Based on the. aidl definition file.

To generate a. aidl file, you must use the aidl tool that comes with androidsdk. 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 cannot be spaces between the parameters and parameter values of this tool, and Google has engineers who are not satisfied with the salary!

C: Use the javac command tool provided by Java SDK to compile the. Java source file into a. Class file.

The javac command is used as follows:

Among them, possible options include:-g to generate all debugging information-G: None not to generate any debugging information-G: {lines, vars, source} only generate some debugging information.-nowarn does not generate any warning.-verbose outputs the message about the operation being executed by the compiler.-deprecation outputs the source location of an outdated API.-classpath <path> specify the location for searching user-class files and annotation handlers-CP <path> specify the location for searching user-class files and annotation handlers-sourcepath <path> specify the location for searching input source files-bootclasspath <path> overwrite the location of the boot class file-extdirs <directory> overwrite the location of the installed extension directory-endorseddirs <directory> overwrite the location of the signature's standard path-Proc: {none, only} controls whether to perform annotation processing and/or compilation. -Processor <class1> [, <class2>, <class3>...] name of the annotation handler to run; attackers can bypass the default search process-processorpath <path> to specify the location of the comment Handler-D <directory> to specify the location where generated class files are stored-S <directory> to specify the location where generated source files are stored location-implicit: {none, class} specifies whether to generate a class file for implicit reference files-encoding <encoding> specifies the character encoding used by the source file-source <version> provides source compatibility with the specified version-target <version> generation for a specific VM version, the class file-version Version Information-help outputs the abstract of the standard option-akey [= value] The option passed to the annotation Handler-x outputs the abstract of the non-standard option-j <flag> pass the <flag> directly to the runtime system
 

The details are as follows [because compilation is required. java files are distributed in different folders, So multiple files are written *. java file path. if a third-party jar package is referenced, you must specify the jar package into classpath]:

D: Use the DX. Bat command line script provided by the android SDK to generate the classes. Dex file.

Compile the class file under the bin directory of the project into classes. Dex. The Android virtual machine can only execute the DEX file!

The. Dex file generated by the command is as follows:

E: Use aapt.exe of Android sdkscripts to generate resource package files (including res, assets, and androidmanifest. XML ):

Refer to the above for aapt usage!

The. AP _ file generated by the command is as follows:

 

 

 

 

F: Use the apkbuilder tool provided by androidsdk to generate unsigned APK files

Important parameters are 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 ommited 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.

Note: 1. to package the so file into the APK, add the-NF parameter.

2. if the third-party jar package contains image resources, you must add the-RJ parameter. Otherwise, the resource file in the jar package cannot be parsed and the program reports an error because the resource cannot be referenced!

Command generated .apk File

 

H: Use the keytools tool provided by javasdk to generate a certificate file!

The above only generates an APK file without a certificate. Android will not install an APK package without a digital certificate signature, so if you need to install the APK, you must sign a digital certificate!

After you enter a command here, the command will interact with the user. If you do not want to interact with the user, you need to tell the command to ask in advance!

Keytool-genkey-alias helloworld. key store-keyalg RSA-validity 1000-keystore helloworld. keystore-dname "cn = W, ou = W, O = localhost, L = W, St = W, c = cn"-keypass 123456-storepass 123456

The. keystore file generated by the command is as follows:

I: In the last step, use the jarsigner command to sign the unsigned APK file with the Certificate file.

Important parameters are as follows:

[-Keystore <URL>] keystore location [-storepass <password>] password used for keystore integrity [-storetype <type>] keystore type [-keypass <password>] private Key password (if different) [-sigfile <File>]. SF /. name of the DSA file [-signedjar <File>] Name of the signed JAR file [-digestalg <algorithm>] Name of the digest algorithm [-sigalg <algorithm>] Name of the signature algorithm [-verify] verifies the signed JAR file [-verbose] and outputs detailed information during signature/verification. [-Certs] outputs detailed information and displays the certificate during verification. [-TSA <URL>] location of the timestamp institution [-tsacert <alias>] Public Key Certificate of the timestamp institution [-altsigner <class>] Name of the signature mechanism class [-altsignerpath <path list>] The Position of the alternative signature mechanism [-internalsf] is included in the signature block. SF file [-sectionsonly] does not calculate the hash of the entire list [-protected] keystore protected authentication path [-providername <Name>] provider name [-providerclass <class> encryption service provider name [-providerarg <parameter>]... main class file and constructor Parameters

 

The usage is as follows:

 

It's done!

Summary:

By using these commands, you can clearly understand every detail of program development, compilation, and packaging, and how eclipse works. Even if you change to an integrated development tool, you can quickly get started! You can even develop a set of tools with the same functions as packaging in eclipse! The following is my summary of the above command! If the company needs it, you can also write it!

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.