Giter Site home page Giter Site logo

dilinirajapaksha / gs-maven-android Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hkarakose/gs-maven-android

0.0 2.0 0.0 327 KB

Building Android Projects with Maven :: Learn how to build an Android project with Maven.

Home Page: https://spring.io/guides/gs/maven-android/

gs-maven-android's Introduction

This guide walks you through the process of building a simple Android project with Maven.

What you’ll build

You’ll create an Android application that gives you the time of day, and then build it with Maven.

What you’ll need

  • About 15 minutes

  • A favorite text editor or IDE

  • Android SDK

  • An Android device or Emulator

Set up the project

First, you set up an Android project for Maven to build. To keep the focus on Maven, make the project as simple as possible for now. If this is your first time working with Android projects, refer to Installing the Android Development Environment to help configure your development environment.

AndroidManifest.xml

link:initial/AndroidManifest.xml[role=include]

Add a text string. Text strings can be referenced from the application or from other resource files.

res/values/strings.xml

link:initial/res/values/strings.xml[role=include]

Here you define the visual structure for the user interface of your application.

res/layout/hello_layout.xml

link:initial/res/layout/hello_layout.xml[role=include]

Within the src/main/java/org/hello directory, you can create any Java classes you want. To maintain consistency with the rest of this guide, create the following class:

src/main/java/org/hello/HelloActivity.java

link:initial/src/main/java/org/hello/HelloActivity.java[role=include]

Install Maven

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.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 08:51:28-0500)
Maven home: /usr/local/apache-maven/apache-maven-3.0.5
Java version: 1.7.0_21, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.8.3", arch: "x86_64", family: "mac"

You now have Maven installed.

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

link:initial/pom.xml[role=include]

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.

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

Declare dependencies

The simple Hello World sample is completely self-contained and does not depend on any additional libraries. Most applications, however, depend on external libraries to handle common and/or complex functionality.

For example, suppose you want your application to print the current date and time. Although you could use the date and time facilities in the native Java libraries, you can make things more interesting by using the Joda Time libraries.

To do this, modify HelloActivity.java to look like this:

src/main/java/org/hello/HelloActivity.java

link:complete/src/main/java/org/hello/HelloActivity.java[role=include]

In this example, you use Joda Time’s LocalTime class to retrieve and display the current time.

If you were to run mvn package to build the project now, the build would fail because you have not declared Joda Time as a compile dependency in the build. You can fix that by adding the following lines to <dependencies> section of the pom.xml:

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.2</version>
</dependency>

Similar to the Android dependency discussed earlier, this block of XML declares a new dependency for the project, specifically the Joda Time library.

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.

Here’s the completed pom.xml file:

pom.xml

link:initial/pom.xml[role=include]

Summary

Congratulations! You have created a simple yet effective Maven project definition for building Java projects.

gs-maven-android's People

Contributors

royclarkson avatar gregturn avatar cbeams avatar habuma avatar btalbott avatar

Watchers

James Cloos avatar Dilini Rajapaksha avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.