Gradle 无法解析 Android Studio 中的依赖关系

问题描述 投票:0回答:10

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

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

我的应用程序的build.gradle如下: 应用插件:'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 时。不过,在没有依赖项的情况下运行应用程序确实可以工作。

java android maven android-studio gradle
10个回答
28
投票

这里有类似的问题,当我选择“构建”->“清理项目”时 出现“对等方未经过身份验证”错误。

通过使用“http://jcenter.bintray.com/”而不是“https://jcenter.bintray.com/”来解决

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

希望这对你有用。


15
投票

我在 github 上的一个库上遇到了类似的问题,它指出将其安装为:

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

此解决方法似乎有效,尽管它不是最理想的,因为它不会获取特定版本并导致 Android Studio 警告:

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

5
投票

就我而言,问题是我在

buildscript.repositories
中声明 jitpack,而不是在我的包
allprojects.repositories
中声明
build.gradle

代替:

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

应该在这里:

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

4
投票

当我在 build.gradle(模块级别)文件中添加此

com.droidninja:filepicker:2.2.5
依赖项时,我收到了此错误。

Failed to resolve: com.droidninja:filepicker:2.2.5

Error message image

然后我添加了

gradlePluginPortal()
settings.gradle

中的 'maven { url 'https://jitpack.io' }'

kts

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        
        // Add these lines if they are not there.
        mavenCentral()
        maven { setUrl ("https://jitpack.io") }
    }
}

格罗维

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        // Add this line
        gradlePluginPortal()
        
        // If issue still persist then try to add this line
        maven { url 'https://jitpack.io' }

    }
}

这对我有用。 Error resolved image


3
投票

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


3
投票

我位于公司防火墙后面。 我设置了 http 设置,但没有设置 https 设置。 因此,我将以下内容添加到我的 gradle.properties 文件中:

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

2
投票

顶部
build.gradle

buildscript {
    repositories {
        jcenter()
    }

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

应用程序
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
}

多去中心化问题:

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


2
投票

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

通过使用

maven { url 'https://jitpack.io' }
代替
maven {url 'https://maven.google.com/'}

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

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

效果很好。


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 .

0
投票

就我而言,我将其添加到我的 build.gradle (module:app):

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

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

compile 'com.github.glomadrian:velocimeterlibrary:+'
© www.soinside.com 2019 - 2024. All rights reserved.