Gradle sync failed: Could not find com.android.tools.build:gradle:xxx

此博客可能适用于本来能正常编译的老项目,一段时间后无法编译通过,并提示这个错误。假如是新建项目碰到的,可能的确是build.gradle里classpath没写好版本。

今天打车还迟到了,很自闭,然后打开android studio。项目自动编译,突然爆了个大红色,更自闭了。以下是错误流程:

说实话,有点懵,我第一反应是不是Android Studio把我安卓编译版本升级了,我的项目下的build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply from: 'config/global.gradle'

buildscript {
    ext.kotlin_version = '1.2.10'
    repositories {
        jcenter()

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

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath "com.tencent.bugly:tinker-support:latest.release"
        classpath 'com.meituan.android.walle:plugin:1.1.5'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        mavenCentral()
        jcenter()
        maven {
            url 'http://maven.aliyun.com/nexus/content/repositories/releases/'
        }
        maven {
            url "https://jitpack.io"
        }
        maven {
            url "https://dl.bintray.com/thelasterstar/maven/"
        }
        maven {
            url "https://maven.google.com"
        }
        maven {
            url "https://dl.google.com/dl/android/maven2/"
        }
        flatDir{
            dir 'libs'
        }
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

我的gradle-wrapper.properties:

#Tue May 23 09:23:46 CST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip

发现没有任何变化,git也没提示有文件被修改

先是照着提示,操作了一遍,发现没有任何作用:
Add Google Maven repository and sync project

这个时候项目下的build.gradle的repositories增加了谷歌仓库

repositories {
    jcenter()

    maven {
        url 'https://maven.google.com/'
        name 'Google'
    }
}

然后编译失败,提示:

说是gradle版本最低为4.6,虽然我不想升级,但是我还是点击了Fix Gradle wrapper and re-import project

这个时候gradle-wrapper.properties的gradle版本号已经变成了4.6,接下来可能是我Android Studio版本太低,是无法编译通过的,提示:Error:Cause: com/android/builder/Version

假如是比较高级的Android Studio可能这个时候就会提示什么compile应该替换成implementation之类的,就可以改了,我没升级Android Studio上面只是猜测。

从最开始提示找不到com.android.tools.build:gradle:3.2.1可以猜测,是否是某些东西需要了3.2.1进行编译,而我们没有改任何配置的情况下,最可疑的就是那些版本号为latest.release的依赖了,那么第一个怀疑对象就是它的同级:腾讯的bugly,先把代码回滚,然后把它版本号写死,写成固定的一个版本,重新编译,编译成功!

假如是突然出现问题是没使用过的编译版本,那么很可能是你的其他依赖自动更新后使用了该编译工具,回退上一个版本,就能解决问题。

有点啰嗦,但是这个问题,的确搞了好久,希望能帮到大家。

猜你喜欢

转载自blog.csdn.net/qq_27454233/article/details/99309434
今日推荐