Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -533,41 +533,39 @@ object FlutterPluginUtils {
@JvmStatic
@JvmName("detectApplyingKotlinGradlePlugin")
internal fun detectApplyingKotlinGradlePlugin(project: Project) {
val gradlePropertiesFile = project.rootProject.file("gradle.properties")
val properties = readPropertiesIfExist(gradlePropertiesFile)
val isBuiltInKotlinEnabled =
properties.getProperty("android.builtInKotlin")?.lowercase()?.toBoolean() ?: false

if (isBuiltInKotlinEnabled) {
val allSubprojectsDoNotApplyKgp =
project.rootProject.subprojects.all { subproject ->
val pluginState = getSubprojectPluginState(subproject)
if (pluginState == null || (!pluginState.hasAppPlugin && !pluginState.hasLibPlugin)) {
true
} else {
!pluginState.hasKgpPlugin
}
}
if (allSubprojectsDoNotApplyKgp) {
return
}
}

val pluginsWithKGPAppliedList = mutableListOf<String>()

var shouldLogForApp = false
project.rootProject.subprojects {
// Accounts for Add-to-app scenarios where the Flutter Module ephemeral .android/ directory should not be adjusted and by default does not apply KGP
if (!buildFile.exists() || buildFile.absolutePath.contains(".android")) return@subprojects

val scriptText: String =
if (buildFile.absolutePath.contains("app/build.gradle")) {
getBuildGradleFileFromProjectDir(this.projectDir, this.logger).readText()
} else {
buildFile.readText()
}

val (hasKgpPlugin, hasAppPlugin, hasLibPlugin) =
if (buildFile.extension == "kts") {
Triple(
kgpRegexKotlin.containsMatchIn(scriptText),
appPluginRegexKotlin.containsMatchIn(scriptText),
libPluginRegexKotlin.containsMatchIn(scriptText)
)
} else {
Triple(
kgpRegexGroovy.containsMatchIn(scriptText),
appPluginRegexGroovy.containsMatchIn(scriptText),
libPluginRegexGroovy.containsMatchIn(scriptText)
)
}
val pluginState = getSubprojectPluginState(this) ?: return@subprojects

// Ensures applying AGP exists in the build file configuration.
if (!hasAppPlugin && !hasLibPlugin) return@subprojects
if (!pluginState.hasAppPlugin && !pluginState.hasLibPlugin) return@subprojects

if (!hasKgpPlugin) {
if (!pluginState.hasKgpPlugin) {
try {
pluginManager.apply("kotlin-android")
println("applied KGP in FGP")
} catch (_: Exception) {
logger.quiet(
"""
Expand All @@ -581,11 +579,11 @@ object FlutterPluginUtils {
}

// Apply AGP exists and Apply KGP also exists in build.gradle
if (hasAppPlugin) {
if (pluginState.hasAppPlugin) {
shouldLogForApp = true
}

if (hasLibPlugin) {
if (pluginState.hasLibPlugin) {
pluginsWithKGPAppliedList.add(name)
}
}
Expand Down Expand Up @@ -617,6 +615,46 @@ object FlutterPluginUtils {
}
}

private data class SubprojectPluginState(
val hasKgpPlugin: Boolean,
val hasAppPlugin: Boolean,
val hasLibPlugin: Boolean
)

private fun getSubprojectPluginState(subproject: Project): SubprojectPluginState? {
val buildFile = subproject.buildFile
if (!buildFile.exists() || buildFile.absolutePath.contains(".android")) {
return null
}

val scriptText: String =
if (buildFile.absolutePath.contains("app/build.gradle")) {
getBuildGradleFileFromProjectDir(
subproject.projectDir,
subproject.logger
).readText()
} else {
buildFile.readText()
}

val (hasKgpPlugin, hasAppPlugin, hasLibPlugin) =
if (buildFile.extension == "kts") {
Triple(
kgpRegexKotlin.containsMatchIn(scriptText),
appPluginRegexKotlin.containsMatchIn(scriptText),
libPluginRegexKotlin.containsMatchIn(scriptText)
)
} else {
Triple(
kgpRegexGroovy.containsMatchIn(scriptText),
appPluginRegexGroovy.containsMatchIn(scriptText),
libPluginRegexGroovy.containsMatchIn(scriptText)
)
}

return SubprojectPluginState(hasKgpPlugin, hasAppPlugin, hasLibPlugin)
}

/** Prints error message and fix for any plugin compileSdkVersion or ndkVersion that are higher than the project. */
@JvmStatic
@JvmName("detectLowCompileSdkVersionOrNdkVersion")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package com.flutter.gradle

import org.gradle.internal.impldep.org.junit.Assert.assertThrows
import org.junit.jupiter.api.assertThrows
import kotlin.test.Test
import kotlin.test.assertContains
import kotlin.test.assertFalse
Expand Down Expand Up @@ -41,7 +41,7 @@ class DeeplinkTest {
val deeplink1 = Deeplink("scheme1", "host1", "path1", IntentFilterCheck())
val deeplink2 = null

assertThrows(NullPointerException::class.java) { deeplink1.equals(deeplink2) }
assertThrows<NullPointerException> { deeplink1.equals(deeplink2) }
}

@Test
Expand Down
Loading
Loading