Building Android Application

Build variants are specific builds that, you can produce from Gradle, based around shared core source code. While a standard app may have a debug and release build type, you can expand on this, by adding flavor dimensions. Flavor dimensions allow you to use the folder structure of your project, to change how individual builds look and act for your users. This enables developers to easily produce multiple similar apps, with different styles and configuration.

Each build that you create, will need a unique application-Id, in order to differentiate it from the other builds. Since any application that you build, will have at least one flavor from each dimension, you only need to define the application-Id in each node for one dimension.

Android application can be build by using the following ways,

  1. Build the app using Android Studio.
  2. Build by using command line
  3. Build by using Maven
  1. Building a project using Android Studio

a. Create a new project.

new project

b. Click Next

c. Project will be opened like this.

target android devices

d. Click Run icon in the tool bar  (or) choose in the menu – Run->Run”app”->Click to build project.

3

run

e. Get APK file in the project root directory:

apk

2. Building and Running Project using Command Line

Reference:

http://developer.android.com/tools/building/building-cmdline.html

Building in debug mode:

a. Open->Terminal->Open your project directory

6

b. Use Gradle command to build the project:

$ chmod +x gradlew
$ ./gradlew assembleDebug

7

c.Show the tasks:

./gradlew tasks

Building in release mode:

a. Open->Terminal->Open your project directory

8

b. Run the below command

$ ./gradlew assembleRelease

9

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

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

c. Build signed and aligned

i. Open a command-line and navigate to the root of your module directory.

ii. Edit the build.gradle file to build your project in release mode:

android {
    …
    defaultConfig { … }
    signingConfigs {
      release {
        storeFile file(“myreleasekey.keystore”)
        storePassword “password”
        keyAlias “MyReleaseKey”
        keyPassword “password”
      }
    }
    buildTypes {
      release {
        …
        signingConfig signingConfigs.release
      }
    }

iii. When prompted, enter you keystore and alias passwords.

Caution:  As described above, your password will be visible on the screen.

4. This creates your Android application .apk file inside the module build/ directory, named <your_module_name>-release.apk. This .apk file has been signed with the private key specified in build.gradle file and aligned with zipalign. It’s ready for installation and distribution.

3. Build Application using Maven

The android-maven-plugin plug-in allows to build Android applications via Maven. In most cases, development of Android applications is done within the Android studio based Android Development Toolkit ADT. The optionally generated Apache Ant based build can be used to build applications outside the IDE. The Android Maven Plugin was created to allow development teams to build, deploy and release Android applications with Apache Maven, taking advantage of all the powerful features available like dependency management, reporting, code analysis and much more.

Following steps are involved in maven build

a. Installing Maven in the system.

b. Define a simple Maven build

c. Build Android code

d. Rebuild Android code with dependencies

a. Install Maven in the system

Now you have a project that you can build with Maven. The next step is to install Maven.

Maven is downloadable as a zip file at http://maven.apache.org/download.cgi. Only the binaries are required, so look for the link to apache-maven-{version}-bin.zip or apache-maven-{version}-bin.tar.gz.

Download and unzip the file, then add the bin folder to your path.
To test the Maven installation, run mvn from the command-line:
mvn -v
If all goes well, you should see installation information like this:

Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T11:37:52-06:00)
Maven home: /usr/local/apache-maven/ apache-maven-3.2.1
Java version: 1.8.0, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents /Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: “mac os x”, version: “10.9.2”, arch: “x86_64”, family: “mac”

You now have Maven installed.

b. Define a simple Maven build

Now that Maven is installed, you need to create a Maven project definition. You define Maven projects with an XML file named pom.xml. Among other things, this file gives the project’s name, version, and dependencies that it has on external libraries.

Create a file named pom.xml at the root of the project and give it the following contents:

pom.xml
<?xml version=“1.0” encoding=“UTF-8”?>
<project xmlns= “http://maven.apache.org/ POM/ 4.0.0” xmlns:xsi= “http://www.w3.org /2001 / XMLSchema-instance”
xsi:schemaLocation= “http://maven.apache.org / POM /4.0.0 http: // maven.apache.org / maven-v4_0_0.xsd”>
<modelVersion>4.0.0</modelVersion>>
  <groupId>org.hello</groupId>
  <artifactId>gs-maven-android</artifactId>
  <version>0.1.0</version>
  <packaging>apk</packaging>
  <properties>
    <!– use UTF-8 for everything –>
    <project.build. sourceEncoding> UTF-8 </ project.build. sourceEncoding>
    <project.reporting. outputEncoding> UTF-8 </ project.reporting. outputEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>com.google. android </groupId>
      <artifactId>android</artifactId>
      <version>4.1.1.4</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId> com.jayway.maven. plugins.android. generation2 </groupId>
        <artifactId> android-maven-plugin </artifactId>
        <version>3.9.0-rc.1</version>
        <configuration>
          <sdk>
            <platform>19</platform>
          </sdk>
          <deleteConflictingFiles> true </deleteConflictingFiles>
          <undeployBeforeDeploy> true </undeployBeforeDeploy>
        </configuration>
        <extensions> true </extensions>
      </plugin>
      <plugin>
        <artifactId> maven-compiler-plugin </artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

The <packaging> element specifies an apk. This is the simplest possible pom.xml file necessary to build an Android project. It includes the following details of the project configuration:

<modelVersion>. POM model version (always 4.0.0).
<groupId>. Group or organization that the project belongs to. Often expressed as an inverted domain name.
<artifactId>. Name to be given to the project’s library artifact (for example, the name of its APK file).
<version>. Version of the project that is being built.
<packaging>. How the project should be packaged, in this case as an Android APK.

The <dependencies> section declares a list of dependencies for the project. Specifically, it declares a single dependency for the Android library. Within the <dependency> element, the dependency coordinates are defined by three subelements:

<groupId>. Group or organization that the dependency belongs to.
<artifactId>. Library that is required.
<version>. Specific version of the library that is required.
<scope>. Scoped as compile dependencies by default. That is, all dependencies should be available at compile-time.

In this case, the <scope> element has a value of provided. Dependencies of this type are required for compiling the project code, but will be provided at runtime by a container running the code. For example, the Android APIs are always available when an Android application is running.

The <build> section declares additional configuration for building an application. Within the build section is a <plugins> section, which contains a list of plugins that add additional functionality to the build process. This is where you define the configuration for the Android Maven Plugin. As with dependencies, plugins also have <groupId>, <artifactId>, and <version> elements, and they behave as previously described. The plugin declaration also has these elements:

<configuration>. Plugin-specific configuration. Here you specify which Android Platform SDK to use in the build.
<extensions>. Combination of specifying a value of true and apk for <packaging> directs the [Android Maven Plugin] to become involved in the build process.

At this point you have defined a minimal yet capable Maven project.

c. Build Android code

Maven is now ready to build the project. You can execute several build lifecycle goals with Maven now, including goals to compile the project’s code, create a library package (such as a JAR file), and install the library in the local Maven dependency repository.

Try out the build:

mvn compile

This command runs Maven, telling it to execute the compile goal. When it’s finished, you should find the compiled .class files in the target/classes directory.

Because it’s unlikely that you’ll want to distribute or work with .class files directly, you’ll probably want to run the package goal instead:

mvn package

The package goal compiles your Java code, runs any tests, and packages the code in a JAR file within the target directory. The name of the JAR file is based on the project’s <artifactId> and <version>. For example, given the minimal pom.xml file shown earlier, the JAR file will be named gs-maven-android-0.1.0.jar.

Because you set the value of <packaging> to “apk”, the result will be an APK file within the target directory in addition to the JAR file. This APK file is now a packaged Android application ready to be deployed to a device or emulator.

The Android Maven plugin provides several more Maven goals that you can use to initiate the various phases of the build process, or interact with the device and emulator. You can see a list of all the available goals by running the following command:

mvn android:help

d. Rebuild Android code with dependencies

Now if you run mvn compile or mvn package, Maven should resolve the Joda Time dependency from the Maven Central repository and the build will be successful if we added as a dependency.

Here’s the completed pom.xml file:

pom.xml
<?xml version=“1.0” encoding=“UTF-8”?>
<project xmlns= “http://maven.apache.org/ POM/ 4.0.0” xmlns:xsi= “http://www.w3.org /2001 / XMLSchema-instance”
xsi:schemaLocation= “http://maven.apache.org / POM /4.0.0 http: // maven.apache.org / maven-v4_0_0.xsd”>
<modelVersion>4.0.0</modelVersion>>
  <groupId>org.hello</groupId>
  <artifactId>gs-maven-android</artifactId>
  <version>0.1.0</version>
  <packaging>apk</packaging>
  <properties>
    <!– use UTF-8 for everything –>
    <project.build. sourceEncoding> UTF-8 </ project.build. sourceEncoding>
    <project.reporting. outputEncoding> UTF-8 </ project.reporting. outputEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>com.google. android </groupId>
      <artifactId>android</artifactId>
      <version>4.1.1.4</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId> com.jayway.maven. plugins.android. generation2 </groupId>
        <artifactId> android-maven-plugin </artifactId>
        <version>3.9.0-rc.1</version>
        <configuration>
          <sdk>
            <platform>19</platform>
          </sdk>
          <deleteConflictingFiles> true </deleteConflictingFiles>
          <undeployBeforeDeploy> true </undeployBeforeDeploy>
        </configuration>
        <extensions> true </extensions>
      </plugin>
      <plugin>
        <artifactId> maven-compiler-plugin </artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Hope this information is useful.

Android Team,
Mallow Technologies.

Leave a Comment

Your email address will not be published. Required fields are marked *