How to Include a Maven Project in Your Android Workspace
By Surender Mohan, Principal Software Architect, AT&T SDK Team
Say you have an existing Android application built using the Android Development Tool (ADT) and you want to integrate a Maven-based java library in your Android application. For the sake of this example we will use AT&T’s Java Codekit API library to illustrate this kind of integration. This article assumes that you know how to install Maven in your development environment using the information available here.
For reference, here’s the development environment used to verify the steps:
- Operating System: Mac OS X 10.9.2, Arch x86_64
- ADT/Eclipse: Android Developer Tools Build: v22.6.2-1085508
- Maven: Apache Maven 3.2.1
- Java: 1.6.0_65, vendor: Apple Inc.
Java Codekit library is a Maven project. You have access to the Maven project files in this Github project. To get started, download the Java Codekit source code from Github and compile the Maven library and then include it in a blank Android Application project.
Step 1: Adding Maven library as “External Jar” to an Android application
1.1 Create a new Android Application project and call it “AndroidMavenSample”:
1.2 Click “Next” three times accepting all the defaults, and then click “Finish” to create a new project called “AndroidMavenSample.”
1.3 Right click on the new “AndroidMavenSample” project and select “Properties.”
1.4 Click on “Java Build Path” in the left pane and then select the “Libraries” tab.
1.5 Click on “Add External JARs …” and navigate to the appropriate codekit folder to include the codekit-1.0.jar file that you created from the codekit Maven project. (NOTE: This step is optional if you want to follow all the way through to Step 3).
1.6 Recompile and run the project. Now you are ready to use the AT&T APIs in your Android application using the Java Codekit. You can look at the codekit examples to learn about how to use the AT&T APIs.
You can use the library calls by including the codekit jar file and include it in your Android application using the steps above. Now, if you want to debug the maven library that you included above in your project, you can follow the following steps to add a Maven project in your workspace. You can install the m2eclipse plugin, which provides Maven integration in Eclipse IDE. m2eclipse provides first-class Apache Maven support in the Eclipse/ADT IDE, making it easier to edit Maven’s pom.xml, run a build from the IDE and much more.
Step 2: Adding Maven project to the Android Workspace
2.1 Start Eclipse (ADT) then go to: Help -> Install New Software…
2.2 Copy this link for the latest Stable Release into Eclipse and hit Enter.
2.3 When the site loads, select the features to install, or click the Select All button. For our requirement select “Maven Integration for Eclipse.”
NOTE: Checking [x] Contact all update sites during install to find required software might take some time and is optional.
2.4 Click Next to view Installation Details.
2.5 Click Next to agree to the license terms, and click Finish.
2.6 If you get any warning message when installing, click OK to continue.
2.7 It will take a few minutes to install the Maven plugin and once it’s done restart the Eclipse.
2.8 After installing the Maven plugin for Eclipse, you can check if the installation is successful by trying to Import a new Maven project into your workspace. You should see a Maven folder in the Import project wizard as shown below.
2.9 Navigate to the codekit maven project that you downloaded (or “cloned”) from the Github link and add that.
After you have imported the maven codekit project, you can change the dependency of your Android project from including the library (jar file) to using the library as a project.
Step 3: Adding Maven project as a Reference in the Android Application
3.1 Go to the project properties dialog and select “Java Build Path” and remove the codekit-1.0.jar file dependency from your project after selecting the file pressing the “Remove” button. (This step is only relevant for this tutorial.)
3.2 Open the “Project References” tab and check the box for the “codekit” Maven project added to your workspace in step 2.9 above to add that as the dependency.
Now you are ready to set breakpoints in the Maven codekit library for troubleshooting purpose.
Let us know if you have any questions about this process or integrating a Maven-based java library in your Android app in the comment section below.