Maven Dependencies with Android Studio / Gradle

I am using the Gradle build system bundled with Android Studio. So far, I am able to build multi-project setups using dependencies that are stored in my project structure. I would now like to use a maven dependency, to no avail. I have written a very simple build.gradle file that always fails :

    buildscript {
        repositories {
            maven { url 'http://repo1.maven.org/maven2' }
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:0.4'
        }
    }
    apply plugin: 'android'

    dependencies {
        compile 'com.google.android:support-v4:r7'
    }

    android {
        compileSdkVersion 17
        buildToolsVersion "17.0.0"

        defaultConfig {
            minSdkVersion 7
            targetSdkVersion 16
        }
    }

with the following message :

    * What went wrong:
        A problem occurred configuring project ':absabs'.
        > Failed to notify project evaluation listener.
           > Could not resolve all dependencies for configuration ':absabs:compile'.
              > Could not find com.google.android:support-v4:r7.
                Required by:
                    absabs:absabs:unspecified
....
Caused by: org.gradle.api.internal.artifacts.ivyservice.ModuleVersionNotFoundException: Could not find com.google.android:support-v4:r7.

It happens with any artifact I have tried so far. Any idea of what's wrong ?

Thanks


The "repositories" block in the buildscript section only applies to the build environment. You also need to specify which repository to use when resolving dependencies for building your project, so you need to put something like the following in your build.gradle file:

repositories {
    mavenCentral()
}

Your complete file would look something like this:

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.google.android:support-v4:r7'
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}

Note the two instances of "repositories". You can read more about what this actually means in the gradle docs.


Seems that current Android Studio version doesn't pick up new dependencies immediately. Try to restart IDE.

Edit: This is not needed for Android Studio >= 0.1.4v. It has build in action Sync Project with Gradle file. You can find it under Tools > Android > Sync Project with Gradle file or just button in Toolbar.


http://tools.android.com/tech-docs/new-build-system/user-guide

Note: This only affects the code running the build, not the project. The project itself needs to declare its own repositories and dependencies. This will be covered later.

So you have to declare

  repositories {
     mavenCentral()
  }

in your project scope once more.


I did it this way:

In Top build.gradle (Project: YourProject) I added:

 buildscript {
     repositories {
         mavenCentral()
     }
     dependencies {
         classpath 'com.h2database:h2:1.4.187'
         //NOTE: you can get the latest version in http://mvnrepository.com/artifact/com.h2database/h2

         } 
 }

allprojects {
    repositories {
        jcenter()
        mavenCentral()
    }
}

NOTE: I added this along with the predefined jcenter() repositories.

And then for my app/build.gradle file I added whichever library or dependency I needed on:

dependencies {
....//Add dependency here
}

I recently had to use a maven dependency in gradle, maybe this little example will be of use for someone.

In maven:

<dependency>
    <groupId>com.turo</groupId>
    <artifactId>pushy</artifactId>
    <version>0.12.1</version>
</dependency>

In grade that turns into:

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.turo:pushy:0.12.1'
}