“I always say, 'This will be my last 'Metal Gear.'” Hideo Kojima

HOWTO add a library as a dependency with gradle

Hi again!

This is a quick post about adding libraries with Gradle. Time ago, I was using git submodules because I thought they were very useful in case you need to do little changes to the library. But it was a pain, period.

So, I decided to use packaged libraries. In a build.gradle file, you can add dependencies from different sources: from local files, Maven, Ivy, etc. I choose Maven because it seems to be the most used system. Also, because it is what HoloEverywhere uses :P

I also use Maven!

In Gradle, you can define HoloEverywhere dependencies like this:


    dependencies {
        compile 'com.android.support:support-v4:18.0.+@jar'
        compile 'com.nineoldandroids:library:2.4.+@jar'
        compile 'org.holoeverywhere:library:2.1.+@aar'
        compile 'org.holoeverywhere:addon-preferences:2.1.+@aar'
    }
Don't forget to add support-v4 and nineoldandroids because app won't build and/ or will crash without them.

You can define a particular version (2.1.0 instead of 2.1.+) if you want. But, first of all, you need to define which Maven repos you need. This can be done like this:

    repositories {
        mavenCentral()
        maven {
            url "https://github.com/Prototik/HoloEverywhere/raw/repo/"
        }
    }

If you can do with Maven Central just use mavenCentral(), but if you are using HoloEverywhere (or any other library that uses a custom repo) you MUST define its custom repo using maven{}.

I prefer using local libs!

That’s also nice. Gradle let you do this without any trouble. Your repositories {} would look like this:

    repositories {
        flatDir {
            dirs 'lib'
        }
    }

Then, you can declare dependencies like this:

This has not been tested by me.
    dependencies {
        compile 'support-v4:18.0.4'
    }

Gradle will look for support-v4-18.0.4.jar on your lib folder.

That’s it. It should work like this. Nice coding! :)

Jordi.

See also: