Wednesday, September 25, 2013

Maven Central's 'artifactid' is build.gradle's 'name' property

Recently I wanted to utilize a third party library in one of my Android applications. The library is available via Maven Central, and since I'm now using Android Studio for development I thought I'd use gradle to auto-import the library. I knew this was possible and relatively straightforward, but I ran into something that threw me for a loop and that I couldn't find documented officially anywhere. I figured I'd share what I found to hopefully save someone else the potential hassle.

At the bottom of each Android app's build.gradle file is the section where you reference third party libraries that will automatically come from Maven Central. For most Android apps the support library is included:
dependencies {
    compile ''
    //This is where references to the third party libs go
To get a library to work in gradle from Maven, it needs 3 pieces of information. For every library, Maven central provides 3 primary pieces of information. Respectively, the fields for each are:
//Gradle Info:
group: 'com.example.library'  //The package name for the library
name: 'someuniquename'        //An identifier for the lib
version: 'X.Y.Z'              //lib version number

//Maven Info:
groupId: 'com.example.library'  //The package name for the library
artifactId: 'someuniquename'    //An identifier for the lib
version: 'X.Y.Z'
It seemed pretty obvious that "group" and "groupId" would line up in both places. But here was my gotcha point: when referencing the maven library, the "artifactId" value from Maven is what you use for gradle's "name" property.

Like I said, I couldn't find this documented anywhere. On a different project previously I attempted to bring in a Maven library and it didn't go so well. It could have been due to a buggier, earlier version of Android Studio, but not knowing this bit of info certainly didn't help.

When you bring it all together, this is what the build.gradle file will look like:
dependencies {
    compile ''
    compile group: 'com.example.library', name: 'someuniquename', version: 'X.Y.Z'

No comments:

Post a Comment