build.gradle error: Could not create tastk ':generateLockfiles'. in Flutter project but App is running normal
Asked Answered
C

13

11

I have an error in my build.gradle file (android/build.gradle) in my Flutter project. The App is running completely normal but this error is still not looking good to me. Does anyone ever had this problem/error?

enter image description here

enter image description here

Here is my complete build.gradle file:

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

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

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

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

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

flutter doctor -v Output:

[√] Flutter (Channel stable, 3.0.5, on Microsoft Windows [Version 10.0.22598.200], locale de-DE)
    • Flutter version 3.0.5 at C:\Flutter\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision f1875d570e (5 weeks ago), 2022-07-13 11:24:16 -0700
    • Engine revision e85ea0e79c
    • Dart version 2.17.6
    • DevTools version 2.12.2

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at C:\Users\jjkla\AppData\Local\Android\sdk
    • Platform android-31, build-tools 30.0.3
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 2020.3)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[√] VS Code (version 1.70.1)
    • VS Code at C:\Users\jjkla\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.46.0

[√] Connected device (2 available)
    • Chrome (web) • chrome • web-javascript • Google Chrome 104.0.5112.81
    • Edge (web)   • edge   • web-javascript • Microsoft Edge 104.0.1293.54

[√] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

EDIT 1: Since this happened to me over a year ago now, I don't remember exactly what solved the error. But there was definitely an incompatible version with conflicted with another version. General Advice: You should take a look at your Kolin Version in android/build.gradle under ext.kotlin_version = 'x.x.x'. Take a look at the to find the Kotlin Website newest available Kolin Version.

Coxcombry answered 15/8, 2022 at 5:58 Comment(3)
Can you run the flutter doctor -v and share the output?Gomes
@NewDeveloper I added the output.Coxcombry
This may help: https://mcmap.net/q/1016231/-flutter-could-not-create-task-39-generatelockfiles-39-problem-with-gradlew-bat-in-windowsBellbird
D
6

Same problem here, but in Linux and Visual Studio code.

With ./gradlew in android directory, the build failed due wrong Java version. Was Java 1.8, but must be at least 11. After update my java version, ./gradlew built successfully.

After restart visual studio code, the error was gone.

Dieball answered 1/11, 2022 at 19:19 Comment(2)
So what did you do? Did you update Java? How? Where?Bellbird
I just updated the java version.Dieball
G
7

Actually the problem is version mismatch ( java and gradle )

In my flutter project i faced the same problem, now i fix it.

Here is how?

First check your Java version run by this command javac --version

In my case my java version is 21 and then go to the Gradle's site now you can see the correct version that matches your java version

https://docs.gradle.org/current/userguide/compatibility.html

Then go to your flutter project directory, and then go to > android > gradle > gradle-wrapper.properties

In this file you can see distributionUrl like this

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

Then change the correct version that matches your java version. for example instead of gradle-8.4-all.zip you can change you varsion and save the file and then reload window run flutter pub get

This will solve the problem

Glowworm answered 28/1 at 11:14 Comment(2)
Thanks, I created a brand new project and updated mine to 8.5 and it worked, for some reason flutter uses outdated onesClaiborn
I also had to update all SDK parts in Android Studio. If anybody still have the Problem, after updating the distributionUrl, take a look there.Amour
D
6

Same problem here, but in Linux and Visual Studio code.

With ./gradlew in android directory, the build failed due wrong Java version. Was Java 1.8, but must be at least 11. After update my java version, ./gradlew built successfully.

After restart visual studio code, the error was gone.

Dieball answered 1/11, 2022 at 19:19 Comment(2)
So what did you do? Did you update Java? How? Where?Bellbird
I just updated the java version.Dieball
C
2

I tried to open Android directory (which is in my flutter project) with Android studio, Android studio suggested to upgrade and then I followed suggestion, and finally error is solved now.

Crofter answered 25/9, 2023 at 15:15 Comment(0)
Z
1

You can do flutter clean and flutter pub get and then try to build again.

Zany answered 15/8, 2022 at 10:44 Comment(0)
I
1

You can try to rebuild the build.gradle file.
Just follow these steps.

cd android 
./gradlew clean  
./gradlew build

You can also execute this command in one line.

./gradlew clean build
Inadvisable answered 26/10, 2022 at 12:11 Comment(0)
F
0

open your android project in android studio file > open > in your flutter project choose android file then rebuild gradle, I have same problem and solved it like this.

Fatigue answered 15/8, 2022 at 6:34 Comment(1)
how do I rebuild in Android Studio? is there a button for this?Coxcombry
G
0

This error can be occurred if you change the name of your app under pubspec.yaml file

enter image description here

you can resolve this error by two ways

  1. reverting the previous name

  2. Going under projects build.gradle file and change this

     subprojects {
     project.buildDir = "${rootProject.buildDir}/${project.name}"
     }
    

to

subprojects {
    project.buildDir = "${rootProject.buildDir}/Groic"
}

we've changed project.name with the name mentioned in pubspec.yaml

Gobetween answered 26/5, 2023 at 18:50 Comment(0)
H
0

You can try to install java 11 version or higher. Download java 11 version install it on your system > restart vs code or whichever code editor you are using > in terminal you can run flutter clean and then run flutter pub get

Make the following changes to your build.gradle file: Recommended change #1 in build.gradle file Recommended change #2 in build.gradle file

Holography answered 25/12, 2023 at 13:40 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewAnnatto
F
0

For me it was definitely related to the java version, installing java 11 and giving the correct JAVA_HOME path in Environment Variables and restarting the computer eliminated the problem

Flapper answered 28/2 at 11:22 Comment(0)
P
0

These are the step I handle above error.

first check errors in flutter:

flutter doctor

then rebuld build.gradle:

cd android 
./gradlew clean  
./gradlew build
./gradlew clean build

Optional tasks:

flutter upgrade
flutter pub get

then restart the IDE and check.

Perdomo answered 25/3 at 21:42 Comment(0)
A
0

If you followed every single answer yet, the error still persists. Make sure that the version of JDK that you are using supports or is compatible with a particular version of Gradle. For more information, check here: compatibility

Aircrewman answered 22/5 at 11:46 Comment(0)
P
0

try this in the command with internet connection then restart vscode

flutter clean
flutter pub get
cd android
./gradlew clean
Patrinapatriot answered 21/6 at 18:53 Comment(0)
D
0

Original Answer:

I deleted the /android folder and created a new one using 'flutter create'. That fixed my warnings despite it running perfectly normally.


Improved Answer:

To fix this, follow these steps:

  1. Add these lines to settings.gradle:
    id "com.android.application" version "7.3.0" apply false
    id "org.jetbrains.kotlin.android" version "2.0.0" apply false
    
  2. Delete the /android folder:
    rm android
    
  3. Recreate the android folder:
    flutter create .
    
  4. Clean the project:
    flutter clean
    
  5. Run the project:
    flutter run
    
  6. Update the minSdkVersion in android/app/build.gradle as needed.
  7. Run again:
    flutter run
    

This should resolve your issue!

How to fix: ‘Could not create task :generateLockfiles.’

Disreputable answered 23/8 at 12:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.