137

I added a custom maven repository to build.gradle in Android Studio but the dependency is not being found

Maven repository and dependency

<repository>
    <id>achartengine</id>
    <name>Public AChartEngine repository</name>
    <url>https://repository-achartengine.forge.cloudbees.com/snapshot/</url>
</repository>

<dependency>
    <groupId>org.achartengine</groupId>
    <artifactId>achartengine</artifactId>
    <version>1.2.0</version>
</dependency>

build.gradle

buildscript {
    repositories {
        mavenCentral()
        maven {
            url "https://repository-achartengine.forge.cloudbees.com/snapshot/"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.6.+'
    }
}
apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile group: 'org.achartengine', name: 'achartengine', version: '1.2.0'
}

android {
    compileSdkVersion 19
    buildToolsVersion "19"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

Error message in Android Studio:

A problem occurred configuring root project 'My-MobileAndroid'.
> Failed to notify project evaluation listener.
> Could not resolve all dependencies for configuration ':_DebugCompile'.
  > Could not find org.achartengine:achartengine:1.2.0.
    Required by:
        :My-MobileAndroid:unspecified

What am I missing in build.gradle?

Mahozad
  • 18,032
  • 13
  • 118
  • 133
André Ricardo
  • 3,051
  • 7
  • 23
  • 32

6 Answers6

173

After

apply plugin: 'com.android.application'

You should add this:

  repositories {
        mavenCentral()
        maven {
            url "https://repository-achartengine.forge.cloudbees.com/snapshot/"
        }
    }

@Benjamin explained the reason.

If you have a maven with authentication you can use:

repositories {
            mavenCentral()
            maven {
               credentials {
                   username xxx
                   password xxx
               }
               url    'http://mymaven/xxxx/repositories/releases/'
            }
}

It is important the order.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 6
    How do we add cloudbees authentication in the gradle file? – inder Dec 31 '14 at 00:06
  • How can I modify Android Studio so that the maven repository is added to build.gradle by default. I keep forgetting about this and have to rediscover why libraries are not loading. – Mike Sep 12 '17 at 01:38
93

You will need to define the repository outside of buildscript. The buildscript configuration block only sets up the repositories and dependencies for the classpath of your build script but not your application.

Benjamin Muschko
  • 32,442
  • 9
  • 61
  • 82
40

Android Studio Users:

If you want to use grade, go to http://search.maven.org/ and search for your maven repo. Then, click on the "latest version" and in the details page on the bottom left you will see "Gradle" where you can then copy/paste that link into your app's build.gradle.

enter image description here

Micro
  • 10,303
  • 14
  • 82
  • 120
  • 1
    You made my day, thank you very much. Also, why this isn't in the official ZXing gh page? It shouldn't be so hard to find... – Nadia Castelli Apr 15 '17 at 17:05
9

You have to add repositories to your build file. For maven repositories you have to prefix repository name with maven{}

repositories {
  maven { url "http://maven.springframework.org/release" }
  maven { url "http://maven.restlet.org" }
  mavenCentral()
}
Marcin Szymczak
  • 11,199
  • 5
  • 55
  • 63
8

Add the maven repository outside the buildscript configuration block of your main build.gradle file as follows:

repositories {
        maven {
            url "https://github.com/jitsi/jitsi-maven-repository/raw/master/releases"
        }
    }

Make sure that you add them after the following:

apply plugin: 'com.android.application'
7

This is for Kotlin DSL (build.gradle.kts):

repositories {
    mavenCentral()
    maven("https://jitpack.io")
    // OR another notation:
    // maven {
    //     url = uri("https://jitpack.io")
    // }
}

You can also centralize your repository declaration for all your subprojects in settings.gradle[.kts] file in newer versions of Gradle (Gradle 7.4):

dependencyResolutionManagement {
    repositories {
        mavenCentral()
        maven("https://jitpack.io")
    }
}

See the full guide here in Gradle documentation website.

Mahozad
  • 18,032
  • 13
  • 118
  • 133