In Flutter version 3.22.3
, I have this error when building the app for an Android device:
* What went wrong:
A problem occurred evaluating project ':app'.
> Could not get unknown property 'versionCode' for extension 'flutter' of type FlutterExtension.
I think the error originates from the android\app\build.gradle file.
plugins {
id "com.android.application"
id "kotlin-android"
id "dev.flutter.flutter-gradle-plugin"
}
android {
namespace = "com.example.flutter_application_1"
compileSdk = flutter.compileSdkVersion
ndkVersion = flutter.ndkVersion
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
}
defaultConfig {
applicationId = "com.example.flutter_application_1"
minSdk = flutter.minSdkVersion
targetSdk = flutter.targetSdkVersion
versionCode = flutter.versionCode // ERROR HERE
versionName = flutter.versionName
}
buildTypes {
release {
signingConfig = signingConfigs.debug
}
}
}
flutter {
source = "../.."
}
I found similar problems on StackOverflow where the author had problem with ndkVersion
and the answer recommended changing flutter.ndkVersion
to an explicit one.
Replacing the versionCode
and versionName
with explicit values makes the app launch successfully.
...
versionCode = 1
versionName = "1.0"
...
However, I don't want to do that for the versionCode
, because the versionCode
should be the same for all targets. If I write it explicitly in the build.gradle, for example 1.0.0+1
and the version changes to 1.2.3+4
, I will probably forget to change it manually.
I have to use Flutter version 3.22.3
, I cannot upgrade to version 3.24.0
because of this issue.
How can I fix this so that the versionCode
is accessible from the Flutter extension inside the build.gradle file without any errors?
You need to upgrade to Flutter 3.24.0
https://docs.flutter.dev/release/release-notes/release-notes-3.24.0