Priyshrm
Priyshrm

Reputation: 1144

Where is flutter.compileSdkVersion?

While building flutter app, I am getting this error.

One or more plugins require a higher Android SDK version.
Fix this issue by adding the following to C:\flutter\projects\my_app\android\app\build.gradle:
android {
  compileSdkVersion 33
  ...
}

However, in my build.gradle I have :

android {
    compileSdkVersion flutter.compileSdkVersion
...
}

But I am unable to find the location where flutter.compileSdkVersion is defined. Any idea where is this defined? I have already run flutter upgrade and flutter --version returns Flutter 3.0.5.

Upvotes: 39

Views: 23459

Answers (7)

If you want to share flutter's compileSdkVersion with the team, you could use Android Project's gradle.properties for that purpose.

Add the following in gradle.properties:

flutterTargetSdkVersion=35
flutterCompileSdkVersion=35

and change app/build.gradle to:

android {
    namespace "com.xxx.mobile.android"
    
    compileSdkVersion 'android-' + Math.max(flutter.compileSdkVersion, Integer.parseInt(flutterCompileSdkVersion))

    compileOptions {
      ...
    }

    defaultConfig {
      ...
      
      targetSdkVersion Math.max(flutter.targetSdkVersion, Integer.parseInt(flutterTargetSdkVersion))
    }
}

Upvotes: 0

Nerdy Bunz
Nerdy Bunz

Reputation: 7427

If you just want to KNOW what values the flutter configurations are providing (and effectively in use for your particular project), for troubleshooting purposes, you can add this to the end of your app/build.gradle:

task printSdkVersions {
    doLast {
        println "The current compileSdkVersion is: ${android.compileSdkVersion}"
        println "The current targetSdkVersion is: ${android.defaultConfig.targetSdkVersion}"
        println "The current minSdkVersion is: ${android.defaultConfig.minSdkVersion}"
    }
} 

Then, in a terminal at the android folder of your flutter project (the folder containing app and gradle folders), run:

./gradlew printSdkVersions

I would recommend against attempting to track down where these flutter config values are stored... as they reside in multiple places and changing them will affect all future flutter projects or even possibly current ones.

Instead just manually override them in you local project app/build.gradle with integers as necessary... there is nothing special about the flutter values other than they conveniently set intial values for you. You will eventually need to change/override them for one reason or another anyway.

Upvotes: 1

Robert Sandberg
Robert Sandberg

Reputation: 8597

Newer versions of flutter

In recent versions, the setting comes from the flutter installation directory here:

<flutter-installation>/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy

at the top of the file there is a class named FlutterExtension, which sets a number of static variables, e.g.

static int compileSdkVersion = 34

Older versions of flutter

To strictly answer your question, the setting comes from the flutter installation directory here:

<flutter-installation>\packages\flutter_tools\gradle\flutter.gradle

Some options to change the setting:

If you want to have the settings gathered in one place, then you could set it in the file local.properties as:

flutter.compileSdkVersion=33

And you change in your build.gradle in the following way:

android {
  compileSdkVersion localProperties.getProperty('flutter.compileSdkVersion').toInteger()
...
}

or (obviously) just set the version direct as:

android {
  compileSdkVersion 33
...
}

Upvotes: 62

Usama Karim
Usama Karim

Reputation: 1428

In the latest version, the path has been changed from flutter/packages/flutter_tools/gradle/flutter.gradle

to

/flutter/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy

The defaults are following

class FlutterExtension {
    /** Sets the compileSdkVersion used by default in Flutter app projects. */
    static int compileSdkVersion = 33

    /** Sets the minSdkVersion used by default in Flutter app projects. */
    static int minSdkVersion = 19

    /**
     * Sets the targetSdkVersion used by default in Flutter app projects.
     * targetSdkVersion should always be the latest available stable version.
     *
     * See https://developer.android.com/guide/topics/manifest/uses-sdk-element.
     */
    static int targetSdkVersion = 33

    /**
     * Sets the ndkVersion used by default in Flutter app projects.
     * Chosen as default version of the AGP version below as found in
     * https://developer.android.com/studio/projects/install-ndk#default-ndk-per-agp.
     */
    static String ndkVersion = "23.1.7779620"

    /**
     * Specifies the relative directory to the Flutter project directory.
     * In an app project, this is ../.. since the app's build.gradle is under android/app.
     */
    String source

    /** Allows to override the target file. Otherwise, the target is lib/main.dart. */
    String target
}

Upvotes: 4

Rahul
Rahul

Reputation: 4331

Every flutter version has a compiledSdkVersion property along with others. As of Flutter 3.3.8, it is 31.

You can access the code here https://chromium.googlesource.com/external/github.com/flutter/flutter/+/refs/heads/dev/packages/flutter_tools/gradle/flutter.gradle#33

In future it might be more than 33. In case you hardcode value as 33, flutter might stop building with errors.

So the solution should be

compileSdkVersion Math.max(flutter.compileSdkVersion, 33)

now, in future if flutter changes the property to say 35, your code won't break.

Upvotes: 15

Gwhyyy
Gwhyyy

Reputation: 9166

yes, and there you need to change it. replace:

android {
compileSdkVersion flutter.compileSdkVersion
...
}

with this:

 android {
  compileSdkVersion 33
  ...
  }

flutter.compileSdkVersion just the default one got from the flutter sdk.

Upvotes: 0

IonicFireBaseApp
IonicFireBaseApp

Reputation: 1230

flutter.compileSdkVersion is the default version that defaults to installed flutter version configs. You can replace flutter.compileSdkVersion to your own custom version.

Upvotes: 0

Related Questions