Update Gradle in Flutter project
Asked Answered
P

12

80

I have this project in Flutter, but I haven't been able to build an apk for a couple of weeks, because of the Gradle version. I've tried everything, but Flutter always returns the error below:

I already install every update I found, even though, it shows that the Gradle version is 4.10.2.

  • flutter build apk
...
FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\israel.gomes\AppData\Local\Pub\Cache\hosted\pub.dartlang.org\audioplayers-0.17.4\android\build.gradle' line: 25

* What went wrong:
A problem occurred evaluating root project 'audioplayers'.
> Failed to apply plugin [id 'kotlin-android']
   > The current Gradle version 4.10.2 is not compatible with the Kotlin Gradle plugin. Please use Gradle 5.3 or newer, or the previous version of the Kotlin plugin.

* 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 1s


The plugin audioplayers could not be built due to the issue above.

Here some information about the project status and environment----------------------

  • Already changed gradle-wrapper.properties (Current file):
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-all.zip
  • android/build.gradle(Current file):
buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
.
.
.
  • gradle --version
------------------------------------------------------------
Gradle 5.6.2
------------------------------------------------------------

Build time:   2019-09-05 16:13:54 UTC
Revision:     55a5e53d855db8fc7b0e494412fc624051a8e781

Kotlin:       1.3.41
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.14 compiled on March 12 2019
JVM:          1.8.0_261 (Oracle Corporation 25.261-b12)
OS:           Windows 10 10.0 amd64

  • flutter doctor -v
[√] Flutter (Channel dev, 2.1.0-12.1.pre, on Microsoft Windows [versão 10.0.19042.867], locale pt-BR)
    • Flutter version 2.1.0-12.1.pre at C:\Flutter
    • Framework revision 8264cb3e8a (3 weeks ago), 2021-03-10 12:37:57 -0800
    • Engine revision 711ab3fda0
    • Dart version 2.13.0 (build 2.13.0-116.0.dev)

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at C:\Users\israel.gomes\AppData\Local\Android\Sdk
    • Platform android-30, build-tools 30.0.2
    • ANDROID_HOME = C:\Users\israel.gomes\AppData\Local\Android\Sdk
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

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

[√] Android Studio (version 4.0)
    • 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 1.8.0_242-release-1644-b01)

[√] IntelliJ IDEA Community Edition (version 2020.3)
    • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.2.3
    • Flutter plugin version 55.0.4
    • Dart plugin version 203.7759

[√] VS Code (version 1.54.3)
    • VS Code at C:\Users\israel.gomes\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.20.0

[√] Connected device (3 available)
    • Android SDK built for x86 (mobile) • emulator-5554 • android-x86    • Android 7.1.1 (API 25) (emulator)
    • Chrome (web)                       • chrome        • web-javascript • Google Chrome 89.0.4389.114
    • Edge (web)                         • edge          • web-javascript • Microsoft Edge 87.0.664.75

• No issues found!

Puryear answered 2/4, 2021 at 14:20 Comment(0)
D
107

Here is how I upgraded gradle in my older Flutter project:

  1. Create a new temp project with the latest version of Flutter. You'll use this to see what version of gradle Flutter is using nowadays.

  2. In the temp project open android/build.gradle check the classpath version of gradle being used and update that in your old Flutter project. For me today it looks like this:

    classpath 'com.android.tools.build:gradle:4.1.0'
    
  3. In the temp project open android/gradle/wrapper/gradle-wrapper.properties and check the distributionUrl. Update your old project to use the same one. For me today it looks like this:

    distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
    
  4. Rerun your app. It has to download the new version of gradle to the android/.gradle directory so the first time might take a while.

Or...

If your Android project doesn't have a lot of customizations, just delete the android folder (back it up first, especially the key.properties file) and recreate it by running the following from the root of your project:

flutter create .

This has been my preference recently. Although I have to recreate the launcher icon and other custom settings in AndroidManifest.xml, I know that I am getting the latest updates Flutter has to offer Android. There seems to be a lot more than gradle that Flutter changes about Android if your project is a year or two old.

Dimond answered 8/10, 2021 at 3:44 Comment(5)
this worked for me with the additional step of upgrading my compileSdkVersion 28 to compileSdkVersion 31Himyarite
Can you update the versions for today? - @DimondElectrostriction
@No.The.Hi, Consider the version numbers that I used in the answer as purely examples. Even if I updated them today, they would become out of date tomorrow. Following the steps listed above is the way to get the most up-to-date version numbers for yourself.Dimond
Use com.android.tools.build:gradle:4.2.2 to avoid recent issue around ads.Quinonez
The second solution worked for me.Humpy
B
47

The easiest way (I do so as Android Dev):

  1. Right click android folder in root folder
  2. Flutter
  3. Open in Android Studio
  4. Wait for it to load completely
  5. It'll ask you "Would you like to upgrade gradle?|"
  6. Click Yes
  7. flutter clean

Android studio open

Baldachin answered 14/7, 2022 at 17:53 Comment(11)
In my case Flutter is greyed out in this context menu in AS, any idea why?Slype
I have the same issue @micer did you solve your problem?Neutralize
What if I'm using VS Code to work on my Flutter app?Radman
For using VS Code,.. route to android/gradle folder and run: gradle updateBozcaada
worked for me this solutionClavicorn
@Bozcaada "The term 'gradle' is not recognized as the name of a cmdlet"Patrolman
Scoured the web for 2 days regarding error ""e: C:/myProject/build/package_info_plus/.transforms/abc47a3830e3f326f11c2f9f898fde9f/transformed/out/jars/classes.jar!/META-INF/package_info_plus_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.6.0.". Every solution attempt suggested updating the build.gradle's "ext.kotlin_version" setting - which did not work. THIS SOLUTION BY ANDRII WORKED!Materiel
@cyber ./gradlew updateDrud
Did not work for meWine
Wish I could upvote 100 times. I will add that in my case, after point 4, it gives some build errors so as point 5. i did "flutter clean" and as point 6. went back and relaunched the build and after it updated as you said. Another strange thing was that the first time it took me from version 7.14 to 7.4.2 so i had to do it again. The second time it took me to current latest version 8.14.Tumbler
UPDATE - while this solution works, sometimes the "Flutter" sub-menu is disabled. In that case... Within Android Studio (AS), choose "File | Open". If necessary wait for file structure to load in pop-up. Then navigate to the "Android" directory and open it in "new window". This now opens your project at that level. Right away upon opening that folder, AS will start doing updates (see bottom-right of AS window). Eventually look for something like "start the upgrade assistant". Allow this to start and perform updates when prompted. Re-open project at top-level and error gone!Materiel
A
10

An update for 2022, Android Studio Dolphin. The other answers gave me a start, but are no longer up-to-date.

  1. Open the main flutter project in Android Studio
  2. Right click on the [android] module. Choose "Flutter". Choose Open Android module in Android Studio
  3. I opened in a new window, but this probably isn't required.
  4. As it says in the text here , choose on the menu (you must be in the "Android" project)
    • File
    • Project Structure
    • Project
  5. Click the Android Gradle Plugin Version dropdown and choose the latest version (or whatever one you want to update to)
  6. Click the Gradle Version dropdown and choose the latest version (or whatever one you want to update to)
  7. Give time for your Gradle to sync (it might have to download quite a lot, so be patient)
  8. Now you should be able to close the Android project window, and go back to the flutter project
Amphichroic answered 29/10, 2022 at 19:45 Comment(1)
What versions should we use? Are they all compatible with each other?Uncharitable
B
9

android/gradle/wrapper/gradle-wrapper.properties:

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

android/build.gradle:

classpath "com.android.tools.build:gradle:7.0.1"

in android/gradle.properties, add this:

org.gradle.java.home=C:\\Program Files\\Android\\Android Studio\\jre
Baobaobab answered 27/1, 2022 at 21:12 Comment(2)
Note that the last path is valid only for Windows and not for mac.Peloria
Add more description...Chloro
E
8

Try renaming the android/ folder in your project. Then run flutter create . in the directory. This should recreate your android directory. Try your build again and see if it fixed your problem.

Esch answered 13/3, 2022 at 8:55 Comment(1)
This is so underrated...Churchless
C
5

It appears as if audioplayers has an incompatible gradle version with your project.

Try upgrading audioplayers to the newest version, although this may not fix the issue, as the last comment in this thread was less than 24 hours ago.

audioplayers: ^0.18.3
Combustor answered 2/4, 2021 at 14:27 Comment(3)
Yeah... The update didn't solve the problem. I had to delete this library for the project.Thank you for notice the issue. I didn't know about that! I'll follow the Issue to see if anyone can solve the problem. Thanks!Puryear
Although this solves the problem at hand. But it's not the actual answer to the question. So this should've been a comment insteadFaustena
@HusseinAl-Mosawi The implied question that OP posted is something along the lines "what's wrong with my code that makes it impossible for me to update Gradle in my project". Stackoverflow is a Q&A website, which makes it completely fine for my answer to address only this specific case. If you believe that my answer isn't compliant with Stackoverflow guidelines then perhaps instead of downvoting it would be better to flag it and report to the mods?Combustor
S
5

Upgrade gradle in a Flutter project. Open the file gradle-wrapper.properties located at {yourProjectName}/android/gradle/wrapper/gradle-wrapper.properties and replace the distributionUrl by changing the gradle version.

For example if you want to upgrade gradle from version 5.1.1 to 6.1.1, change the Url from https\://services.gradle.org/distributions/gradle-5.1.1-all.zip to https\://services.gradle.org/distributions/gradle-6.1.1-all.zip and rebuild your app.

Stupa answered 11/2, 2022 at 20:48 Comment(0)
B
0

Fast way of doing it:

  1. Open AndroidManifest.xml (andriod/app/src/main/AndroidManifest.xml)
  2. Click "Open for Editing in Android Studio"

enter image description here

Baldachin answered 4/10, 2022 at 14:35 Comment(0)
S
0

In late 2022, Android Studio can fix that and upgrade Gradle:

  1. Open the main flutter project in Android Studio
  2. Right click on the android module. Choose "Flutter". Choose Open Android module in Android Studio, I opened in a new window
  3. Then, in the lower right corner, Android Studio presents a message suggesting to upgrade Gradle. Follow this suggestion!
  4. After that, you can close the Android project window

Based on Nick Fortescue answer

Silver answered 19/12, 2022 at 14:21 Comment(0)
D
0

I was getting this issue:

FAILURE: Build failed with an exception.
  • Where: Build file '/home/marcelo-cesar/Desktop/Overseasconnection-player-teams/android/app/build.gradle' line: 47

  • What went wrong: A problem occurred evaluating project ':app'.

Could not find method minSdkVersion() for arguments [19, null] on DefaultConfig$AgpDecorated_Decorated{name=main, dimension=null, minSdkVersion=null, targetSdkVersion=null, renderscriptTargetApi=null, renderscriptSupportModeEnabled=null, renderscriptSupportModeBlasEnabled=null, renderscriptNdkModeEnabled=null, versionCode=null, versionName=null, applicationId=com.ls.overseasagent, testApplicationId=null, testInstrumentationRunner=null, testInstrumentationRunnerArguments={}, testHandleProfiling=null, testFunctionalTest=null, signingConfig=null, resConfig=[], buildConfigFields={}, resValues={}, proguardFiles=[], consumerProguardFiles=[], manifestPlaceholders={applicationName=android.app.Application}, wearAppUnbundled=null} of type com.android.build.gradle.internal.dsl.DefaultConfig$AgpDecorated.

  • 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.

BUILD FAILED in 3s Exception: Gradle task assembleDebug failed with exit code 1 Exited (sigterm)

I solved it by making these changes: In android/build.gradle update these lines with these following:

ext.kotlin_version = '1.7.10'
classpath 'com.android.tools.build:gradle:7.3.1'

In android/app/build.gradle update with this value: compileSdkVersion 33

defaultConfig {

… minSdkVersion 21 targetSdkVersion 31 }

In android/gradle/wrapper/gradle-wrapper.properties update distribution line with this:

distributionUrl=https://services.gradle.org/distributions/gradle-7.5-all.zip
Dinerman answered 21/2, 2023 at 16:8 Comment(0)
E
0

Everyone is saying edit this, update that but it's not always the best solution when the app code is shared across multiple developers.

You should install the recommended java version corresponding to the project gradle version.

  1. Get the project gradle version in this file :
    $PROJECT/android/gradle/wrapper/gradle-wraper.properties
  2. Check what Java version is recommended for your gradle version
    Gradle Website Compatibility Matrix
  3. Install it :D
  4. (optional) If you have multiple versions you can for example set a virtual env for the correct JAVA Version by adding the correct binary path at the start of PATH Var like this
export PATH="/usr/lib/jvm/java-18-openjdk-amd64/bin/java:$PATH"

Please note that other solutions listed above will also work fine, but this answer add some context and will just be easier than to update all your project's gradle version.

Have a nice day 👋🏼

Enjoyable answered 24/6, 2023 at 0:13 Comment(0)
M
-1

android/gradle/wrapper/gradle-wrapper.properties:

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

if 7.6.1 is not working use 7.5

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

android/build.gradle:

classpath "com.android.tools.build:gradle:7.4.2"
Midday answered 24/8, 2023 at 4:5 Comment(1)
Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Seethrough

© 2022 - 2024 — McMap. All rights reserved.