gradle proj sync失败,每个方法都尝试过

时间:2018-02-02 04:46:28

标签: java android android-studio gradle

错误消息:

Error:Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve com.android.support:appcompat-v7:26.1.0.
<a href="openFile:C:/Users/Alexs/AndroidStudioProjects/applicationone/app/build.gradle">Open File</a><br><a href="Unable to resolve dependency for &#39;:app@debug/compileClasspath&#39;: Could not resolve com.android.support:appcompat-v7:26.1.0.">Show Details</a>

此错误伴随着大量类似的依赖项错误。

尝试了方法:

  1. clear caches
  2. 重新安装android studio
  3. 下载最新的Gradle并替换原来的Gradle。
  4. 4。使用build.gradle文件并添加几行

    我只是一个想要用java开始一些android项目的学生。

    提前致谢。

    这是我的高级gradle文件:

    // Top-level build file where you can add configuration options common to all sub-projects/modules.
    
    buildscript {
    
        repositories {
            google()
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.0.1'
    
    
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    
    allprojects {
        repositories {
            google()
            jcenter()
        }
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    

    这是我的项目的gradle文件:

    apply plugin: 'com.android.application'
    
    repositories {
        maven {
           url "https://s3.amazonaws.com/repo.commonsware.com"
        }
        jcenter()
        google()
    }
    
    
    android {
        compileSdkVersion 26
        buildToolsVersion "26.0.2" // -------------------> Add this
        defaultConfig {
            applicationId "com.example.alexs.applicationone"
            minSdkVersion 21
            targetSdkVersion 26
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
            debug {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        implementation fileTree(include: ['*.jar'], dir: 'libs')
        implementation 'com.android.support:appcompat-v7:26.1.0'
        implementation 'com.android.support.constraint:constraint-layout:1.0.2'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.1'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
        implementation 'com.android.support.test.espresso:espresso-core:3.0.2-alpha1'
    }
    

    我的sdk版本是27 btw。

2 个答案:

答案 0 :(得分:0)

buildToolsVersion "26.0.2"添加到build.gradle文件中,如下所示:

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2" // -------------------> Add this
    defaultConfig {
        applicationId "com.example.alexs.applicationone"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

答案 1 :(得分:0)

在您的顶级build.gradle文件中,不是您发布的应用模块,请添加以下块:

allprojects {
    repositories {
        google()
    }
}

或者在您的app/build.gradle文件(您发布的文件)中添加google存储库:

repositories {
    maven {
       url "https://s3.amazonaws.com/repo.commonsware.com"
    }
    jcenter()
    google()
}