September 15, 2012

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="" xmlns:xsi=""

<name>Gridlayout compatibility library</name>





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


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


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


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


Last Step is mvn install.
comments powered by Disqus