Skip to content

Commit

Permalink
Reland "Identify and re-throw our dependency checking errors in flutt…
Browse files Browse the repository at this point in the history
…er.groovy" (#150128)

The original approach in flutter/flutter#149609 didn't work when the Flutter Gradle plugin was applied using the deprecated script apply - the kotlin portion couldn't resolve the custom exception defined in `flutter.groovy`:
```
e: /Users/mackall/development/flutter/flutter/packages/flutter_tools/gradle/src/main/kotlin/dependency_version_checker.gradle.kts:238:23: Unresolved reference: DependencyValidationException
e: /Users/mackall/development/flutter/flutter/packages/flutter_tools/gradle/src/main/kotlin/dependency_version_checker.gradle.kts:263:23: Unresolved reference: DependencyValidationException
e: /Users/mackall/development/flutter/flutter/packages/flutter_tools/gradle/src/main/kotlin/dependency_version_checker.gradle.kts:288:23: Unresolved reference: DependencyValidationException
e: /Users/mackall/development/flutter/flutter/packages/flutter_tools/gradle/src/main/kotlin/dependency_version_checker.gradle.kts:313:23: Unresolved reference: DependencyValidationException
Warning: Flutter was unable to detect project Gradle, Java, AGP, and KGP versions. Skipping dependency version checking. Error was: org.gradle.internal.exceptions.LocationAwareException: Script '/Users/mackall/development/flutter/flutter/packages/flutter_tools/gradle/src/main/kotlin/dependency_version_checker.gradle.kts' line: 238
Script compilation errors:

  Line 238:                 throw DependencyValidationException(errorMessage)
                                  ^ Unresolved reference: DependencyValidationException

  Line 263:                 throw DependencyValidationException(errorMessage)
                                  ^ Unresolved reference: DependencyValidationException

  Line 288:                 throw DependencyValidationException(errorMessage)
                                  ^ Unresolved reference: DependencyValidationException

  Line 313:                 throw DependencyValidationException(errorMessage)
                                  ^ Unresolved reference: DependencyValidationException
```

This new approach of setting one of the [`extra` properties](https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#N14FF7) works in both cases (tested with the `camera_android` example app, which uses the script apply, and a freshly created counter app). 

It also removes some brittleness in that we don't have to unwrap the exception anymore, and aren't subject to breaking if Gradle decides one day to wrap our custom exception 1 layer deeper in additional exceptions.
  • Loading branch information
gmackall authored Jun 13, 2024
1 parent 32df505 commit 7e97cf7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
17 changes: 12 additions & 5 deletions packages/flutter_tools/gradle/src/main/groovy/flutter.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ class FlutterExtension {

return flutterVersionName
}

}

// This buildscript block supplies dependencies for this file's own import
Expand Down Expand Up @@ -340,10 +339,18 @@ class FlutterPlugin implements Plugin<Project> {
"packages", "flutter_tools", "gradle", "src", "main", "kotlin",
"dependency_version_checker.gradle.kts")
project.apply from: dependencyCheckerPluginPath
} catch (Exception ignored) {
project.logger.error("Warning: Flutter was unable to detect project Gradle, Java, " +
"AGP, and KGP versions. Skipping dependency version checking. Error was: "
+ ignored)
} catch (Exception e) {
if (!project.usesUnsupportedDependencyVersions) {
// Possible bug in dependency checking code - warn and do not block build.
project.logger.error("Warning: Flutter was unable to detect project Gradle, Java, " +
"AGP, and KGP versions. Skipping dependency version checking. Error was: "
+ e)
}
else {
// If usesUnsupportedDependencyVersions is set, the exception was thrown by us
// in the dependency version checker plugin so re-throw it here.
throw e
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class DependencyVersionChecker {
private const val AGP_NAME: String = "Android Gradle Plugin"
private const val KGP_NAME: String = "Kotlin"

// String constant that defines the name of the Gradle extra property that we set when
// detecting that the project is using versions outside of Flutter's support range.
// https://docs.gradle.org/current/kotlin-dsl/gradle/org.gradle.api/-project/index.html#-2107180640%2FProperties%2F-1867656071.
private const val OUT_OF_SUPPORT_RANGE_PROPERTY = "usesUnsupportedDependencyVersions"

// The following messages represent best effort guesses at where a Flutter developer should
// look to upgrade a dependency that is below the corresponding threshold. Developers can
// change some of these locations, so they are not guaranteed to be accurate.
Expand Down Expand Up @@ -104,6 +109,7 @@ class DependencyVersionChecker {
* we treat it as within the range for the purpose of this check.
*/
fun checkDependencyVersions(project: Project) {
project.extra.set(OUT_OF_SUPPORT_RANGE_PROPERTY, false)
var agpVersion: Version?
var kgpVersion: Version?

Expand Down Expand Up @@ -235,7 +241,8 @@ class DependencyVersionChecker {
errorGradleVersion.toString(),
getPotentialGradleFix(project.getRootDir().getPath())
)
throw GradleException(errorMessage)
project.extra.set(OUT_OF_SUPPORT_RANGE_PROPERTY, true)
throw DependencyValidationException(errorMessage)
} else if (version < warnGradleVersion) {
val warnMessage: String =
getWarnMessage(
Expand All @@ -260,7 +267,8 @@ class DependencyVersionChecker {
errorJavaVersion.toString(),
POTENTIAL_JAVA_FIX
)
throw GradleException(errorMessage)
project.extra.set(OUT_OF_SUPPORT_RANGE_PROPERTY, true)
throw DependencyValidationException(errorMessage)
} else if (version < warnJavaVersion) {
val warnMessage: String =
getWarnMessage(
Expand All @@ -285,7 +293,8 @@ class DependencyVersionChecker {
errorAGPVersion.toString(),
getPotentialAGPFix(project.getRootDir().getPath())
)
throw GradleException(errorMessage)
project.extra.set(OUT_OF_SUPPORT_RANGE_PROPERTY, true)
throw DependencyValidationException(errorMessage)
} else if (version < warnAGPVersion) {
val warnMessage: String =
getWarnMessage(
Expand All @@ -310,7 +319,8 @@ class DependencyVersionChecker {
errorKGPVersion.toString(),
getPotentialKGPFix(project.getRootDir().getPath())
)
throw GradleException(errorMessage)
project.extra.set(OUT_OF_SUPPORT_RANGE_PROPERTY, true)
throw DependencyValidationException(errorMessage)
} else if (version < warnKGPVersion) {
val warnMessage: String =
getWarnMessage(
Expand Down Expand Up @@ -359,3 +369,9 @@ class Version(val major: Int, val minor: Int, val patch: Int) : Comparable<Versi
return major.toString() + "." + minor.toString() + "." + patch.toString()
}
}

// Custom error for when the dependency_version_checker.kts script finds a dependency out of
// the defined support range.
class DependencyValidationException(message: String? = null, cause: Throwable? = null) : Exception(message, cause) {
constructor(cause: Throwable) : this(null, cause)
}

0 comments on commit 7e97cf7

Please sign in to comment.