38

I am trying to run a flutter app I am writing on my device but when I try to run, I never get past the "assembleDebug" stage and get this message in the terminal:

Launching lib\main.dart on SM N950U in debug mode...
Running Gradle task 'assembleDebug'...

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'android'.
> Could not resolve all artifacts for configuration ':classpath'.
   > Could not find com.android.tools.build:gradle:5.6.2.
     Searched in the following locations:
       - https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/5.6.2/gradle-5.6.2.pom
       - https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/5.6.2/gradle-5.6.2.jar
       - https://jcenter.bintray.com/com/android/tools/build/gradle/5.6.2/gradle-5.6.2.pom
       - https://jcenter.bintray.com/com/android/tools/build/gradle/5.6.2/gradle-5.6.2.jar
     Required by:
         project :

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 14s
Finished with error: Gradle task assembleDebug failed with exit code 1

I have recently updated to gradle 6.0.1 (I assume I did that correctly but I am very new to all this) and there are no issues with flutter doctor.

Here is my project level build.gradle:

buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:5.6.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.3'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

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

task clean(type: Delete) {
    delete rootProject.buildDir
}

Here is the app level build.gradle:

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 28

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.skypeclone1"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'

Here are my Gradle wrapper properties in case they are also important.

#Fri Jun 23 08:50:38 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip

gradle.properties

org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
android.enableR8=true
Jude Harding
  • 413
  • 1
  • 5
  • 7

10 Answers10

26

The problem probably was caused by updating the gradle. The Android Studio always asks you to update it, but don't do this for flutter apps. I had the same issue and I solved with the following versions:

On build.gradle module level:

buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        jcenter()
    }

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

On build.gradle app level:

compileSdkVersion 28
targetSdkVersion 28

On gradle-wrapper.properties:

distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip

By the way, I got all those versions by creating a new flutter app on 1.20 stable version.

Danilo Rêgo
  • 539
  • 5
  • 4
25

You're trying to use actual Gradle. For Android development, there's Android Gradle which is currently on version 3.5.3 which came out this month. Read this article: https://developer.android.com/studio/releases/gradle-plugin#3-5-0.

EDIT: To use the latest version of Android Gradle (3.5.3), you need Gradle versions between 5.4.1-5.6.4 which you can configure in the gradle-wrapper.properties file. I guess Android Gradle doesn't yet support Gradle 6.

Benjamin
  • 5,783
  • 4
  • 25
  • 49
  • Hi, I changed the distribution ref in `gradle-wrapper.properties` and the project level back to 5.6.2 and am getting the same error. Is there a specific way to implement this change correctly? – Jude Harding Dec 26 '19 at 01:13
  • Can you please update your post with your new files? – Benjamin Dec 26 '19 at 01:13
  • 7
    Set `classpath 'com.android.tools.build:gradle:5.6.2'` to `classpath 'com.android.tools.build:gradle:3.5.3'` – Benjamin Dec 26 '19 at 17:18
  • Thank you so much! That fixed the initial error, but now I have another "failure to build" error. It says that the problem is in the app level gradle on line 24 (apply plugin: 'com.android.application'). The error reads: * What went wrong: A problem occurred evaluating project ':app'. > Failed to apply plugin [id 'com.android.application'] > Cannot parse project property android.enableR8='true ' of type 'class java.lang.String' as boolean. Expected 'true' or 'false'. – Jude Harding Dec 26 '19 at 18:00
  • Can you share your `gradle.properties` file? – Benjamin Dec 26 '19 at 18:04
  • 2
    Follow these steps here: https://stackoverflow.com/a/54857699/10808802. You need to migrate to AndroidX properly – Benjamin Dec 26 '19 at 18:23
  • Thank you for your help! Ultimately the last error I encountered was because there was a trailing space after "true" that I missed (you can see it in my above comment). Annoying but I'm back in business. – Jude Harding Dec 27 '19 at 22:06
15

Sometime you need to run flutter pub upgrade.

Richie
  • 570
  • 6
  • 10
11

I had problem that i was connected with VPN (Home Office) but once i disconnect VPN then it run. So, there was network problem.

s_khan92
  • 969
  • 8
  • 21
2

you can use this in you build.gradle

    buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        jcenter()
        maven {
            url 'http://storage.googleapis.com/r8-releases/raw'
        }
    }

    dependencies {
        classpath 'com.android.tools:r8:2.1.75'
        classpath 'com.google.gms:google-services:4.3.2'
        classpath 'com.android.tools.build:gradle:4.1.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

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

task clean(type: Delete) {
    delete rootProject.buildDir
}
gtr Developer
  • 2,369
  • 16
  • 12
1

My flutter project require gradle upgrade and this error happened..

I try to solve this case and read this article.

I should not change build.gradle version. Just version at gradle-wrapper.properties

build.gradle file should like this as original :

buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        jcenter()
    }

    dependencies {
        //classpath 'com.android.tools.build:gradle:4.1.0' >> Original
        classpath 'com.android.tools.build:gradle:4.1.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

And I try run this at VS Code terminal :

  1. cd android (go to android directory below project dir)
  2. run : ./gradlew clean build
toha
  • 5,095
  • 4
  • 40
  • 52
0

Don't know about this question as it already accepted the answer.

If you are here after a recent flutter update(like me) then do the following.

If you have recently upgraded to a new flutter version

then you need to run flutter pub upgrade this will upgrade the

dependencies to the latest version accordingly.

Hope it solved your problem. Thank you!

repleeka
  • 550
  • 5
  • 14
0

I tried to change kotlin version to the latest while the Gardle isn't compatible with it, so make sure

 //ext.kotlin_version = '1.6.21' this v not compatible with current gradle version
 ext.kotlin_version = '1.5.30' // this compatible
 dependencies {
    classpath 'com.android.tools.build:gradle:4.1.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
 }
ibrahim saputra
  • 407
  • 6
  • 12
0

In my case, path_provider package need gradle 3 and I was getting this error.

Could not resolve all artifacts for configuration ':path_provider_android:classpath'
Could not find gradle-3.3.0.jar (com.android.tools.build:gradle:3.3.0)

These changes solved my problem.

1- In android\build.gradle I changed gradle version from 4.1.0 to 3.6.4

classpath 'com.android.tools.build:gradle:3.6.4'

2- In android\gradle\wrapper\gradle-wrapper.properties I changed distribution url from gradle-6.7-all.zip to gradle-6.1.1-all.zip

distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
Amir Aghajani
  • 367
  • 2
  • 5
0

I had an issue of same sort i got the answer from this post solution it happens by the lint package blocking app release build from building hope it helps