Gradle无法解析Android Studio中的依赖项

时间:2015-03-16 12:56:40

标签: java android maven android-studio gradle

每当我在Android Studio 1.1(OS X)中从远程存储库(jcenter)添加依赖项时,在同步Gradle时会出现以下错误:

Error:(26, 13) Failed to resolve: <packagename:version>

我的应用程序的build.gradle如下:    apply plugin:'com.android.application'

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
    applicationId "nl.timmevandermeer.cargoapp"
    minSdkVersion 19
    targetSdkVersion 21
    versionCode 1
    versionName "1.0"
}
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     compile 'com.android.support:appcompat-v7:21.0.3'
     compile 'com.android.support:support-v4:21.0.3'
     compile 'com.google.android.gms:play-services:6.5.87'
     compile 'org.json:json:20141113'
}

我尝试更改存储库(jcenter(),mavenCentral()等),重新安装Android Studio,JDK(7和8),更改Gradle版本和Android SDK版本,但都没有。运行./gradlew build后,我得到:

Could not resolve com.android.tools.build:gradle:1.1.0.
13:48:04.914 [ERROR] [org.gradle.BuildExceptionReporter]      Required by:
13:48:04.915 [ERROR] [org.gradle.BuildExceptionReporter]          :CargoApp:unspecified
13:48:04.932 [ERROR] [org.gradle.BuildExceptionReporter] >
 org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;[Ljava/lang/Object;Ljava/lang/Throwable;)V

这是我在其他情况下遇到的错误,例如在尝试使用Intellij Idea而不是Android Studio时。运行没有依赖项的应用程序确实有效。

9 个答案:

答案 0 :(得分:26)

这里有类似的问题,当我选择Build - &gt;清洁项目 有一个“peer not authenticated”错误。

使用“http://jcenter.bintray.com/”代替“https://jcenter.bintray.com/

解决
repositories {
    jcenter({url "http://jcenter.bintray.com/"})
}

希望这对你有用。

答案 1 :(得分:14)

我在github上的库遇到了类似的问题,它声称将其安装为:

dependencies {
    compile 'com.github.chrisbanes.photoview:library:1.2.4'
}

这种解决方法似乎有效,虽然它不是最理想的,因为它无法获取特定版本并导致Android Studio警告:

dependencies {
    compile 'com.github.chrisbanes.photoview:library:+'
}

答案 2 :(得分:3)

相当晚,但可能你的gradle在离线模式下工作。 转到文件 - >设置 - &gt; Gradle并取消选中离线模式。 然后尝试刷新gradle,它应该工作

答案 3 :(得分:2)

热门build.gradle

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.3'
    }
}

App build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "nl.timmevandermeer.cargoapp"
        minSdkVersion 19
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
     // compile fileTree(dir: 'libs', include: ['*.jar']) // <-- use gradle depedencies
     compile 'com.android.support:appcompat-v7:22.0.0' // <-- brings in v4
    // compile 'com.google.android.gms:play-services:7.0.0' // <-- will cause MultiDex exception, you need to choose which ones you want
    // compile 'org.json:json:20141113' // <-- android comes with org.json
}

Multi-Dex问题:

compile 'com.google.android.gms:play-services:7.0.0'太大了,不应该直接使用。请在此处选择您需要的模块:http://developer.android.com/google/play-services/setup.html

答案 4 :(得分:2)

我在公司防火墙后面。我设置了http设置,但不是我的https设置。所以,我将以下内容添加到我的gradle.properties文件中:

systemProp.https.proxyPassword=...
systemProp.https.proxyHost=...
systemProp.https.nonProxyHosts=...
systemProp.https.proxyUser=...
systemProp.https.proxyPort=...

答案 5 :(得分:2)

就我而言,问题是我在包 buildscript.repositories 中声明了 jitpack 而不是 allprojects.repositories

代替:

build.gradle

它应该在这里:

buildscript {
    repositories {
        ...
        maven {
            url "https://jitpack.io"
        }
    }
}

答案 6 :(得分:1)

我有类似的问题->错误:无法解决:

通过使用maven { url 'https://jitpack.io' }代替 maven {url 'https://maven.google.com/'}

我已将其添加到项目级别的build.gradle

allprojects {
  repositories {
    maven { url 'https://jitpack.io' }
  }
}

它工作正常。

答案 7 :(得分:0)

在UBUNTU中测试 - 14.04,

>delete .androidstudio1.2 , .gradle , .android  , Android studio project from home directory 
> Launch using studio.sh
>close the dialogue letting you choose new or old setting , just close it using that "x " button 
> then it will set up an virtual nexus 5 
>then there will be two errors saying problem about "sdk" and "gradle "
> Caution : choose the appropriate sdk version by clicking on the link given by where the problem is shown .
> close android studio 
> reopen android studio 
> then you will be thrown a possible  error with jdk not found .
>go to file -> project structure 
> give the path to jdk , in my case its "usr/local/java/jdk..."
>close android studio and open again , the gradle might work well after this .

答案 8 :(得分:0)

在我的情况下,我将其添加到 build.gradle(module:app)

compile 'com.github.glomadrian:velocimeterlibrary:1.1@aar' 

然而,它今天给了我一个错误。我的解决方案是使用它:

compile 'com.github.glomadrian:velocimeterlibrary:+'