How to Fix `flutter.minSdkVersion` Issue in `build.gradle.kts` for Android Projects

When developing a Flutter application, you might encounter issues with the minSdkVersion configuration, particularly when using the Kotlin-based build.gradle.kts file. One common error occurs when trying to reference flutter.minSdkVersion, resulting in build errors such as "Function invocation 'minSdkVersion(...)' expected" or "None of the following functions can be called with the arguments supplied."

Problem Overview

While working with Flutter and Android projects, it's common to set the minimum SDK version for the Android application. Flutter allows the SDK version to be set dynamically through the flutter.minSdkVersion property, but this can sometimes lead to issues if not accessed correctly in the build.gradle.kts.

Here’s an example of a common error:


e: file:///path/to/your/project/android/app/build.gradle.kts:32:9: Function invocation 'minSdkVersion(...)' expected
e: file:///path/to/your/project/android/app/build.gradle.kts:32:9: None of the following functions can be called with the arguments supplied:
    public abstract fun minSdkVersion(minSdkVersion: Int): Unit defined in com.android.build.api.dsl.ApplicationDefaultConfig
            

The Solution

The issue arises because flutter.minSdkVersion is stored in the local.properties file, and it needs to be accessed properly. Here’s how you can fix this error:

Approach 1: Use `flutter.minSdkVersion` from `local.properties`

The first approach involves reading the flutter.minSdkVersion from the local.properties file. This allows for dynamic configuration based on the Flutter environment.

Step 1: Ensure `flutter.minSdkVersion` is Defined in `local.properties`

Make sure the local.properties file in your project contains the following line:

flutter.minSdkVersion=21

Step 2: Update `build.gradle.kts` to Access the Value

Now, in your android/app/build.gradle.kts file, load the flutter.minSdkVersion from the local.properties file and use it as follows:


import java.util.Properties
import java.io.FileInputStream

// Load properties from local.properties
val localProperties = rootProject.file("local.properties")
val flutterProperties = Properties()
flutterProperties.load(localProperties.inputStream())

// Get the flutter.minSdkVersion value from the properties
val minSdkVersion = flutterProperties.getProperty("flutter.minSdkVersion")?.toInt() ?: 21 // Default to 21 if not found

android {
    namespace = "com.example.flutterproject"  // Dummy app name
    compileSdk = flutter.compileSdkVersion
    ndkVersion = "29.0.13113456"  // flutter.ndkVersion

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_11.toString()
    }

    defaultConfig {
        applicationId = "com.example.flutterproject"  // Dummy app ID
        minSdkVersion(21)  // Correctly reference the minSdkVersion
        targetSdk = flutter.targetSdkVersion
        versionCode = 1
        versionName = "1.0.0"  // Dummy version name
    }

    signingConfigs {
        create("release") {
            keyAlias = "dummyAlias"
            keyPassword = "dummyPassword"
            storeFile = file("dummyPath")
            storePassword = "dummyStorePassword"
        }
    }

    buildTypes {
        release {
            signingConfig = signingConfigs.getByName("release")
        }
    }
}
            

Explanation:

  • localProperties: We load the local.properties file to read the flutter.minSdkVersion value.
  • minSdkVersion(minSdkVersion): We then use this value to set the minimum SDK version dynamically.

Approach 2: Hardcode the `minSdkVersion`

The second approach is more straightforward: you can simply hardcode the minSdkVersion value directly in the build.gradle.kts file. This approach is simple and works well when you have a fixed minimum SDK version that doesn't change often.

Step 1: Directly Set `minSdkVersion` in `build.gradle.kts`

In your android/app/build.gradle.kts file, replace minSdkVersion with a fixed integer value:


android {
    defaultConfig {
        applicationId = "com.example.flutterproject"  // Dummy app ID
        minSdkVersion = 21  // Hardcoded value for minSdkVersion
        targetSdk = 30
        versionCode = 1
        versionName = "1.0.0"  // Dummy version name
    }
}
            

Explanation:

  • minSdkVersion = 21: This approach directly assigns a hardcoded value of `21` for the minimum SDK version. You can replace `21` with any value you need.
  • Simplicity: This approach is simple and straightforward, but it lacks flexibility if you need to update the SDK version dynamically.

Conclusion

Both approaches will allow you to resolve the flutter.minSdkVersion issue in your build.gradle.kts file:

  • Approach 1: Use the flutter.minSdkVersion from the local.properties file for a more dynamic configuration.
  • Approach 2: Hardcode the minSdkVersion directly for simplicity, but at the cost of flexibility.

Choose the approach that works best for your project, and you'll be able to avoid common build errors related to the minSdkVersion setting.

Post a Comment