Search code examples
flutterandroid-studiogradlebuild.gradleflutter-dependencies

Gradle error downloading dependencies in Flutter project


I'm getting an error when building an APK for my Flutter project (Flutter 3, Gradle 7.3.0). I'm using the easy_localization plugin which depends on shared_preferences_android. Gradle starts downloading dependencies but then throws an error.

Some initial lines of error trace:

FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring project ':shared_preferences_android'.
> Could not resolve all files for configuration ':shared_preferences_android:classpath'.
   > Could not find gradle-7.2.2.jar (com.android.tools.build:gradle:7.2.2).
     Searched in the following locations:
         https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/7.2.2/gradle-7.2.2.jar
   > Could not find builder-7.2.2.jar (com.android.tools.build:builder:7.2.2).
     Searched in the following locations:
         https://dl.google.com/dl/android/maven2/com/android/tools/build/builder/7.2.2/builder-7.2.2.jar
   > Could not find sdk-common-30.2.2.jar (com.android.tools:sdk-common:30.2.2).
     Searched in the following locations:

https://dl.google.com/dl/android/maven2/com/android/tools/sdk-common/30.2.2/sdk-common-30.2.2.jar
   > Could not find repository-30.2.2.jar (com.android.tools:repository:30.2.2).
     Searched in the following locations:
         https://dl.google.com/dl/android/maven2/com/android/tools/repository/30.2.2/repository-30.2.2.jar
   > Could not find gradle-api-7.2.2.jar (com.android.tools.build:gradle-api:7.2.2).
     Searched in the following locations:
         https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle-api/7.2.2/gradle-api-7.2.2.jar
   > Could not find builder-test-api-7.2.2.jar (com.android.tools.build:builder-test-api:7.2.2).
...

build.gradle file:

buildscript {
    ext.kotlin_version = '1.7.10'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}
  • I tried with flutter clean.
  • Flutter doctor says "All OK" But all those not work.

Gradle seems unable to find .jar files at the URLs listed in the error, even though they work when I click the links.

Important: if I try to install another plugin, gradle throws the same error newly

How can I resolve this Gradle error and successfully build my Flutter APK?


Solution

  • Solved!

    1. I deleted the gradle cache at /home/<my_user_name>/.gradle/caches/
    2. (Very important) I left Visual Studio behind and used Android Studio to build the gradle. Apparently, Android Studio manages the gradle building much better than the linux console or Visual Studio.