Android's Ant compilation package

Source: Internet
Author: User


Android itself supports ant packaging projects, and the SDK comes with a build.xml file.
Through this file, the file can be compiled, packaged, installed, and so on. and support a variety of ways to package, such as Debug or release.
As a general rule, you can do this in the following ways:

First create an Android project.
After the project is created, you need to add ant support for the project, which is to create the appropriate Build.xml file.
This file does not need to be created by the user, just execute the following command:
<sdk>/tools/android update project-p <project> t <target>

Use: Android Updata project-p./-N oliveboxclient-t android-10 to regenerate related files. The most important thing is build.xml.


which
The SDK is the installation directory for the SDK, and the tools/android below is the command we want to use.
Project is a catalog of projects.
Target is the target ID of the android used by the project, which is the version of Android that corresponds to the project.

You can add the tools directory of the SDK to the environment variable ~/.BASHRC file, which takes effect after a reboot, and you can use the command directly later.
You can view the target and the corresponding ID contained in the current SDK by executing the following command:
Android list targets

When you finish executing the Android Update Project command, the appropriate ant file is generated at the root of the project.
Mainly have Build.xml, ant.properties, local.properties three files.
If you do not have a ant.properties file, you can manually add it yourself.

Where the Build.xml file is the primary file for the ant build, it references other files.
The Sdk.dir attribute is defined in the Local.properties file, which is the directory of the SDK currently in use.
In the Ant.properties file, you can define some of your own properties, or redefine some properties.
Of course, these two properties files are dispensable and are not directly defined in Build.xml.

At the end of the Build.xml, the Tools/ant/build.xml file is referenced in the SDK directory.
This is the default build file for the SDK, which can copy the contents directly or keep the current reference.
The default target for Build.xml under the project is help, which can be seen after running.
With the help information, you can see other available target.
In general, we need to change to debug or release.
After you have modified it, run the Build.xml file again, and the compilation process we need will be executed.
The Build.xml file is executed by right-clicking->run as->ant Build.

If it is a debug version, it will be signed by default using debug mode.
If this is the release version, you need to specify the appropriate KeyStore and the private key.
Otherwise, only one unsigned apk file can be generated at the end.

To set up the private key library, in the Ant.properties file, add the following:
Key.store=<keystore>
Key.alias=<key>
Key.store.password=<keystore pwd>
Key.alias.password=<key pwd>

which
KeyStore is the private key library file.
Key is the private key to be used for signing.
Key.store.password is the password for the private key library.
Key.alias.password is the password for the private key.

Two password-related attributes can also be added without adding a private key library and a private key.
In this case, the execution will pop up the corresponding input box, prompting the user to enter the appropriate password.
This is relatively cumbersome, but the protection of the private key library is better because there is no clear-text designation of the password.

If the project references a third party libraries, you only need to create the Libs folder in the project root directory and put it in.
If it is a jar library, it is placed directly under the Libs directory, and if so, it needs to be placed in Libs's subdirectory named Armeabi.
You can also specify the location of the third-party library by setting the appropriate properties, which by default are the Libs folder.
Jar.libs.dir is the directory where the jar type library resides.
Native.libs.absolute.dir is the directory where the so type library resides, that is, the parent directory of Armeabi.

If the project contains JNI code and you want to automatically recompile the so library when packaging, you can modify the Build.xml file.
To modify the method, add the following target before referencing the SDK's Build.xml file:
<target name= "-pre-build" depends= "-ndk-build" >
</target>
<target name= "-ndk-build" >
<exec executable= "Ndk-build" failonerror= "true" >
<arg value= "clean"/>
</exec>
<exec executable= "Ndk-build" failonerror= "true"/>
</target>

Ndk-build commands for the NDK, you need to add the NDK installation directory to the environment variable and add the method with Sdk/tools.
The resulting so is placed by default in the Libs/armeabi directory.

When you use the default Build.xml to package the APK, you have involved processes such as obfuscation, signing, alignment optimization, and so on.
If you use release, the last generated apk file can be published directly.

Legacy issues:
Currently using this method to generate the APK, although it has been signed, but the installation error, the prompt is not signed.
View the signature file in the APK package, not the default cert.*, but <key>.*.
Then, even if you modify the name to Cert.*, the program still does not install properly.
If you export the debug version, you will not have this problem.
There is no problem with using the ADT plugin to export the signature apk.

Workaround:
The root cause of this problem is JDK1.7, which occurs only when you run ant to use the jre1.7 version.
This can be resolved by setting the version of the JRE that is used when the Build.xml file is run:
Select Build.xml-> Right-click->run as->external Tools configurations,
Select the JRE tab in the right area to see three options for JRE settings:
The Run in the same JRE as the workspace uses the same version of the JRE as the workspace.
Execution Environment Select a JRE version based on the relevant environment.
The separate JRE uses the current version of a JRE that is already installed.
The JRE for a generic project is set to a version below 1.7, so it is recommended that you select the first one so that it is consistent with the project settings.

Android's Ant compilation package

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.