My little Android warehouse

Random thoughts about my experience as moonlight android developer.

Gridlayout Support Library With Maven


EDIT 17/10/2012:
Gridlayout support is now working natively with maven android deployer. I strongly suggest to use it instead of my method.

---------------------------


Gridlayout has been introduced since api level 14, and is being suggested layout as a more efficient alternative over nested linear layouts and relative layout.

It has also been recently added as part of the android support library, in order to have it available on older devices.

Trying to use it in a maven setup is a bit tricky.
At the time of writing, the version added by maven android deployer won't work because of this issue .
The problem is, you can't just add the jar because the library is intended to be an android project library, which means that brings with itself a couple of resources.


The error you might get is

java.lang.NoClassDefFoundError: android/support/v7/gridlayout/R$dimen

I don't know if the following is the best "mavenic" way to solve the problem, however it worked for me and I am sharing it here.

What you need to do is to build your own apk lib to add to your maven repo to link against.

Step 1: building an apk lib

The information you can find on android maven plugin website wasn't very clear to me. However they ship a couple of examples you can take inspiration from.


Assuming that you have a proper ANDROID_HOME variable set up, and you already used maven android deployer to have a working setup, the first thing you have to do is to download the support library. 
You will find it under $ANDROID_HOME/extras/android/support/v7/gridlayout

Make your own copy somewhere else in your hard disk.


Now copy my 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.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>


<version>1.0.0-SNAPSHOT</version>
<groupId>com.support.gridlayout</groupId>
<artifactId>gridlayout_compat</artifactId>
<packaging>apklib</packaging>
<name>Gridlayout compatibility library</name>

<dependencies>
<dependency>
<groupId>android</groupId>
<artifactId>android</artifactId>
<scope>provided</scope>
<version>4.0.3_r3</version>
</dependency>

<dependency>
<groupId>com.grid</groupId>
<artifactId>gridlayout</artifactId>
<version>1.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<version>3.1.1</version>
<artifactId>android-maven-plugin</artifactId>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>repo</id>
<releases>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<url>file://${project.basedir}/repo</url>
</repository>
</repositories>

</project>


You will have another thing to do. Since you cannot include jar files directly but you have to specify them as depedencies, you can:
  • refer to the jar provided by maven android deployer
  • have a dependency pointing a local repo, as described here

I chose the second approach.

As you can see in the lower part of the pom file, I add a local repository.
You need to put the jar file in that repository. As suggested by the stackoverflow answer, you can let maven build it for you with

mvn install:install-file -DlocalRepositoryPath=repo -DcreateChecksum=true -Dpackaging=jar -Dfile=libs/android-support-v7-gridlayout.jar -DgroupId=com.grid -DartifactId=gridlayout -Dversion=1.0

Step 2: Including the apklib to your project


Now you have your apklib ready to use. You can add it as a dependency in the pom.xml of your app


       <dependency>
<groupId>com.support.gridlayout</groupId>
<artifactId>gridlayout_compat</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>apklib</type>
</dependency>



This costed me a couple of sleepless nights figuring out why it was not working...

Edit:

As markus pointed out in his comment, I forgot the last trivial step which is to run
mvn install 




Epilogue:

Please read the README file included in the gridlayout support library BEFORE trying to use it. It will save you a bit of extra headache.


If you liked this post, you might be interested in following me on twitter @fedepaol




Comments

Markus
Last Step is mvn install.