Action Bar Sherlock, Maven, Eclipse integration

Hello, today I’d like to describe a process of integration commonly used library for emulation action bar in old Android versions - ActionBarSherlock into maven - Eclipse project. In a result we’ll get a project with:

support of Action Bar Sherlock

support of integration tests as a separate project

configured Maven build and release targets

configured Eclipse for develop and debug

This is my first post, so don’t be too critical :)

At first let’s create empty Android mvn project. In order to complete this step we’ll need:

At first let’s create Android project using akquinet android-archetypes. In a result, we’ll get a sample project with configured release targets and integration tests project. Copy following code to the terminal, replacing highligted lines with your values:

mvn archetype:generate \

-DarchetypeArtifactId=android-release \

-DarchetypeGroupId=de.akquinet.android.archetypes \

-DarchetypeVersion=1.0.8 \

-DgroupId=com.foo.bar \

-DartifactId=my-android-project \

-Dpackage=com.foo.bar.test \

-Dplatform=16

Once generated, the application is ready to be built and tested. Start an Android emulator, or plug an Android dev phone, and execute:

cd my-android-project

mvn clean install

Besides your application, this commands will also install and execute integrations tests application.

Generated project goes with test keystore that could be used for test, but not for deploying to market, run this command to get signed apk:

Latest version of the library always is available in the project download page. When it’s ready, unzip archive, copy library folder to your project root and rename it to actionbarsherlock:

As it is stated on abs usage page the only thing we need - is to add this lines to my-android-project/my-android-project/pom.xml :

<dependency>

<groupId>com.actionbarsherlock</groupId>

<artifactId>actionbarsherlock</artifactId>

<version>4.2.0</version>

<type>apklib</type>

</dependency>

Also you need to extend HelloAndroidActivity from com.actionbarsherlock.app.SherlockActivity instead of android.app.Activity and change activity theme in AndroidManifest.xml to android:theme="@style/Theme.Sherlock". Checkout git patch file for details. Try to build app. From parent project folder execute:

If you carefully take a look at the last error, you’ll notice that a problem is with generation2 goal. It uses dex tool from Android SDK to generate class files for Dalvik. During the execution, it includes actionbarsherlock and support-v4-r7 dependencies two times (second time like transitive dependency from our application project). Check out it --output parameter values (here is a bit formatted text from maven error):

We can fix this error by adding actionbarsherlock and support-v4-r7 dependencies with scope provided to my-android-project-it/pom.xml:

<dependency>

<groupId>com.google.android</groupId>

<artifactId>support-v4</artifactId>

<version>r7</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>com.actionbarsherlock</groupId>

<artifactId>actionbarsherlock</artifactId>

<version>4.2.0</version>

<type>apklib</type>

<scope>provided</scope>

</dependency>

Now project should build successfully.

Next step is to configurate eclipse. Rename parent folder of a project from my-android-project to my-android-project-parent.

Open Eclipse, import my-android-project-parent using existing maven project wizard. As actionbarsherlock is not part of our parent maven project you’ll also need to import it as existing maven project (don't import it as Android library project!). After this steps you should have following project structure:

Let’s fix build path problems that we have.

Go to my-android-project properties

Open Android section

Add actionbarsherlock as a library by pressing Add button

Now you should see actionbarsherlock in a library section

As we are using separate project for integration test, we should remove src/test/java folder from my-android-project and my-android-project-it build path:

Go to my-android-project (or my-android-project-it) properties

Open Java Build Path section

Under Sources tab select src/test/java folder

Press remove button

Now all errors from Eclipse markers view should disappear, and we can run our project. Select my-android-project and run it as Android application… but you’ll get:

Remember our problem with maven configuration? Here we have quite the same issue with double inclusion. In order to fix it remove libs folder with android-support-v4.jar from actionbarsherlock project.

Finally, clean all projects and run my-android-project. We’ve got working application:

About

Hi, my name is Vitaliy. My background includes mobile application and game development, tech leadership and conference organization. Nowadays, I'm leading development of the next generation in-app marketing tools at the Dutch startup GetSocial. I'm a frequent speaker at conferences, an active member of various technical communities and a founder of the Google Developers Group Lviv.