Format all kotlin according to ktlint (#143390)

Entire pr generated with [ktlint](https://github.com/pinterest/ktlint) --format. First step before enabling linting as part of presubmit for kotlin changes.
This commit is contained in:
Gray Mackall 2024-02-14 09:58:18 -08:00 committed by GitHub
parent dc1f9f183f
commit c61dc2a586
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 272 additions and 228 deletions

View file

@ -2,5 +2,4 @@ package com.example.a11y_assessments
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity()
}

View file

@ -4,18 +4,13 @@
package dev.flutter.multipleflutters package dev.flutter.multipleflutters
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle import android.os.Bundle
import android.view.View
import android.widget.FrameLayout import android.widget.FrameLayout
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import io.flutter.FlutterInjector import io.flutter.FlutterInjector
import io.flutter.embedding.android.FlutterFragment import io.flutter.embedding.android.FlutterFragment
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.embedding.engine.dart.DartExecutor import io.flutter.embedding.engine.dart.DartExecutor
@ -25,10 +20,11 @@ class MainActivity : FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
val root = LinearLayout(this) val root = LinearLayout(this)
root.layoutParams = LinearLayout.LayoutParams( root.layoutParams =
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT LinearLayout.LayoutParams.MATCH_PARENT,
) LinearLayout.LayoutParams.MATCH_PARENT,
)
root.orientation = LinearLayout.VERTICAL root.orientation = LinearLayout.VERTICAL
root.weightSum = numberOfFlutters.toFloat() root.weightSum = numberOfFlutters.toFloat()
@ -39,21 +35,24 @@ class MainActivity : FragmentActivity() {
val app = applicationContext as App val app = applicationContext as App
val dartEntrypoint = val dartEntrypoint =
DartExecutor.DartEntrypoint( DartExecutor.DartEntrypoint(
FlutterInjector.instance().flutterLoader().findAppBundlePath(), "main" FlutterInjector.instance().flutterLoader().findAppBundlePath(),
"main",
) )
val engines = generateSequence(0) { it + 1 } val engines =
.take(numberOfFlutters) generateSequence(0) { it + 1 }
.map { app.engines.createAndRunEngine(this, dartEntrypoint) } .take(numberOfFlutters)
.toList() .map { app.engines.createAndRunEngine(this, dartEntrypoint) }
.toList()
for (i in 0 until numberOfFlutters) { for (i in 0 until numberOfFlutters) {
val flutterContainer = FrameLayout(this) val flutterContainer = FrameLayout(this)
root.addView(flutterContainer) root.addView(flutterContainer)
flutterContainer.id = 12345 + i flutterContainer.id = 12345 + i
flutterContainer.layoutParams = LinearLayout.LayoutParams( flutterContainer.layoutParams =
FrameLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT,
1.0f FrameLayout.LayoutParams.MATCH_PARENT,
) 1.0f,
)
val engine = engines[i] val engine = engines[i]
FlutterEngineCache.getInstance().put(i.toString(), engine) FlutterEngineCache.getInstance().put(i.toString(), engine)
val flutterFragment = val flutterFragment =
@ -62,7 +61,7 @@ class MainActivity : FragmentActivity() {
.beginTransaction() .beginTransaction()
.add( .add(
12345 + i, 12345 + i,
flutterFragment flutterFragment,
) )
.commit() .commit()
} }

View file

@ -11,28 +11,40 @@ import io.flutter.plugin.common.BinaryCodec
import io.flutter.plugin.common.StandardMessageCodec import io.flutter.plugin.common.StandardMessageCodec
import java.nio.ByteBuffer import java.nio.ByteBuffer
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity() {
// We allow for the caching of a response in the binary channel case since // We allow for the caching of a response in the binary channel case since
// the reply requires a direct buffer, but the input is not a direct buffer. // the reply requires a direct buffer, but the input is not a direct buffer.
// We can't directly send the input back to the reply currently. // We can't directly send the input back to the reply currently.
private var byteBufferCache : ByteBuffer? = null private var byteBufferCache: ByteBuffer? = null
override fun configureFlutterEngine(flutterEngine: FlutterEngine) { override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
val reset = BasicMessageChannel(flutterEngine.dartExecutor, "dev.flutter.echo.reset", StandardMessageCodec.INSTANCE) val reset = BasicMessageChannel(flutterEngine.dartExecutor, "dev.flutter.echo.reset", StandardMessageCodec.INSTANCE)
reset.setMessageHandler { message, reply -> run { reset.setMessageHandler { message, reply ->
byteBufferCache = null run {
} } byteBufferCache = null
val basicStandard = BasicMessageChannel(flutterEngine.dartExecutor, "dev.flutter.echo.basic.standard", StandardMessageCodec.INSTANCE) }
}
val basicStandard =
BasicMessageChannel(flutterEngine.dartExecutor, "dev.flutter.echo.basic.standard", StandardMessageCodec.INSTANCE)
basicStandard.setMessageHandler { message, reply -> reply.reply(message) } basicStandard.setMessageHandler { message, reply -> reply.reply(message) }
val basicBinary = BasicMessageChannel(flutterEngine.dartExecutor, "dev.flutter.echo.basic.binary", BinaryCodec.INSTANCE_DIRECT) val basicBinary = BasicMessageChannel(flutterEngine.dartExecutor, "dev.flutter.echo.basic.binary", BinaryCodec.INSTANCE_DIRECT)
basicBinary.setMessageHandler { message, reply -> run { basicBinary.setMessageHandler { message, reply ->
if (byteBufferCache == null) { run {
byteBufferCache = ByteBuffer.allocateDirect(message!!.capacity()) if (byteBufferCache == null) {
byteBufferCache!!.put(message) byteBufferCache = ByteBuffer.allocateDirect(message!!.capacity())
byteBufferCache!!.put(message)
}
reply.reply(byteBufferCache)
} }
reply.reply(byteBufferCache) }
} } val taskQueue = flutterEngine.dartExecutor.getBinaryMessenger().makeBackgroundTaskQueue()
val taskQueue = flutterEngine.dartExecutor.getBinaryMessenger().makeBackgroundTaskQueue(); val backgroundStandard =
val backgroundStandard = BasicMessageChannel(flutterEngine.dartExecutor, "dev.flutter.echo.background.standard", StandardMessageCodec.INSTANCE, taskQueue) BasicMessageChannel(
flutterEngine.dartExecutor,
"dev.flutter.echo.background.standard",
StandardMessageCodec.INSTANCE,
taskQueue,
)
backgroundStandard.setMessageHandler { message, reply -> reply.reply(message) } backgroundStandard.setMessageHandler { message, reply -> reply.reply(message) }
super.configureFlutterEngine(flutterEngine) super.configureFlutterEngine(flutterEngine)
} }

View file

@ -2,7 +2,6 @@ package com.example.abstract_method_smoke_test
import android.content.Context import android.content.Context
import android.graphics.Color import android.graphics.Color
import android.os.Bundle
import android.view.View import android.view.View
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import androidx.annotation.NonNull import androidx.annotation.NonNull
@ -30,25 +29,34 @@ class MainActivity : FlutterActivity() {
} }
} }
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { override fun configureFlutterEngine(
GeneratedPluginRegistrant.registerWith(flutterEngine); @NonNull flutterEngine: FlutterEngine,
) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
val shimPluginRegistry = ShimPluginRegistry(flutterEngine); val shimPluginRegistry = ShimPluginRegistry(flutterEngine)
shimPluginRegistry.registrarFor("com.example.abstract_method_smoke_test") shimPluginRegistry.registrarFor("com.example.abstract_method_smoke_test")
.platformViewRegistry() .platformViewRegistry()
.registerViewFactory("simple", object : PlatformViewFactory(StandardMessageCodec.INSTANCE) { .registerViewFactory(
override fun create(context: Context?, viewId: Int, args: Any?): PlatformView { "simple",
object : PlatformViewFactory(StandardMessageCodec.INSTANCE) {
override fun create(
context: Context?,
viewId: Int,
args: Any?,
): PlatformView {
return SimplePlatformView(this@MainActivity) return SimplePlatformView(this@MainActivity)
} }
}) },
)
// Triggers the Android keyboard, which causes the resize of the Flutter view. // Triggers the Android keyboard, which causes the resize of the Flutter view.
// We need to wait for the app to complete. // We need to wait for the app to complete.
MethodChannel(flutterEngine.getDartExecutor(), "com.example.abstract_method_smoke_test") MethodChannel(flutterEngine.getDartExecutor(), "com.example.abstract_method_smoke_test")
.setMethodCallHandler { _, result -> .setMethodCallHandler { _, result ->
toggleInput() toggleInput()
result.success(null) result.success(null)
} }
} }
override fun onPause() { override fun onPause() {

View file

@ -1,12 +1,14 @@
package com.example.android_embedding_v2_smoke_test package com.example.android_embedding_v2_smoke_test
import androidx.annotation.NonNull; import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant import io.flutter.plugins.GeneratedPluginRegistrant
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { override fun configureFlutterEngine(
GeneratedPluginRegistrant.registerWith(flutterEngine); @NonNull flutterEngine: FlutterEngine,
) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
} }
} }

View file

@ -2,5 +2,4 @@ package io.flutter.integration.deferred_components_test
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity()
}

View file

@ -2,5 +2,4 @@ package com.example.non_nullable
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity()
}

View file

@ -2,5 +2,4 @@ package com.example.spell_check
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity()
}

View file

@ -2,5 +2,4 @@ package dev.flutter.manual_tests
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity()
}

View file

@ -2,5 +2,4 @@ package com.example.tracing_tests
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity()
}

View file

@ -2,5 +2,4 @@ package dev.flutter.flutter_api_samples
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity()
}

View file

@ -24,7 +24,7 @@ subprojects {
ignoredDependencies.add("io.flutter:*") ignoredDependencies.add("io.flutter:*")
lockFile = file("${rootProject.projectDir}/project-${project.name}.lockfile") lockFile = file("${rootProject.projectDir}/project-${project.name}.lockfile")
if (!project.hasProperty("local-engine-repo")) { if (!project.hasProperty("local-engine-repo")) {
lockAllConfigurations() lockAllConfigurations()
} }
} }
} }

View file

@ -7,13 +7,14 @@
// See #141540. // See #141540.
pluginManagement { pluginManagement {
val flutterSdkPath = run { val flutterSdkPath =
val properties = java.util.Properties() run {
file("local.properties").inputStream().use { properties.load(it) } val properties = java.util.Properties()
val flutterSdkPath = properties.getProperty("flutter.sdk") file("local.properties").inputStream().use { properties.load(it) }
require(flutterSdkPath != null) { "flutter.sdk not set in local.properties" } val flutterSdkPath = properties.getProperty("flutter.sdk")
flutterSdkPath require(flutterSdkPath != null) { "flutter.sdk not set in local.properties" }
} flutterSdkPath
}
includeBuild("$flutterSdkPath/packages/flutter_tools/gradle") includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")

View file

@ -7,7 +7,6 @@ plugins {
`groovy` `groovy`
} }
group = "dev.flutter.plugin" group = "dev.flutter.plugin"
version = "1.0.0" version = "1.0.0"

View file

@ -1,7 +1,7 @@
dependencyResolutionManagement { dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories { repositories {
google() google()
mavenCentral() mavenCentral()
} }
} }

View file

@ -33,50 +33,50 @@ class FlutterDependencyCheckerPlugin : Plugin<Project> {
} }
} }
class DependencyVersionChecker { class DependencyVersionChecker {
companion object { companion object {
private const val GRADLE_NAME : String = "Gradle" private const val GRADLE_NAME: String = "Gradle"
private const val JAVA_NAME : String = "Java" private const val JAVA_NAME: String = "Java"
private const val AGP_NAME : String = "Android Gradle Plugin" private const val AGP_NAME: String = "Android Gradle Plugin"
private const val KGP_NAME : String = "Kotlin" private const val KGP_NAME: String = "Kotlin"
// The following messages represent best effort guesses at where a Flutter developer should // 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 // 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. // change some of these locations, so they are not guaranteed to be accurate.
private fun getPotentialGradleFix(projectDirectory : String) : String { private fun getPotentialGradleFix(projectDirectory: String): String {
return "Your project's gradle version is typically " + return "Your project's gradle version is typically " +
"defined in the gradle wrapper file. By default, this can be found at " + "defined in the gradle wrapper file. By default, this can be found at " +
"$projectDirectory/gradle/wrapper/gradle-wrapper.properties. \n" + "$projectDirectory/gradle/wrapper/gradle-wrapper.properties. \n" +
"For more information, see https://docs.gradle.org/current/userguide/gradle_wrapper.html.\n" "For more information, see https://docs.gradle.org/current/userguide/gradle_wrapper.html.\n"
} }
// The potential java fix does not make use of the project directory, // The potential java fix does not make use of the project directory,
// so it left as a constant. // so it left as a constant.
private const val POTENTIAL_JAVA_FIX : String = "The Java version used by Flutter can be " + private const val POTENTIAL_JAVA_FIX: String =
"The Java version used by Flutter can be " +
"set with `flutter config --jdk-dir=<path>`. \nFor more information about how Flutter " + "set with `flutter config --jdk-dir=<path>`. \nFor more information about how Flutter " +
"chooses which version of Java to use, see the --jdk-dir section of the " + "chooses which version of Java to use, see the --jdk-dir section of the " +
"output of `flutter config -h`.\n" "output of `flutter config -h`.\n"
private fun getPotentialAGPFix(projectDirectory : String) : String { private fun getPotentialAGPFix(projectDirectory: String): String {
return "Your project's AGP version is typically " + return "Your project's AGP version is typically " +
"defined the plugins block of the `settings.gradle` file " + "defined the plugins block of the `settings.gradle` file " +
"($projectDirectory/settings.gradle), by a plugin with the id of " + "($projectDirectory/settings.gradle), by a plugin with the id of " +
"com.android.application. \nIf you don't see a plugins block, your project " + "com.android.application. \nIf you don't see a plugins block, your project " +
"was likely created with an older template version. In this case it is most " + "was likely created with an older template version. In this case it is most " +
"likely defined in the top-level build.gradle file " + "likely defined in the top-level build.gradle file " +
"($projectDirectory/build.gradle) by the following line in the dependencies" + "($projectDirectory/build.gradle) by the following line in the dependencies" +
" block of the buildscript: \"classpath 'com.android.tools.build:gradle:<version>'\".\n" " block of the buildscript: \"classpath 'com.android.tools.build:gradle:<version>'\".\n"
} }
private fun getPotentialKGPFix(projectDirectory : String) : String { private fun getPotentialKGPFix(projectDirectory: String): String {
return "Your project's KGP version is typically " + return "Your project's KGP version is typically " +
"defined the plugins block of the `settings.gradle` file " + "defined the plugins block of the `settings.gradle` file " +
"($projectDirectory/settings.gradle), by a plugin with the id of " + "($projectDirectory/settings.gradle), by a plugin with the id of " +
"org.jetbrains.kotlin.android. \nIf you don't see a plugins block, your project " + "org.jetbrains.kotlin.android. \nIf you don't see a plugins block, your project " +
"was likely created with an older template version, in which case it is most " + "was likely created with an older template version, in which case it is most " +
"likely defined in the top-level build.gradle file " + "likely defined in the top-level build.gradle file " +
"($projectDirectory/build.gradle) by the ext.kotlin_version property.\n" "($projectDirectory/build.gradle) by the ext.kotlin_version property.\n"
} }
// The following versions define our support policy for Gradle, Java, AGP, and KGP. // The following versions define our support policy for Gradle, Java, AGP, and KGP.
@ -86,26 +86,26 @@ class DependencyVersionChecker {
// "warn" version for a full release to provide advanced warning. See // "warn" version for a full release to provide advanced warning. See
// flutter.dev/go/android-dependency-versions for more. // flutter.dev/go/android-dependency-versions for more.
// TODO(gmackall): https://github.com/flutter/flutter/issues/142653. // TODO(gmackall): https://github.com/flutter/flutter/issues/142653.
val warnGradleVersion : Version = Version(7,0,2) val warnGradleVersion: Version = Version(7, 0, 2)
val errorGradleVersion : Version = Version(0,0,0) val errorGradleVersion: Version = Version(0, 0, 0)
val warnJavaVersion : JavaVersion = JavaVersion.VERSION_11 val warnJavaVersion: JavaVersion = JavaVersion.VERSION_11
val errorJavaVersion : JavaVersion = JavaVersion.VERSION_1_1 val errorJavaVersion: JavaVersion = JavaVersion.VERSION_1_1
val warnAGPVersion : Version = Version(7,0,0) val warnAGPVersion: Version = Version(7, 0, 0)
val errorAGPVersion : Version = Version(0,0,0) val errorAGPVersion: Version = Version(0, 0, 0)
val warnKGPVersion : Version = Version(1,5,0) val warnKGPVersion: Version = Version(1, 5, 0)
val errorKGPVersion : Version = Version(0,0,0) val errorKGPVersion: Version = Version(0, 0, 0)
/** /**
* Checks if the project's Android build time dependencies are each within the respective * Checks if the project's Android build time dependencies are each within the respective
* version range that we support. When we can't find a version for a given dependency * version range that we support. When we can't find a version for a given dependency
* we treat it as within the range for the purpose of this check. * we treat it as within the range for the purpose of this check.
*/ */
fun checkDependencyVersions(project : Project) { fun checkDependencyVersions(project: Project) {
var agpVersion : Version? = null var agpVersion: Version? = null
var kgpVersion : Version? = null var kgpVersion: Version? = null
checkGradleVersion(getGradleVersion(project), project) checkGradleVersion(getGradleVersion(project), project)
checkJavaVersion(getJavaVersion(project), project) checkJavaVersion(getJavaVersion(project), project)
@ -113,22 +113,26 @@ class DependencyVersionChecker {
if (agpVersion != null) { if (agpVersion != null) {
checkAGPVersion(agpVersion, project) checkAGPVersion(agpVersion, project)
} else { } else {
project.logger.error("Warning: unable to detect project AGP version. Skipping " + project.logger.error(
"version checking. \nThis may be because you have applied AGP after the Flutter Gradle Plugin.") "Warning: unable to detect project AGP version. Skipping " +
"version checking. \nThis may be because you have applied AGP after the Flutter Gradle Plugin.",
)
} }
kgpVersion = getKGPVersion(project) kgpVersion = getKGPVersion(project)
if (kgpVersion != null) { if (kgpVersion != null) {
checkKGPVersion(kgpVersion, project) checkKGPVersion(kgpVersion, project)
} else { } else {
project.logger.error("Warning: unable to detect project KGP version. Skipping " + project.logger.error(
"version checking. \nThis may be because you have applied KGP after the Flutter Gradle Plugin.") "Warning: unable to detect project KGP version. Skipping " +
"version checking. \nThis may be because you have applied KGP after the Flutter Gradle Plugin.",
)
} }
} }
// https://docs.gradle.org/current/kotlin-dsl/gradle/org.gradle.api.invocation/-gradle/index.html#-837060600%2FFunctions%2F-1793262594 // https://docs.gradle.org/current/kotlin-dsl/gradle/org.gradle.api.invocation/-gradle/index.html#-837060600%2FFunctions%2F-1793262594
fun getGradleVersion(project : Project) : Version { fun getGradleVersion(project: Project): Version {
val untrimmedGradleVersion : String = project.gradle.getGradleVersion() val untrimmedGradleVersion: String = project.gradle.getGradleVersion()
// Trim to handle candidate gradle versions (example 7.6-rc-4). This means we treat all // Trim to handle candidate gradle versions (example 7.6-rc-4). This means we treat all
// candidate versions of gradle as the same as their base version // candidate versions of gradle as the same as their base version
// (i.e., "7.6"="7.6-rc-4"). // (i.e., "7.6"="7.6-rc-4").
@ -136,49 +140,54 @@ class DependencyVersionChecker {
} }
// https://docs.gradle.org/current/kotlin-dsl/gradle/org.gradle.api/-java-version/index.html#-1790786897%2FFunctions%2F-1793262594 // https://docs.gradle.org/current/kotlin-dsl/gradle/org.gradle.api/-java-version/index.html#-1790786897%2FFunctions%2F-1793262594
fun getJavaVersion(project : Project) : JavaVersion { fun getJavaVersion(project: Project): JavaVersion {
return JavaVersion.current() return JavaVersion.current()
} }
// This approach is taken from AGP's own version checking plugin: // This approach is taken from AGP's own version checking plugin:
// https://android.googlesource.com/platform/tools/base/+/1839aa23b8dc562005e2f0f0cc8e8b4c5caa37d0/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/utils/agpVersionChecker.kt#58. // https://android.googlesource.com/platform/tools/base/+/1839aa23b8dc562005e2f0f0cc8e8b4c5caa37d0/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/utils/agpVersionChecker.kt#58.
fun getAGPVersion(project: Project): Version? { fun getAGPVersion(project: Project): Version? {
val agpPluginName : String = "com.android.base"; val agpPluginName: String = "com.android.base"
val agpVersionFieldName : String = "ANDROID_GRADLE_PLUGIN_VERSION" val agpVersionFieldName: String = "ANDROID_GRADLE_PLUGIN_VERSION"
var agpVersion: Version? = null var agpVersion: Version? = null
try { try {
agpVersion = Version.fromString( agpVersion =
project.plugins.getPlugin(agpPluginName)::class.java.classLoader.loadClass( Version.fromString(
com.android.Version::class.java.name project.plugins.getPlugin(agpPluginName)::class.java.classLoader.loadClass(
).fields.find { it.name == agpVersionFieldName }!! com.android.Version::class.java.name,
.get(null) as String ).fields.find { it.name == agpVersionFieldName }!!
) .get(null) as String,
)
} catch (ignored: ClassNotFoundException) { } catch (ignored: ClassNotFoundException) {
// Use deprecated Version class as it exists in older AGP (com.android.Version) does // Use deprecated Version class as it exists in older AGP (com.android.Version) does
// not exist in those versions. // not exist in those versions.
agpVersion = Version.fromString( agpVersion =
project.plugins.getPlugin(agpPluginName)::class.java.classLoader.loadClass( Version.fromString(
com.android.builder.model.Version::class.java.name project.plugins.getPlugin(agpPluginName)::class.java.classLoader.loadClass(
).fields.find { it.name == agpVersionFieldName }!! com.android.builder.model.Version::class.java.name,
.get(null) as String ).fields.find { it.name == agpVersionFieldName }!!
) .get(null) as String,
)
} }
return agpVersion return agpVersion
} }
fun getKGPVersion(project : Project) : Version? { fun getKGPVersion(project: Project): Version? {
val kotlinVersionProperty : String = "kotlin_version" val kotlinVersionProperty: String = "kotlin_version"
val firstKotlinVersionFieldName : String = "pluginVersion" val firstKotlinVersionFieldName: String = "pluginVersion"
val secondKotlinVersionFieldName : String = "kotlinPluginVersion" val secondKotlinVersionFieldName: String = "kotlinPluginVersion"
// This property corresponds to application of the Kotlin Gradle plugin in the // This property corresponds to application of the Kotlin Gradle plugin in the
// top-level build.gradle file. // top-level build.gradle file.
if (project.hasProperty(kotlinVersionProperty)) { if (project.hasProperty(kotlinVersionProperty)) {
return Version.fromString(project.properties.get(kotlinVersionProperty) as String) return Version.fromString(project.properties.get(kotlinVersionProperty) as String)
} }
val kotlinPlugin = project.getPlugins() val kotlinPlugin =
.findPlugin(KotlinAndroidPluginWrapper::class.java) project.getPlugins()
.findPlugin(KotlinAndroidPluginWrapper::class.java)
val versionfield = val versionfield =
kotlinPlugin?.javaClass?.kotlin?.members?.first { it.name == firstKotlinVersionFieldName || it.name == secondKotlinVersionFieldName } kotlinPlugin?.javaClass?.kotlin?.members?.first {
it.name == firstKotlinVersionFieldName || it.name == secondKotlinVersionFieldName
}
val versionString = versionfield?.call(kotlinPlugin) val versionString = versionfield?.call(kotlinPlugin)
if (versionString == null) { if (versionString == null) {
return null return null
@ -187,132 +196,152 @@ class DependencyVersionChecker {
} }
} }
private fun getErrorMessage(dependencyName : String, private fun getErrorMessage(
versionString : String, dependencyName: String,
errorVersion : String, versionString: String,
potentialFix : String) : String { errorVersion: String,
potentialFix: String,
): String {
return "Error: Your project's $dependencyName version ($versionString) is lower " + return "Error: Your project's $dependencyName version ($versionString) is lower " +
"than Flutter's minimum supported version of $errorVersion. Please upgrade " + "than Flutter's minimum supported version of $errorVersion. Please upgrade " +
"your $dependencyName version. \nAlternatively, use the flag " + "your $dependencyName version. \nAlternatively, use the flag " +
"\"--android-skip-build-dependency-validation\" to bypass this check.\n\n"+ "\"--android-skip-build-dependency-validation\" to bypass this check.\n\n" +
"Potential fix: $potentialFix" "Potential fix: $potentialFix"
} }
private fun getWarnMessage(dependencyName : String, private fun getWarnMessage(
versionString : String, dependencyName: String,
warnVersion : String, versionString: String,
potentialFix : String) : String { warnVersion: String,
potentialFix: String,
): String {
return "Warning: Flutter support for your project's $dependencyName version " + return "Warning: Flutter support for your project's $dependencyName version " +
"($versionString) will soon be dropped. Please upgrade your $dependencyName " + "($versionString) will soon be dropped. Please upgrade your $dependencyName " +
"version to a version of at least $warnVersion soon." + "version to a version of at least $warnVersion soon." +
"\nAlternatively, use the flag \"--android-skip-build-dependency-validation\"" + "\nAlternatively, use the flag \"--android-skip-build-dependency-validation\"" +
" to bypass this check.\n\nPotential fix: $potentialFix" " to bypass this check.\n\nPotential fix: $potentialFix"
} }
fun checkGradleVersion(version : Version, project : Project) { fun checkGradleVersion(
version: Version,
project: Project,
) {
if (version < errorGradleVersion) { if (version < errorGradleVersion) {
val errorMessage : String = getErrorMessage( val errorMessage: String =
GRADLE_NAME, getErrorMessage(
version.toString(), GRADLE_NAME,
errorGradleVersion.toString(), version.toString(),
getPotentialGradleFix(project.getRootDir().getPath()) errorGradleVersion.toString(),
) getPotentialGradleFix(project.getRootDir().getPath()),
)
throw GradleException(errorMessage) throw GradleException(errorMessage)
} } else if (version < warnGradleVersion) {
else if (version < warnGradleVersion) { val warnMessage: String =
val warnMessage : String = getWarnMessage( getWarnMessage(
GRADLE_NAME, GRADLE_NAME,
version.toString(), version.toString(),
warnGradleVersion.toString(), warnGradleVersion.toString(),
getPotentialGradleFix(project.getRootDir().getPath()) getPotentialGradleFix(project.getRootDir().getPath()),
) )
project.logger.error(warnMessage) project.logger.error(warnMessage)
} }
} }
fun checkJavaVersion(version : JavaVersion, project : Project) { fun checkJavaVersion(
version: JavaVersion,
project: Project,
) {
if (version < errorJavaVersion) { if (version < errorJavaVersion) {
val errorMessage : String = getErrorMessage( val errorMessage: String =
JAVA_NAME, getErrorMessage(
version.toString(), JAVA_NAME,
errorJavaVersion.toString(), version.toString(),
POTENTIAL_JAVA_FIX errorJavaVersion.toString(),
) POTENTIAL_JAVA_FIX,
)
throw GradleException(errorMessage) throw GradleException(errorMessage)
} } else if (version < warnJavaVersion) {
else if (version < warnJavaVersion) { val warnMessage: String =
val warnMessage : String = getWarnMessage( getWarnMessage(
JAVA_NAME, JAVA_NAME,
version.toString(), version.toString(),
warnJavaVersion.toString(), warnJavaVersion.toString(),
POTENTIAL_JAVA_FIX POTENTIAL_JAVA_FIX,
) )
project.logger.error(warnMessage) project.logger.error(warnMessage)
} }
} }
fun checkAGPVersion(version : Version, project : Project) { fun checkAGPVersion(
version: Version,
project: Project,
) {
if (version < errorAGPVersion) { if (version < errorAGPVersion) {
val errorMessage : String = getErrorMessage( val errorMessage: String =
AGP_NAME, getErrorMessage(
version.toString(), AGP_NAME,
errorAGPVersion.toString(), version.toString(),
getPotentialAGPFix(project.getRootDir().getPath()) errorAGPVersion.toString(),
) getPotentialAGPFix(project.getRootDir().getPath()),
)
throw GradleException(errorMessage) throw GradleException(errorMessage)
} } else if (version < warnAGPVersion) {
else if (version < warnAGPVersion) { val warnMessage: String =
val warnMessage : String = getWarnMessage( getWarnMessage(
AGP_NAME, AGP_NAME,
version.toString(), version.toString(),
warnAGPVersion.toString(), warnAGPVersion.toString(),
getPotentialAGPFix(project.getRootDir().getPath()) getPotentialAGPFix(project.getRootDir().getPath()),
) )
project.logger.error(warnMessage) project.logger.error(warnMessage)
} }
} }
fun checkKGPVersion(version : Version, project : Project) { fun checkKGPVersion(
version: Version,
project: Project,
) {
if (version < errorKGPVersion) { if (version < errorKGPVersion) {
val errorMessage : String = getErrorMessage( val errorMessage: String =
KGP_NAME, getErrorMessage(
version.toString(), KGP_NAME,
errorKGPVersion.toString(), version.toString(),
getPotentialKGPFix(project.getRootDir().getPath()) errorKGPVersion.toString(),
) getPotentialKGPFix(project.getRootDir().getPath()),
)
throw GradleException(errorMessage) throw GradleException(errorMessage)
} } else if (version < warnKGPVersion) {
else if (version < warnKGPVersion) { val warnMessage: String =
val warnMessage : String = getWarnMessage( getWarnMessage(
KGP_NAME, KGP_NAME,
version.toString(), version.toString(),
warnKGPVersion.toString(), warnKGPVersion.toString(),
getPotentialKGPFix(project.getRootDir().getPath()) getPotentialKGPFix(project.getRootDir().getPath()),
) )
project.logger.error(warnMessage) project.logger.error(warnMessage)
} }
} }
} }
} }
// Helper class to parse the versions that are provided as plain strings (Gradle, Kotlin) and // Helper class to parse the versions that are provided as plain strings (Gradle, Kotlin) and
// perform easy comparisons. All versions will have a major, minor, and patch value. These values // perform easy comparisons. All versions will have a major, minor, and patch value. These values
// default to 0 when they are not provided or are otherwise unparseable. // default to 0 when they are not provided or are otherwise unparseable.
// For example the version strings "8.2", "8.2.2hfd", and "8.2.0" would parse to the same version. // For example the version strings "8.2", "8.2.2hfd", and "8.2.0" would parse to the same version.
class Version(val major : Int, val minor : Int, val patch : Int) : Comparable<Version> { class Version(val major: Int, val minor: Int, val patch: Int) : Comparable<Version> {
companion object { companion object {
fun fromString(version : String) : Version { fun fromString(version: String): Version {
val asList : List<String> = version.split(".") val asList: List<String> = version.split(".")
val convertedToNumbers : List<Int> = asList.map {it.toIntOrNull() ?: 0} val convertedToNumbers: List<Int> = asList.map { it.toIntOrNull() ?: 0 }
return Version( return Version(
major = convertedToNumbers.getOrElse(0, {0}), major = convertedToNumbers.getOrElse(0, { 0 }),
minor = convertedToNumbers.getOrElse(1, {0}), minor = convertedToNumbers.getOrElse(1, { 0 }),
patch = convertedToNumbers.getOrElse(2, {0}) patch = convertedToNumbers.getOrElse(2, { 0 }),
) )
} }
} }
override fun compareTo(otherVersion : Version) : Int {
override fun compareTo(otherVersion: Version): Int {
if (major != otherVersion.major) { if (major != otherVersion.major) {
return major - otherVersion.major return major - otherVersion.major
} }
@ -324,7 +353,8 @@ class Version(val major : Int, val minor : Int, val patch : Int) : Comparable<Ve
} }
return 0 return 0
} }
override fun toString() : String {
override fun toString(): String {
return major.toString() + "." + minor.toString() + "." + patch.toString() return major.toString() + "." + minor.toString() + "." + patch.toString()
} }
} }