In Flutter, 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 androidappbuild.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.
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.
How can I fix this so that the versionCode
is accessible from the Flutter extension inside the build.gradle file without any errors?