From ec82f0d895244795d0f996efdaa66fb8cec4d1b7 Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Mon, 1 Apr 2024 18:09:13 +0200 Subject: [PATCH] Flutter Gradle Plugin: add versionName and versionCode to FlutterExtension (#146044) This PR is a follow-up of a previous PR of mine: https://github.com/flutter/flutter/pull/141417. It was unfinished, i.e. I only implemented it and used it in `examples/hello_world`. No "flutter create" templates were modified. This change is theoretically breaking, but in practice, I am pretty sure nobody uses this. It was not accounced anywhere, the only app using it is `examples/hello_world`. I did not do that (that=update docs and templates) because I wanted a solution that is idiomatic in both Gradle and Kotlin, and only now I found time to do this. ### Without this change ```groovy defaultConfig { applicationId = "io.flutter.examples.hello_world" minSdk = flutter.minSdkVersion targetSdk = flutter.targetSdkVersion versionCode = flutter.versionCode() versionName = flutter.versionName() } ``` ### With this change ```groovy defaultConfig { applicationId = "io.flutter.examples.hello_world" minSdk = flutter.minSdkVersion targetSdk = flutter.targetSdkVersion versionCode = flutter.versionCode versionName = flutter.versionName } ``` Idiomatic getter - yay! It's consistent between assignment of all four props. ### Issue fixes https://github.com/flutter/flutter/issues/146067 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. --------- Co-authored-by: Reid Baker --- .../flutter_view/android/app/build.gradle | 22 ++----------------- .../hello_world/android/app/build.gradle.kts | 4 ++-- examples/layers/android/app/build.gradle | 4 ++-- .../src/main/groovy/app_plugin_loader.groovy | 2 +- .../gradle/src/main/groovy/flutter.groovy | 4 ++-- 5 files changed, 9 insertions(+), 27 deletions(-) diff --git a/examples/flutter_view/android/app/build.gradle b/examples/flutter_view/android/app/build.gradle index 7113ce5458..7c18e6456f 100644 --- a/examples/flutter_view/android/app/build.gradle +++ b/examples/flutter_view/android/app/build.gradle @@ -7,24 +7,6 @@ plugins { id "dev.flutter.flutter-gradle-plugin" } -def localProperties = new Properties() -def localPropertiesFile = rootProject.file('local.properties') -if (localPropertiesFile.exists()) { - localPropertiesFile.withReader('UTF-8') { reader -> - localProperties.load(reader) - } -} - -def flutterVersionCode = localProperties.getProperty('flutter.versionCode') -if (flutterVersionCode == null) { - flutterVersionCode = '1' -} - -def flutterVersionName = localProperties.getProperty('flutter.versionName') -if (flutterVersionName == null) { - flutterVersionName = '1.0' -} - android { namespace "com.example.view" compileSdk flutter.compileSdkVersion @@ -38,8 +20,8 @@ android { applicationId "io.flutter.examples.flutter_view" minSdkVersion flutter.minSdkVersion targetSdkVersion flutter.targetSdkVersion - versionCode flutterVersionCode.toInteger() - versionName flutterVersionName + versionCode flutter.versionCode + versionName flutter.versionName } buildTypes { diff --git a/examples/hello_world/android/app/build.gradle.kts b/examples/hello_world/android/app/build.gradle.kts index 6732ca6873..175e23095a 100644 --- a/examples/hello_world/android/app/build.gradle.kts +++ b/examples/hello_world/android/app/build.gradle.kts @@ -20,8 +20,8 @@ android { applicationId = "io.flutter.examples.hello_world" minSdk = flutter.minSdkVersion targetSdk = flutter.targetSdkVersion - versionCode = flutter.versionCode() - versionName = flutter.versionName() + versionCode = flutter.versionCode + versionName = flutter.versionName } buildTypes { diff --git a/examples/layers/android/app/build.gradle b/examples/layers/android/app/build.gradle index 185bafc025..87d5fd2ee6 100644 --- a/examples/layers/android/app/build.gradle +++ b/examples/layers/android/app/build.gradle @@ -20,8 +20,8 @@ android { applicationId "io.flutter.examples.layers" minSdkVersion flutter.minSdkVersion targetSdkVersion flutter.targetSdkVersion - versionCode flutter.versionCode() - versionName flutter.versionName() + versionCode flutter.versionCode + versionName flutter.versionName } buildTypes { diff --git a/packages/flutter_tools/gradle/src/main/groovy/app_plugin_loader.groovy b/packages/flutter_tools/gradle/src/main/groovy/app_plugin_loader.groovy index 16965213df..0a03e26999 100644 --- a/packages/flutter_tools/gradle/src/main/groovy/app_plugin_loader.groovy +++ b/packages/flutter_tools/gradle/src/main/groovy/app_plugin_loader.groovy @@ -17,7 +17,7 @@ class FlutterAppPluginLoaderPlugin implements Plugin { settings.ext.flutterSdkPath = properties.getProperty("flutter.sdk") assert settings.ext.flutterSdkPath != null, "flutter.sdk not set in local.properties" } - + // Load shared gradle functions settings.apply from: Paths.get(settings.ext.flutterSdkPath, "packages", "flutter_tools", "gradle", "src", "main", "groovy", "native_plugin_loader.groovy") diff --git a/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy b/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy index 178d61dcce..5f191fe8b0 100644 --- a/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy +++ b/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy @@ -78,7 +78,7 @@ class FlutterExtension { public String flutterVersionName = null /** Returns flutterVersionCode as an integer with error handling. */ - public Integer versionCode() { + public Integer getVersionCode() { if (flutterVersionCode == null) { throw new GradleException("flutterVersionCode must not be null.") } @@ -91,7 +91,7 @@ class FlutterExtension { } /** Returns flutterVersionName with error handling. */ - public String versionName() { + public String getVersionName() { if (flutterVersionName == null) { throw new GradleException("flutterVersionName must not be null.") }