Building Your Application

There are two ways to build your application: one for testing/debugging your application — debug mode — and one for building your final package for release — release mode. As described in the previous section, your application must be signed before it can be installed on an emulator or device.

Whether you're building in debug mode or release mode, you need to use the Ant tool to compile and build your project. This will create the .apk file that is installed onto the emulator or device. When you build in debug mode, the .apk file is automatically signed by the SDK tools with a debug key, so it's instantly ready for installation (but only onto an emulator or attached development device). When you build in release mode, the .apk file is unsigned, so you must manually sign it with your own private key, using Keytool and Jarsigner.

To update an existing Android project, open a command-line and navigate to the tools / directory of your SDK. Now run:

android update project --name <project name> --target <target ID> --path path/to/your/project/

It's important that you read and understand Signing Your Applications, particularly once you're ready to release your application and share it with end-users. That document describes the procedure for generating a private key and then using it to sign your .apk file. If you're just getting started, however, you can quickly run your applications on an emulator or your own development device by building in debug mode.

If you don't have Ant, you can obtain it from the Apache Ant home page. Install it and make sure it is in your executable PATH. Before calling Ant, you need to declare the JAVA_HOME environment variable to specify the path to where the JDK is installed.

J Note: When installing JDK on Windows, the default is to install in the "Program Files" directory. This location will cause ant to fail, because of the space. To fix the problem, you can specify the JAVA_HOME variable like this: set JAVA HOME=c:\Prora~1\Java\. The easiest solution, however, is to install JDK in a non-space directory, for example: c:\java\jdk1.6.0 02.

Building in debug mode

For immediate application testing and debugging, you can build your application in debug mode and immediately install it on an emulator. In debug mode, the build tools automatically sign your application with a debug key and optimize the package with zipalign. However, you can (and should) also test your application in release mode. Debug mode simply allows you to run your application without manually signing the application.

To build in debug mode:

1. Open a command-line and navigate to the root of your project directory.

2. Use Ant to compile your project in debug mode:

ant debug

This creates your debug .apk file inside the project bin/ directory, named <y0ur_pr0ject name>-debug.apk. The file is already signed with the debug key and has been aligned withzipalign.

Each time you change a source file or resource, you must run Ant again in order to package up the latest version of the application.

To install and run your application on an emulator, see the following section about Running Your Application. Building in release mode

When you're ready to release and distribute your application to end-users, you must build your application in release mode. Once you have built in release mode, it's a good idea to perform additional testing and debugging with the final .apk.

Before you start building your application in release mode, be aware that you must sign the resulting application package with your private key, and should then align it using the zipalign tool. There are two approaches to building in release mode: build an unsigned package in release mode and then manually sign and align the package, or allow the build script to sign and align the package for you.

Build unsigned

If you build your application unsigned, then you will need to manually sign and align the package. To build an unsigned .apk in release mode:

1. Open a command-line and navigate to the root of your project directory.

2. Use Ant to compile your project in release mode:

ant release

This creates your Android application .apk file inside the project bin/ directory, named <yOUr_prOjeCt name>-


Note: The .apk file is unsigned at this point and can't be installed until signed with your private key.

Once you have created the unsigned .apk, your next step is to sign the .apk with your private key and then align it with zipalign. To complete this procedure, read Signing Your Applications.

Caution: Due to the way Ant handles input, the password that you enter during the build process will be visible. If you are concerned about your keystore and alias password being visible on screen, then you may prefer to perform the application signing manually, via Jarsigner (or a similar tool). To instead perform the signing procedure manually, buid unsigned and then continue with Signing Your Applications.

To specify your keystore and alias, open the project file (found in the root of the project directory) and add entries for and key.alias. For example: key.alias=mykeystore

Save your changes. Now you can build a signed .apk in release mode:

1. Open a command-line and navigate to the root of your project directory.

2. Use Ant to compile your project in release mode:

ant release

3. When prompted, enter you keystore and alias passwords. Caution: As described above, your password will be visible on the screen.

This creates your Android application .apk file inside the project bin/ directory, named <yOUr_prOjeCt name>-release.apk. This .apk file has been signed with the private key specified inbuild .properties and aligned with zipalign. It's ready for installation and distribution.

Once built and signed in release mode

Once you have signed your application with a private key, you can install it on an emulator or device as discussed in the following section about Running Your Application. You can also try installing it onto a device from a web server. Simply upload the signed APK to a web site, then load the .apk URL in your Android web browser to download the application and begin installation. (On your device, be sure you have enabled Settings > Applications > Unknown sources.)

When your .apk has been signed and aligned, it's ready to be distributed to end-users. Build signed and aligned

If you would like, you can configure the Android build script to automatically sign and align your application package. To do so, you must provide the path to your keystore and the name of your key alias in your project's file. With this information provided, the build script will prompt you for your keystore and alias password when you build in release mode and produce your final application package, which will be ready for distribution.

0 0

Post a comment