简体   繁体   English

将 Maven 存储库添加到 build.gradle

[英]add maven repository to build.gradle

I added a custom maven repository to build.gradle in Android Studio but the dependency is not being found我在 Android Studio 中向 build.gradle 添加了一个自定义 Maven 存储库,但未找到依赖项

Maven repository and dependency 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构建.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: 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?我在 build.gradle 中缺少什么?

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. @Benjamin 解释了原因。

If you have a maven with authentication you can use:如果您有带身份验证的 Maven,您可以使用:

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

It is important the order.顺序很重要。

You will need to define the repository outside of buildscript .您需要在buildscript之外定义存储库。 The buildscript configuration block only sets up the repositories and dependencies for the classpath of your build script but not your application. buildscript配置块仅为您的构建脚本的类路径设置存储库和依赖项,而不是您的应用程序。

Android Studio Users:安卓工作室用户:

If you want to use grade, go to http://search.maven.org/ and search for your maven repo.如果您想使用 Grade,请访问http://search.maven.org/并搜索您的 maven 存储库。 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.然后,单击“最新版本”,在左下角的详细信息页面中,您将看到“Gradle”,然后您可以将该链接复制/粘贴到您的应用程序的 build.gradle 中。

在此处输入图片说明

You have to add repositories to your build file.您必须将repositories添加到构建文件中。 For maven repositories you have to prefix repository name with maven{}对于 maven 存储库,您必须使用maven{}存储库名称的前缀

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

Add the maven repository outside the buildscript configuration block of your main build.gradle file as follows:在主build.gradle文件的buildscript配置块之外添加 maven 存储库,如下所示:

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM