Declaring dependencies

Now, we will see how to insert and use third-party libraries easily. For our scope, we will use a very simple and functional library, which is called RoboGuice 2, and which allows us to inject our view, resource, system service, or any other object into our activity (we call this activity RoboActivity).

To do this, we have to modify the MainActivity.java class. This class contains the following code:

package com.androidmavenproject;

import roboguice.activity.RoboActivity;
import roboguice.inject.ContentView;
import roboguice.inject.InjectView;
import android.os.Bundle;
import android.widget.TextView;

@ContentView(R.layout.activity_main)
public class MainActivity extends RoboActivity {

  @InjectView(R.id.text_view)
  TextView name;

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    name.setText("Hello world!");
  }
}

We take advantage of the functionalities offered by this library to set the content view and inject the TextView instance.

The next step is to insert the dependent library into the pom.xml file:

[…]
<dependency>
  <groupId>org.roboguice</groupId>
  <artifactId>roboguice</artifactId>
  <version>2.0</version>
</dependency>
[…]

A compatibility library for API v4

Another common add-on is the compatibility library for API v4 and higher. Obviously, we need to include the library in the final package (.apk), and this does not have the provided scope.

Tip

If you generate a project with the Eclipse wizard, make sure that no support for library v4 is automatically included into the libs folder; if it is, delete it.

The next step is to include support-v4 in the pom.xml file:

[…]
<dependency>
  <groupId>com.google.android</groupId>
  <artifactId>support-v4</artifactId>
  <version>r7</version>
</dependency>
[…]

Finally, modify the MainActivity class to use android.support.v4.app.FragmentActivity with the following code:

package com.androidmavenproject;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }

  @Override
  public void onStart() {
    super.onStart();
    TextView textView = (TextView) findViewById(R.id.text_view);
    textView.setText("Hello world!");
  }
}

The final POM file with dependencies

We are ready to run our application with dependencies. First, we need to resolve the dependencies; for this, run the following command:

$ mvn package

The following is the code of the final pom.xml file:

<?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.0http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.androidmavenproject</groupId>
  <artifactId>android-maven-project</artifactId>
  <version>0.1.0</version>
  <packaging>apk</packaging>
  <name>Android Maven project</name>
  <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>
    <dependency>
      <groupId>org.roboguice</groupId>
      <artifactId>roboguice</artifactId>
      <version>2.0</version>
    </dependency>
    <dependency>
      <groupId>com.google.android</groupId>
      <artifactId>support-v4</artifactId>
      <version>r7</version>
    </dependency>
  </dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <groupId>com.jayway.maven.plugins.android.generation2</groupId>
        <artifactId>android-maven-plugin</artifactId>
        <version>3.9.0-rc.2</version>
        <extensions>true</extensions>
        <configuration>
          <sdk>
            <path>${env.ANDROID_HOME}</path>
            <platform>19</platform>
          </sdk>
          <deleteConflictingFiles>true</deleteConflictingFiles>
          <undeployBeforeDeploy>true</undeployBeforeDeploy>
        </configuration>
      </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>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset