From ecc24371860c80c23233f124d2432d513ded0522 Mon Sep 17 00:00:00 2001 From: Mohellebi abdessalem Date: Tue, 5 Nov 2024 15:49:26 +0100 Subject: [PATCH] Readability change to `flutter.groovy`, align on null assignment, reduce unused scope for some methods, apply static where possible (#157471) removed public modifier from this methods :`getVersionCode` , `getVersionName`. add static to :`pluginSupportsAndroidPlatform` ,`buildGradleFile`,`settingsGradleFile` `getCompileSdkFromProject` ,`getAssembleTask` refactor `==null` usage to `:?` to unify the usage see https://github.com/flutter/flutter/issues/147122 for context --- .../gradle/src/main/groovy/flutter.groovy | 51 ++++++------------- 1 file changed, 16 insertions(+), 35 deletions(-) diff --git a/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy b/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy index 031011a7e85e0..ad5f72d9125a0 100644 --- a/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy +++ b/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy @@ -79,7 +79,7 @@ class FlutterExtension { public String flutterVersionName = null /** Returns flutterVersionCode as an integer with error handling. */ - public Integer getVersionCode() { + Integer getVersionCode() { if (flutterVersionCode == null) { throw new GradleException("flutterVersionCode must not be null.") } @@ -92,7 +92,7 @@ class FlutterExtension { } /** Returns flutterVersionName with error handling. */ - public String getVersionName() { + String getVersionName() { if (flutterVersionName == null) { throw new GradleException("flutterVersionName must not be null.") } @@ -271,17 +271,11 @@ class FlutterPlugin implements Plugin { } } - Object flutterVersionCode = localProperties.getProperty("flutter.versionCode") - if (flutterVersionCode == null) { - flutterVersionCode = "1" - } - extension.flutterVersionCode = flutterVersionCode + String flutterVersionCode = localProperties.getProperty("flutter.versionCode") + extension.flutterVersionCode = flutterVersionCode ?: "1" - Object flutterVersionName = localProperties.getProperty("flutter.versionName") - if (flutterVersionName == null) { - flutterVersionName = "1.0" - } - extension.flutterVersionName = flutterVersionName + String flutterVersionName = localProperties.getProperty("flutter.versionName") + extension.flutterVersionName = flutterVersionName ?: "1.0" this.addFlutterTasks(project) @@ -717,7 +711,7 @@ class FlutterPlugin implements Plugin { * Returns `true` if the given project is a plugin project having an `android` directory * containing a `build.gradle` or `build.gradle.kts` file. */ - private Boolean pluginSupportsAndroidPlatform(Project project) { + private static Boolean pluginSupportsAndroidPlatform(Project project) { File buildGradle = new File(project.projectDir.parentFile, "android" + File.separator + "build.gradle") File buildGradleKts = new File(project.projectDir.parentFile, "android" + File.separator + "build.gradle.kts") return buildGradle.exists() || buildGradleKts.exists() @@ -728,7 +722,7 @@ class FlutterPlugin implements Plugin { * Kotlin variants exist, then Groovy (build.gradle) is preferred over * Kotlin (build.gradle.kts). This is the same behavior as Gradle 8.5. */ - private File buildGradleFile(Project project) { + private static File buildGradleFile(Project project) { File buildGradle = new File(project.projectDir.parentFile, "app" + File.separator + "build.gradle") File buildGradleKts = new File(project.projectDir.parentFile, "app" + File.separator + "build.gradle.kts") if (buildGradle.exists() && buildGradleKts.exists()) { @@ -746,7 +740,7 @@ class FlutterPlugin implements Plugin { * Kotlin variants exist, then Groovy (settings.gradle) is preferred over * Kotlin (settings.gradle.kts). This is the same behavior as Gradle 8.5. */ - private File settingsGradleFile(Project project) { + private static File settingsGradleFile(Project project) { File settingsGradle = new File(project.projectDir.parentFile, "settings.gradle") File settingsGradleKts = new File(project.projectDir.parentFile, "settings.gradle.kts") if (settingsGradle.exists() && settingsGradleKts.exists()) { @@ -929,7 +923,7 @@ class FlutterPlugin implements Plugin { * Returns the portion of the compileSdkVersion string that corresponds to either the numeric * or string version. */ - private String getCompileSdkFromProject(Project gradleProject) { + private static String getCompileSdkFromProject(Project gradleProject) { return gradleProject.android.compileSdkVersion.substring(8) } @@ -997,17 +991,9 @@ class FlutterPlugin implements Plugin { if (localProperties == null) { localProperties = readPropertiesIfExist(new File(project.projectDir.parentFile, "local.properties")) } - String result - if (project.hasProperty(name)) { - result = project.property(name) - } - if (result == null) { - result = localProperties.getProperty(name) - } - if (result == null) { - result = defaultValue - } - return result + String result = project.hasProperty(name) ? project.property(name) : null + result = result ?: localProperties?.getProperty(name) + return result ?: defaultValue } private List getTargetPlatforms() { @@ -1071,10 +1057,7 @@ class FlutterPlugin implements Plugin { * Gets the target file. This is typically `lib/main.dart`. */ private String getFlutterTarget() { - String target = project.flutter.target - if (target == null) { - target = "lib/main.dart" - } + String target = project.flutter.target ?: "lib/main.dart" final String propTarget = "target" if (project.hasProperty(propTarget)) { target = project.property(propTarget) @@ -1135,7 +1118,7 @@ class FlutterPlugin implements Plugin { return false } - private Task getAssembleTask(variant) { + private static Task getAssembleTask(variant) { // `assemble` became `assembleProvider` in AGP 3.3.0. return variant.hasProperty("assembleProvider") ? variant.assembleProvider.get() : variant.assemble } @@ -1476,9 +1459,7 @@ class FlutterPlugin implements Plugin { if (buildModeFor(appProjectVariant.buildType) != variantBuildMode) { return } - if (copyFlutterAssetsTask == null) { - copyFlutterAssetsTask = addFlutterDeps(libraryVariant) - } + copyFlutterAssetsTask = copyFlutterAssetsTask ?: addFlutterDeps(libraryVariant) Task mergeAssets = project .tasks .findByPath(":${hostAppProjectName}:merge${appProjectVariant.name.capitalize()}Assets")