简体   繁体   English

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

[英]Gradle fails to resolve dependencies in Android Studio

Whenever I add a dependency from an remote repository (jcenter) in Android Studio 1.1 (OS X) I get the following error upon syncing Gradle:每当我在 Android Studio 1.1 (OS X) 中添加来自远程存储库 (jcenter) 的依赖项时,同步 Gradle 时会出现以下错误:

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

My app's build.gradle is as follows: apply plugin: 'com.android.application'我的应用程序的 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'
}

I have tried changing repositories (jcenter(), mavenCentral(), etc.), reinstalling Android Studio, JDK (7 and 8), changing Gradle version and Android SDK version, none of which worked. I have tried changing repositories (jcenter(), mavenCentral(), etc.), reinstalling Android Studio, JDK (7 and 8), changing Gradle version and Android SDK version, none of which worked. Upon running./gradlew build I get:在运行./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

This is an error I get in other cases as well, for example when trying to use Intellij Idea instead of Android Studio.这也是我在其他情况下遇到的错误,例如在尝试使用 Intellij Idea 而不是 Android Studio 时。 Running the app without the dependency does work though.不过,在没有依赖关系的情况下运行应用程序确实有效。

A similar problem here, and when I choose Build -> Clean Project there is a "peer not authenticated" error.这里有一个类似的问题,当我选择 Build -> Clean Project 时,会出现“对等方未验证”错误。

solved by using " http://jcenter.bintray.com/ " instead of " https://jcenter.bintray.com/ "通过使用“ http://jcenter.bintray.com/ ”而不是“ https://jcenter.bintray.com/ ”解决

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

hope this work for you.希望这对你有用。

I had a similar problem with a library on github, which stated to install it as:我在 github 上的一个库也遇到了类似的问题,它声明将其安装为:

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

This workaround seem to work, although it's suboptimal because it doesn't fetch a specific version and leads to Android Studio warnings:这种解决方法似乎有效,尽管它不是最理想的,因为它不会获取特定版本并导致 Android Studio 警告:

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

Rather late but probably your gradle works in offline mode.比较晚,但可能您的 gradle 可以在离线模式下工作。 Go to File->Settings->Gradle and uncheck the offline mode.转到 File->Settings->Gradle 并取消选中离线模式。 Then try to refresh gradle and it should work然后尝试刷新gradle,它应该可以工作

Top build.gradle :顶部build.gradle

buildscript {
    repositories {
        jcenter()
    }

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

App build.gradle :应用程序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 Issue:多 Dex 问题:

compile 'com.google.android.gms:play-services:7.0.0' is too big and should not be used directly. compile 'com.google.android.gms:play-services:7.0.0'太大,不应该直接使用。 Please choose the modules you want here: http://developer.android.com/google/play-services/setup.html请在此处选择您想要的模块: http : //developer.android.com/google/play-services/setup.html

I am behind a corporate firewall.我在公司防火墙后面。 I had my http settings set, but not my https settings.我设置了 http 设置,但没有设置 https 设置。 So, I added the following to my gradle.properties file:因此,我在 gradle.properties 文件中添加了以下内容:

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

I have similar problem -> ERROR: Failed to resolve:我有类似的问题 -> 错误:无法解决:

solved by using maven { url 'https://jitpack.io' } instead of maven {url 'https://maven.google.com/'}通过使用maven { url 'https://jitpack.io' }而不是maven {url 'https://maven.google.com/'}

I have added it in project level build.gradle我已将它添加到项目级 build.gradle

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

It works fine.它工作正常。

In my case, the problem was I was declaring jitpack in buildscript.repositories instead of allprojects.repositories in my package build.gradle .就我而言,这个问题是我在宣布jitpack buildscript.repositories而不是allprojects.repositories在我的包build.gradle

Instead of:代替:

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

It should be here:它应该在这里:

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

Tested in UBUNTU - 14.04 ,在 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 .

In my case I added this to my build.gradle (module:app) :就我而言,我将其添加到我的build.gradle (module:app) 中

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

However, it gave me an error today.但是,今天它给了我一个错误。 My solution is to use this instead:我的解决方案是改用它:

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

When I added this com.droidninja:filepicker:2.2.5 dependency in my build.gradle (Module Level) file then I got this error.当我在我的 build.gradle(模块级)文件中添加这个com.droidninja:filepicker:2.2.5依赖项时,我得到了这个错误。

Failed to resolve: com.droidninja:filepicker:2.2.5

错误信息图片

Then I added gradlePluginPortal() in settings.gradle然后我在settings.gradle中添加了gradlePluginPortal()

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        // Add this line
        gradlePluginPortal()
    }
}

It worked for me.它对我有用。 错误解决的图像

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

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