While my last blog post on building Android apps from the command line was a great start, I felt like it was time to tackle some more advanced issues. Lets learn how to compile complex apps which use different libraries at runtime. (The beginning may look very familiar.)
- Latest Android SDK
- Latest Java JDK
- Apache ANT
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.
Also set the PATH VARIABLE to Android SDK
Pre Build Configuration
- Checkout the Latest Code from SVN
- Navigate to Checkout Directory using Command Line (Terminal)
- Enter the following command
<code>android update project -p <PATH TO CHKOUT DIR> -t "Google Inc.:Google APIs:<VERSION NUMBER>" -n <Name of Application> </code>
- Also include the libraries which needs to be included during compile time as below.
<code>android update project -p <PATH TO CHKOUT DIR>/libs/ <Library Directory name>/ -t "Google Inc.:Google APIs: <VERSION NUMBER>" </code>Repeat the steps for other libraries as well.
- To make sure everything is in place, use
ant clean. This will make sure build.xml is proper and all the required tools are in place
There are two ways to build your application using the Ant build script: one for testing/debugging your application — debug mode — and one for building your final package for release — release mode. We will make Release Mode build as it’s the actual file that we send out for QA and end users.
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
Before building in Release mode you must sign the app using your Private Key.
Obtaining a suitable private key
In preparation for signing your application, you must first ensure that you have a suitable private key with which to sign. A suitable private key is one that:
- Is in your possession
- Represents the personal, corporate, or organizational entity to be identified with the application
- Has a validity period that exceeds the expected lifespan of the application or application suite. A validity period of more than 25 years is recommended. To publish your application on Google Play, note that a validity period ending after October 22, 2033 is required. You cannot upload an application if it is signed with a key whose validity expires before that date.
The key may be self-signed. If you do not have a suitable key, you must generate one using Keytool.
Compile the application in release mode
To generate a self-signed key with Keytool, use the keytool command as shown below
<code>keytool -genkey -v -keystore <App-Name>.keystore -alias <Alias Name> -keyalg RSA -keysize 2048 -validity 10000 </code>
Running the above command, Keytool prompts you to provide passwords for the keystore and key, and to provide the Distinguished Name fields for your key. It then generates the keystore as a file called
<App-Name>.keystore. The keystore and key are protected by the passwords you entered. The keystore contains a single key, valid for 10000 days. The alias is a name that you will use later, to refer to this keystore when signing your application.
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 ant.properties 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.
To specify your keystore and alias, open the project ant.properties file (found in the root of the project directory) and add entries for
key.alias as below
<code>key.store=<App-Name>.keystore key.alias=Alias-Name </code>
Save your changes. Now you can build a signed
.apk in release mode
Open a command-line and navigate to the root of your project directory. Use Ant to compile your project in release mode:
<code>ant release </code>
When prompted, enter you keystore and alias passwords.
This creates your Android application
.apk file inside the project
bin/ directory, named
< Name of Application > -release.apk. This
.apk file has been signed with the private key specified in ant.properties and aligned with zipalign. It's ready for installation and distribution.
Once built and signed in release mode with a private key, you can install and run it on an emulator or device. 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.)
Adding Directories to PATH VARIABLES
To add directories to
PATH VARIABLES follow the steps below based on your OS:
- Click on Start Menu
- Right-click on Computer and select Properties
- In the System Properties window click on Advanced System Settings
- Click on
System Variableslook for
- Add the complete path to your Android-SDK as shown below
<code>C:<PATH to SDK>android-sdktools C:<PATH to SDK>android-sdkplatform-tools </code>Make sure not to remove the other path that may already exist.
- Click OK and the path will be set in environment.
LINUX & MAC OSX
- Open the Terminal application.
- Run the following lines of code
<code>echo 'export PATH=<ANDROID-SDK-PATH>/tools:$PATH' >> ~/.profile echo 'export PATH=<ANDROID-SDK-PATH>/platform-tools:$PATH' >> ~/.profile </code>
- Logout from the System and login again.
What is Keytool?
Keytool is a key and certificate management utility. It allows users to administer their own public/private key pairs and associated certificates for use in self-authentication (where the user authenticates himself/herself to other users/services) or data integrity and authentication services, using digital signatures. It also allows users to cache the public keys (in the form of certificates) of their communicating peers.
What is a keystore?
A keystore is a storage facility for cryptographic keys and certificates.
Like what you read? Join our community to get more technical information, chances to win prizes, and more: built.io/community