48

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

Guillaume
  • 1,051
  • 1
  • 11
  • 15

6 Answers6

70

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.

ZoFreX
  • 8,812
  • 5
  • 31
  • 51
Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • Could you expound on that, because I am not sure what you mean? – arinte Jun 18 '13 at 17:18
  • 2
    guys, i had similar problem. crucial part here is double repositories declaration one inside buildscript another outside of it. – pelotasplus Jul 03 '13 at 16:09
  • @peter-niederwieser Do you mind taking a look at [this question](http://stackoverflow.com/questions/19245855/noclassdeffounderror-using-jackson-2-2-x-on-android-with-gradle)? – JJD Oct 09 '13 at 14:56
  • 1
    I'm new to gradle and really annoyed with where to put what keyword, can't find it in their documentation... Is there any sample build script that contains all the keywords in the correct structure? Thanks – Zennichimaro Jun 27 '14 at 10:22
  • like for example, the doc for AndroidManifest lists all the available keywords and possible value very nicely, it is so easy for even beginners to search: http://developer.android.com/guide/topics/manifest/manifest-intro.html – Zennichimaro Jun 27 '14 at 10:23
  • Is `mavenCentral()` some predefined function in Android? – Kenny Worden Jul 03 '17 at 16:44
13

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.

user123
  • 1,053
  • 15
  • 27
  • +1 for this info. After restarting the IDE I was able to import the classes referred in dependencies. – Andy Res Jun 21 '13 at 08:02
  • this was baffling me for ages as `build.gradle` in my project was fine. Restarting IDE sorted it. Thanks – samael Oct 19 '13 at 17:07
7

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.

log1000
  • 587
  • 5
  • 17
4

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
}
mangu23
  • 884
  • 9
  • 13
4

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'
}
algrid
  • 5,600
  • 3
  • 34
  • 37
0

maven Maven continues using XML as the format to write build specification. However, structure is diametrically different. Maven has its own problems. Dependencies management does not handle well conflicts between different versions of the same library (something Ivy is much better at). XML as the build configuration format is strictly structured and highly standardized. Customization of targets (goals) is hard. Since Maven is focused mostly on dependency management, complex, customized build scripts are actually harder to write in Maven than in gradel.

Krishna Avadhanam
  • 98
  • 1
  • 2
  • 10