无法解决:com.android.support:appcompat-v7:27.+(依赖性错误)

Hel*_*hah 60 android android-studio android-gradle-plugin

我在Android studio中遇到此问题.

Error:Failed to resolve: com.android.support:appcompat-v7:27.+
<a href="install.m2.repo">Install Repository and sync project</a><br><a href="open.dependency.in.project.structure">Show in Project Structure 
dialog</a>
Run Code Online (Sandbox Code Playgroud)

我的Android Studio充满了错误,android studio无法识别库.整个屏幕看起来像这样.

图片

这是我的Gradle代码:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    buildToolsVersion "27.0.1"

    defaultConfig {
        applicationId "com.example.hp.temp"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:27.+'
}
Run Code Online (Sandbox Code Playgroud)

Ary*_*yan 117

找到根build.gradle文件并在allprojects标签内添加google maven repo

repositories {
        mavenLocal()
        mavenCentral()
        maven {                                  // <-- Add this
            url 'https://maven.google.com/' 
            name 'Google'
        }
    } 
Run Code Online (Sandbox Code Playgroud)

最好使用特定版本而不是变量版本

compile 'com.android.support:appcompat-v7:27.0.0'
Run Code Online (Sandbox Code Playgroud)

如果你正在使用Android插件Gradle 3.0.0或更高版本

repositories {
      mavenLocal()
      mavenCentral()
      google()        //---> Add this
} 
Run Code Online (Sandbox Code Playgroud)

并以这种方式注入依赖:

implementation 'com.android.support:appcompat-v7:27.0.0'
Run Code Online (Sandbox Code Playgroud)

  • 我总是喜欢使用最新版本,当前最新版本是`27.0.2`,如果你得到dex错误,启用multidex. (2认同)

D_A*_*pha 56

如果您使用的是Android Studio 3.0或更高版本,请确保您的项目build.gradle的内容类似于 -

buildscript {                 
    repositories {
        google()
        jcenter()
    }
    dependencies {            
        classpath 'com.android.tools.build:gradle:3.0.1'

    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}
Run Code Online (Sandbox Code Playgroud)

注意 -位置真的很重要在jcenter()之前添加google ()

对于Android Studio 3.0以下,从支持库26开始.+你的项目build.gradle必须看起来像这样 -

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

请查看以下链接了解更多详情 -

1- 构建Android应用程序

2- 添加构建依赖项

3- 配置您的构建

  • 谷歌必须像你一样在jcenter之前设置,否则错误将继续出现 (3认同)