26

In my flutter app, flutter build apk succeeds without warning but android.app.build.gradle has an error in this code:

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.")
}

The error is

unable to resolve class GradleException

whereis flutter returns "/home/mark/dev/flutter/bin/flutter".

flutter doctor -v returns Flutter version 3.3.1 on channel stable at /home/mark/dev/flutter` and has no errors.

my local.properties file contains flutter.sdk=/home/mark/dev/flutter

How can I resolve this error?

4
  • 1
    It will work fine. Looks like you are new with flutter. You cannot remove this error Commented Sep 13, 2022 at 9:57
  • I just commented that line and hard coded my sdk path in and it works fine. I can confirm that it works fine even with the error so I assume this is just a linter error. Commented Sep 13, 2022 at 10:07
  • I do not recommend you to comment that line. Maybe it will work with debug mode. You should try to run relese mode or try to build apk. Commented Sep 13, 2022 at 10:13
  • The release apk builds fine. Commented Sep 13, 2022 at 10:32

5 Answers 5

51

Remove the new keyword before GradleException.

try this

throw GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
Sign up to request clarification or add additional context in comments.

4 Comments

Don't do this, Groovy requires the new keyword when raising exceptions.
@DarkNeuron can you expand or share a bit more on this? Just curious to learn. Personal workaround was simply to replace with FileNotFoundException()
could you explain more why this error was thrown and since groovy needs new to throw expection what can be done and thiis a auto generated file not something which i created
The new keyword in Groovy is used to create a new instance of a class. In this case, the new keyword is used to create a new instance of the GradleException class. If you remove the new keyword and just use GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file."), Groovy will interpret this as a reference to a static method on the GradleException class called GradleException(). Since no such method exists, you will get a compilation error.
20

Do not remove the new keyword in the gradle file

This is basically a false positive in VSCode. There is no problem with GradleException, or the new keyword. You can double check by opening the project in Android Studio and verify that GradleException indeed does exist.

If it annoys you that VSCode thinks this is an error, you can probably change GradleException to just Exception.

Comments

12

Replace GradleException with FileNotFoundException

throw new FileNotFoundException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")

This should fix the lint error.

Comments

2

This code has been deprecated and should be migrated using the official documentation.
(I.e. https://docs.flutter.dev/release/breaking-changes/flutter-gradle-plugin-apply )

What worked for me was adding the below code in the android/settings.gradle file get the agpVersion and kotlinVersion from the <app-src/android/build.gradle> file

plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "{agpVersion}" apply false
id "org.jetbrains.kotlin.android" version "{kotlinVersion}" apply false}

Remove the whole buildscript block from <app-src/android/build.gradle>

Remove the following code from <app-src/android/app/build.gradle>:

-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.")
-}

Dependencies in <app-src>/android/build.gradle buildscripts need to be moved to the plugin block of setting.gradle

Remove lines like:

apply plugin: 'com.google.gms.google-services'

apply plugin: 'com.google.firebase.crashlytics'

And add them to <app-src>/android/app/build.gradle like:

 plugins {
     id "com.android.application"
     id "dev.flutter.flutter-gradle-plugin"
     id "org.jetbrains.kotlin.android"
+    id "com.google.gms.google-services"
+    id "com.google.firebase.crashlytics"
 }

Comments

0

I got the error too and tried all the solutions I found on the internet but it didn't work. Then I deleted the things in the extensions and realized the solution was to delete the Gradle for the Java package. In short, delete Gradle for Java from the extensions section. This was the solution for me.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.