将Maven存储库添加到build.gradle


118

我在Android Studio中向build.gradle添加了一个自定义Maven存储库,但未找到依赖项

Maven存储库和依赖项

<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')
    }
}

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

我在build.gradle中缺少什么?

Answers:


88

您将需要在之外定义存储库buildscript。该buildscript配置仅阻止树立库和依赖关系构建脚本的类路径,但不是你的应用程序。


这样可以对依赖关系进行排序,但是Android Studio不会重新加载存储库,因此“ import org.achartengine.chart。*;” 失败了,您知道如何“刷新”存储库吗?
安德烈·里卡多·

3
非常重要。在此影片上浪费了15分钟
Nitin Bansal 2015年

内部和外部应该buildscript repositories具有相同的远程存储库吗?
R.Abedi '19

158

apply plugin: 'com.android.application'

您应该添加以下内容:

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

@Benjamin解释了原因。

如果您具有通过身份验证的专家,则可以使用:

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

顺序很重要。


6
我们如何在gradle文件中添加cloudbees身份验证?
2014年

如何修改Android Studio,以便默认情况下将maven存储库添加到build.gradle。我一直忘了这件事,不得不重新发现为什么没有加载库。
迈克(Mike)

36

Android Studio用户:

如果要使用等级,请访问http://search.maven.org/并搜索您的Maven存储库。然后,单击“最新版本”,在左下角的详细信息页面中,您将看到“ Gradle”,然后可以在其中复制/粘贴链接到应用程序的build.gradle中。

在此处输入图片说明


1
你让我开心,非常感谢。另外,为什么这不在官方的ZXing gh页面中?应该不难发现……
Nadia Castelli

8

您必须添加repositories到构建文件。对于Maven存储库,您必须在存储库名称前添加前缀maven{}

repositories {
  maven { url "http://maven.springframework.org/release" }
  maven { url "http://maven.restlet.org" }
  mavenCentral()
}

6

如下所示,将Maven存储库添加到buildscriptbuild.gradle文件的配置块之外:

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

确保在以下步骤之后添加它们:

apply plugin: 'com.android.application'
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.