Skip to content

Commit edea41e

Browse files
committed
[Gradle] Un-deprecate 'androidTarget()'
We want to allow users to use it with AGP 8.x versions and only show runtime deprecation warning in projects using AGP 9+ versions. This allows users easier to adapt to new changes and migrate when they are ready for these quite significant changes. ^KT-83687 Verification Pending
1 parent 0946452 commit edea41e

21 files changed

Lines changed: 104 additions & 126 deletions

libraries/tools/kotlin-gradle-plugin-dsl-codegen/src/main/kotlin/org/jetbrains/kotlin/generators/gradle/dsl/mppPresetFunctionsCodegen.kt

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,14 @@ private fun KotlinPresetEntry.deprecated(replaceWithArguments: List<String>? = n
9393
"@Deprecated(${deprecation.renderDeprecationMessage()}, level = DeprecationLevel.${deprecation.level.name})"
9494
}
9595

96-
// magic indent is needed to make the result look pretty
97-
return "$deprecationAnnotation\n "
96+
return "$deprecationAnnotation\n"
97+
}
98+
99+
private fun KotlinPresetEntry.kdoc(): String {
100+
val kdoc = kdoc ?: return ""
101+
102+
val kdocLines = kdoc.split("\n")
103+
return kdocLines.joinToString(separator = "\n", prefix = "/**\n", postfix = "\n| */\n") { "| * $it" }
98104
}
99105

100106
private fun KotlinPresetEntry.Deprecation.renderDeprecationMessage(): String = if (messageIsTheCode) {
@@ -113,7 +119,7 @@ private fun generatePresetFunctions(
113119
DeprecationLevel.ERROR -> "DEPRECATION_ERROR"
114120
DeprecationLevel.HIDDEN -> "DEPRECATION_HIDDEN"
115121
}
116-
"@Suppress(\"$suppressDeprecationId\")\n "
122+
"@Suppress(\"$suppressDeprecationId\")\n"
117123
} else {
118124
""
119125
}
@@ -122,22 +128,22 @@ private fun generatePresetFunctions(
122128
val entityName = presetEntry.entityName
123129

124130
return """
125-
${presetEntry.deprecated()}fun $presetName(
126-
name: String = "$entityName",
127-
configure: ${presetEntry.targetType.renderShort()}.() -> Unit = { }
128-
): ${presetEntry.targetType.renderShort()}
129-
130-
${presetEntry.deprecated(emptyList())}${suppress}fun $presetName() = $presetName("$entityName") { }
131-
132-
${presetEntry.deprecated(listOf("name"))}${suppress}fun $presetName(name: String) = $presetName(name) { }
133-
134-
${presetEntry.deprecated()}${suppress}fun $presetName(
135-
name: String,
136-
configure: Action<${presetEntry.targetType.renderShort()}>
137-
) = $presetName(name) { configure.execute(this) }
138-
139-
${presetEntry.deprecated()}${suppress}fun $presetName(configure: Action<${presetEntry.targetType.renderShort()}>) = $presetName { configure.execute(this) }
140-
""".trimIndent()
131+
|${presetEntry.kdoc()}${presetEntry.deprecated()}fun $presetName(
132+
| name: String = "$entityName",
133+
| configure: ${presetEntry.targetType.renderShort()}.() -> Unit = { }
134+
|): ${presetEntry.targetType.renderShort()}
135+
|
136+
|${presetEntry.kdoc()}${presetEntry.deprecated(emptyList())}${suppress}fun $presetName() = $presetName("$entityName") { }
137+
|
138+
|${presetEntry.kdoc()}${presetEntry.deprecated(listOf("name"))}${suppress}fun $presetName(name: String) = $presetName(name) { }
139+
|
140+
|${presetEntry.kdoc()}${presetEntry.deprecated()}${suppress}fun $presetName(
141+
| name: String,
142+
| configure: Action<${presetEntry.targetType.renderShort()}>
143+
|) = $presetName(name) { configure.execute(this) }
144+
|
145+
|${presetEntry.kdoc()}${presetEntry.deprecated()}${suppress}fun $presetName(configure: Action<${presetEntry.targetType.renderShort()}>) = $presetName { configure.execute(this) }
146+
""".trimMargin()
141147
}
142148

143149
private fun generateDefaultPresetFunctionImplementation(
@@ -158,17 +164,17 @@ private fun generateDefaultPresetFunctionImplementation(
158164
val entityName = presetEntry.entityName
159165

160166
return """
161-
${presetEntry.deprecated()}override fun $presetName(
162-
name: String,
163-
configure: ${presetEntry.targetType.renderShort()}.() -> Unit
164-
): ${presetEntry.targetType.renderShort()} =
165-
$configureOrCreateFunctionName(
166-
name,
167-
presets.getByName("$entityName") as ${presetEntry.presetType.renderShort()},
168-
configure
169-
)$alsoBlockAfterConfiguration
170-
171-
""".trimIndent()
167+
|${presetEntry.kdoc()}${presetEntry.deprecated()}override fun $presetName(
168+
| name: String,
169+
| configure: ${presetEntry.targetType.renderShort()}.() -> Unit
170+
|): ${presetEntry.targetType.renderShort()} =
171+
| $configureOrCreateFunctionName(
172+
| name,
173+
| presets.getByName("$entityName") as ${presetEntry.presetType.renderShort()},
174+
| configure
175+
| )$alsoBlockAfterConfiguration
176+
|
177+
""".trimMargin()
172178
}
173179

174180
//language=kotlin

libraries/tools/kotlin-gradle-plugin-dsl-codegen/src/main/kotlin/org/jetbrains/kotlin/generators/gradle/dsl/presetEntries.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ internal class KotlinPresetEntry(
2121
val alsoBlockAfterConfiguration: String? = null,
2222
// Extra declarations will be inserted before functions are generated
2323
val extraTopLevelDeclarations: List<String> = emptyList(),
24+
val kdoc: String? = null,
2425
) {
2526
class Deprecation(
2627
val message: String,
@@ -59,14 +60,13 @@ internal val androidTargetPresetEntry = KotlinPresetEntry(
5960
typeName("$MPP_PACKAGE.KotlinAndroidTargetPreset"),
6061
typeName("$MPP_PACKAGE.KotlinAndroidTarget"),
6162
entityName = "android",
62-
deprecation = KotlinPresetEntry.Deprecation(
63-
message = "The 'org.jetbrains.kotlin.multiplatform' plugin will not be compatible with most of the Android Gradle plugins since " +
64-
"Android Gradle Plugin version 9.0.0.\\n\\nPlease use the 'com.android.kotlin.multiplatform.library' plugin instead. " +
65-
"Read more: https://kotl.in/gradle/agp-new-kmp\\n\\nThe change may require changing the structure of the your project. " +
66-
"Read more: https://kotl.in/kmp-project-structure-migration",
67-
messageIsTheCode = false,
68-
level = DeprecationLevel.WARNING,
69-
)
63+
kdoc = """
64+
The 'org.jetbrains.kotlin.multiplatform' plugin will not be compatible with most of the Android Gradle plugins
65+
from Android Gradle Plugin version 9.0.0.
66+
67+
Please use the 'com.android.kotlin.multiplatform.library' plugin instead. Read more: https://kotl.in/gradle/agp-new-kmp
68+
The change may require changing the structure of your project. Read more: https://kotl.in/kmp-project-structure-migration
69+
""".trimIndent(),
7070
)
7171

7272
// Note: modifying these sets should also be reflected in the MPP plugin code, see 'setupDefaultPresets'

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinTargetContainerWithPresetFunctions.kt

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,55 @@ interface KotlinTargetContainerWithPresetFunctions : KotlinTargetsContainer {
4141

4242
fun jvm(configure: Action<KotlinJvmTarget>) = jvm { configure.execute(this) }
4343

44-
@Deprecated("The 'org.jetbrains.kotlin.multiplatform' plugin will not be compatible with most of the Android Gradle plugins since Android Gradle Plugin version 9.0.0.\n\nPlease use the 'com.android.kotlin.multiplatform.library' plugin instead. Read more: https://kotl.in/gradle/agp-new-kmp\n\nThe change may require changing the structure of the your project. Read more: https://kotl.in/kmp-project-structure-migration", level = DeprecationLevel.WARNING)
44+
/**
45+
* The 'org.jetbrains.kotlin.multiplatform' plugin will not be compatible with most of the Android Gradle plugins
46+
* from Android Gradle Plugin version 9.0.0.
47+
*
48+
* Please use the 'com.android.kotlin.multiplatform.library' plugin instead. Read more: https://kotl.in/gradle/agp-new-kmp
49+
* The change may require changing the structure of your project. Read more: https://kotl.in/kmp-project-structure-migration
50+
*/
4551
fun androidTarget(
4652
name: String = "android",
4753
configure: KotlinAndroidTarget.() -> Unit = { }
4854
): KotlinAndroidTarget
4955

50-
@Deprecated("The 'org.jetbrains.kotlin.multiplatform' plugin will not be compatible with most of the Android Gradle plugins since Android Gradle Plugin version 9.0.0.\n\nPlease use the 'com.android.kotlin.multiplatform.library' plugin instead. Read more: https://kotl.in/gradle/agp-new-kmp\n\nThe change may require changing the structure of the your project. Read more: https://kotl.in/kmp-project-structure-migration", level = DeprecationLevel.WARNING)
51-
@Suppress("DEPRECATION")
56+
/**
57+
* The 'org.jetbrains.kotlin.multiplatform' plugin will not be compatible with most of the Android Gradle plugins
58+
* from Android Gradle Plugin version 9.0.0.
59+
*
60+
* Please use the 'com.android.kotlin.multiplatform.library' plugin instead. Read more: https://kotl.in/gradle/agp-new-kmp
61+
* The change may require changing the structure of your project. Read more: https://kotl.in/kmp-project-structure-migration
62+
*/
5263
fun androidTarget() = androidTarget("android") { }
5364

54-
@Deprecated("The 'org.jetbrains.kotlin.multiplatform' plugin will not be compatible with most of the Android Gradle plugins since Android Gradle Plugin version 9.0.0.\n\nPlease use the 'com.android.kotlin.multiplatform.library' plugin instead. Read more: https://kotl.in/gradle/agp-new-kmp\n\nThe change may require changing the structure of the your project. Read more: https://kotl.in/kmp-project-structure-migration", level = DeprecationLevel.WARNING)
55-
@Suppress("DEPRECATION")
65+
/**
66+
* The 'org.jetbrains.kotlin.multiplatform' plugin will not be compatible with most of the Android Gradle plugins
67+
* from Android Gradle Plugin version 9.0.0.
68+
*
69+
* Please use the 'com.android.kotlin.multiplatform.library' plugin instead. Read more: https://kotl.in/gradle/agp-new-kmp
70+
* The change may require changing the structure of your project. Read more: https://kotl.in/kmp-project-structure-migration
71+
*/
5672
fun androidTarget(name: String) = androidTarget(name) { }
5773

58-
@Deprecated("The 'org.jetbrains.kotlin.multiplatform' plugin will not be compatible with most of the Android Gradle plugins since Android Gradle Plugin version 9.0.0.\n\nPlease use the 'com.android.kotlin.multiplatform.library' plugin instead. Read more: https://kotl.in/gradle/agp-new-kmp\n\nThe change may require changing the structure of the your project. Read more: https://kotl.in/kmp-project-structure-migration", level = DeprecationLevel.WARNING)
59-
@Suppress("DEPRECATION")
74+
/**
75+
* The 'org.jetbrains.kotlin.multiplatform' plugin will not be compatible with most of the Android Gradle plugins
76+
* from Android Gradle Plugin version 9.0.0.
77+
*
78+
* Please use the 'com.android.kotlin.multiplatform.library' plugin instead. Read more: https://kotl.in/gradle/agp-new-kmp
79+
* The change may require changing the structure of your project. Read more: https://kotl.in/kmp-project-structure-migration
80+
*/
6081
fun androidTarget(
6182
name: String,
6283
configure: Action<KotlinAndroidTarget>
6384
) = androidTarget(name) { configure.execute(this) }
6485

65-
@Deprecated("The 'org.jetbrains.kotlin.multiplatform' plugin will not be compatible with most of the Android Gradle plugins since Android Gradle Plugin version 9.0.0.\n\nPlease use the 'com.android.kotlin.multiplatform.library' plugin instead. Read more: https://kotl.in/gradle/agp-new-kmp\n\nThe change may require changing the structure of the your project. Read more: https://kotl.in/kmp-project-structure-migration", level = DeprecationLevel.WARNING)
66-
@Suppress("DEPRECATION")
86+
/**
87+
* The 'org.jetbrains.kotlin.multiplatform' plugin will not be compatible with most of the Android Gradle plugins
88+
* from Android Gradle Plugin version 9.0.0.
89+
*
90+
* Please use the 'com.android.kotlin.multiplatform.library' plugin instead. Read more: https://kotl.in/gradle/agp-new-kmp
91+
* The change may require changing the structure of your project. Read more: https://kotl.in/kmp-project-structure-migration
92+
*/
6793
fun androidTarget(configure: Action<KotlinAndroidTarget>) = androidTarget { configure.execute(this) }
6894

6995
fun androidNativeX64(
@@ -435,7 +461,13 @@ internal abstract class DefaultKotlinTargetContainerWithPresetFunctions @Inject
435461
configure
436462
)
437463

438-
@Deprecated("The 'org.jetbrains.kotlin.multiplatform' plugin will not be compatible with most of the Android Gradle plugins since Android Gradle Plugin version 9.0.0.\n\nPlease use the 'com.android.kotlin.multiplatform.library' plugin instead. Read more: https://kotl.in/gradle/agp-new-kmp\n\nThe change may require changing the structure of the your project. Read more: https://kotl.in/kmp-project-structure-migration", level = DeprecationLevel.WARNING)
464+
/**
465+
* The 'org.jetbrains.kotlin.multiplatform' plugin will not be compatible with most of the Android Gradle plugins
466+
* from Android Gradle Plugin version 9.0.0.
467+
*
468+
* Please use the 'com.android.kotlin.multiplatform.library' plugin instead. Read more: https://kotl.in/gradle/agp-new-kmp
469+
* The change may require changing the structure of your project. Read more: https://kotl.in/kmp-project-structure-migration
470+
*/
439471
override fun androidTarget(
440472
name: String,
441473
configure: KotlinAndroidTarget.() -> Unit

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/diagnostics/AgpWithBuiltInKotlinAppliedCheck.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ internal object AgpWithBuiltInKotlinAppliedCheck {
3838
// it's important to have strict ordering between those 2 checks.
3939
// IncompatibleWithTheNewAgpDsl must not hide AgpWithBuiltInKotlinIsAlreadyApplied
4040
checkIfNewDslIsUsed(isKmpProject = false)
41-
reportKotlinAndroidDeprecation(agpVersionProvider)
41+
reportKotlinAndroidDeprecation(
42+
KotlinToolingDiagnostics.DeprecatedKotlinAndroidPlugin(path),
43+
agpVersionProvider,
44+
)
4245
}
4346

4447
/**
@@ -68,7 +71,8 @@ internal object AgpWithBuiltInKotlinAppliedCheck {
6871
}
6972
}
7073

71-
private fun Project.reportKotlinAndroidDeprecation(
74+
internal fun Project.reportKotlinAndroidDeprecation(
75+
diagnostic: ToolingDiagnostic,
7276
agpVersionProvider: AndroidGradlePluginVersionProvider = AndroidGradlePluginVersionProvider.Default,
7377
) {
7478
val wasChecked = AtomicBoolean(false)
@@ -82,9 +86,7 @@ internal object AgpWithBuiltInKotlinAppliedCheck {
8286
// by other diagnostics like 'KotlinToolingDiagnostics.IncompatibleWithTheNewAgpDsl' or
8387
// 'KotlinToolingDiagnostics.AgpWithBuiltInKotlinIsAlreadyApplied'.
8488
// This diagnostic should be fired at the end of related diagnostics checks and only if the checks are passed.
85-
project.reportDiagnosticOncePerProject(
86-
KotlinToolingDiagnostics.DeprecatedKotlinAndroidPlugin(path)
87-
)
89+
project.reportDiagnosticOncePerProject(diagnostic)
8890
}
8991
}
9092
}

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/diagnostics/KotlinToolingDiagnostics.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,15 +2003,15 @@ internal object KotlinToolingDiagnostics {
20032003
titleStep
20042004
.description(
20052005
"""
2006-
|The 'org.jetbrains.kotlin.multiplatform' plugin will not be compatible with 'com.android.library' starting with Android Gradle Plugin 9.0.0.
2006+
|The 'org.jetbrains.kotlin.multiplatform' plugin is not compatible with 'com.android.library' starting with Android Gradle Plugin 9.0.0.
20072007
""".trimMargin()
20082008
)
20092009
.solution("Please use the 'com.android.kotlin.multiplatform.library' plugin instead of 'com.android.library'.")
20102010
} else {
20112011
titleStep
20122012
.description(
20132013
"""
2014-
|The 'org.jetbrains.kotlin.multiplatform' plugin will not be compatible with '$androidPluginId' starting with Android Gradle Plugin 9.0.0.
2014+
|The 'org.jetbrains.kotlin.multiplatform' plugin is not compatible with '$androidPluginId' starting with Android Gradle Plugin 9.0.0.
20152015
|
20162016
|Please change the structure of the your project and move the usage of '$androidPluginId' into a separate subproject. The new subproject should add a dependency on this KMP subproject.
20172017
|

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/android/KotlinAndroidTargetPreset.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.gradle.plugin.mpp
88

99
import org.gradle.api.Project
1010
import org.jetbrains.kotlin.gradle.internal.diagnostics.AgpWithBuiltInKotlinAppliedCheck.checkIfNewDslIsUsed
11+
import org.jetbrains.kotlin.gradle.internal.diagnostics.AgpWithBuiltInKotlinAppliedCheck.reportKotlinAndroidDeprecation
1112
import org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPlugin.Companion.dynamicallyApplyWhenAndroidPluginIsApplied
1213
import org.jetbrains.kotlin.gradle.plugin.diagnostics.KotlinToolingDiagnostics
1314
import org.jetbrains.kotlin.gradle.plugin.diagnostics.KotlinToolingDiagnostics.AndroidGradlePluginIsMissing
@@ -38,7 +39,9 @@ internal abstract class KotlinAndroidTargetPreset @Inject constructor(
3839
project.reportDiagnostic(AndroidGradlePluginIsMissing(Throwable()))
3940
} else {
4041
project.checkIfNewDslIsUsed(isKmpProject = true)
41-
project.reportDiagnostic(KotlinToolingDiagnostics.NonKmpAgpIsDeprecated(androidPluginId))
42+
project.reportKotlinAndroidDeprecation(
43+
KotlinToolingDiagnostics.NonKmpAgpIsDeprecated(androidPluginId)
44+
)
4245
}
4346

4447
return project.objects.KotlinAndroidTarget(project, name, true).apply {

libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/regressionTests/KT41641AbsentAndroidTarget.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class KT41641AbsentAndroidTarget : MultiplatformExtensionTest() {
4444

4545
// Present android target -> expect no warning message anymore
4646
project.evaluate()
47-
project.assertContainsDiagnostic(KotlinToolingDiagnostics.NonKmpAgpIsDeprecated)
47+
project.assertNoDiagnostics()
4848
}
4949

5050
@Test

libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/diagnosticsTests/AndroidGradlePluginIsMissingTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
package org.jetbrains.kotlin.gradle.unitTests.diagnosticsTests
99

1010
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtension
11-
import org.jetbrains.kotlin.gradle.plugin.diagnostics.KotlinToolingDiagnostics
1211
import org.jetbrains.kotlin.gradle.util.*
1312
import kotlin.test.Test
1413
import kotlin.test.assertFails
@@ -28,6 +27,6 @@ class AndroidGradlePluginIsMissingTest {
2827
project.androidApplication { compileSdk = 33 }
2928
@Suppress("DEPRECATION")
3029
project.multiplatformExtension.androidTarget()
31-
project.assertContainsDiagnostic(KotlinToolingDiagnostics.NonKmpAgpIsDeprecated)
30+
project.assertNoDiagnostics()
3231
}
3332
}

libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/diagnosticsTests/ComposePluginSuggestApplyCheckerTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import org.gradle.api.Project
1010
import org.jetbrains.kotlin.compose.compiler.gradle.ComposeCompilerGradleSubplugin
1111
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtension
1212
import org.jetbrains.kotlin.gradle.plugin.AndroidGradlePluginVersion
13-
import org.jetbrains.kotlin.gradle.plugin.diagnostics.KotlinToolingDiagnostics
1413
import org.jetbrains.kotlin.gradle.util.*
1514
import kotlin.test.Test
1615

@@ -62,7 +61,7 @@ class ComposePluginSuggestApplyCheckerTest {
6261

6362
project.evaluate()
6463

65-
project.assertContainsDiagnostic(KotlinToolingDiagnostics.NonKmpAgpIsDeprecated)
64+
project.assertNoDiagnostics()
6665
}
6766

6867
private fun buildProjectWithKotlinAndroidPlugin(

libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/diagnosticsTests/JvmWithJavaIsIncompatibleWithAndroidDiagnosticTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package org.jetbrains.kotlin.gradle.unitTests.diagnosticsTests
1010
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtension
1111
import org.jetbrains.kotlin.gradle.util.androidApplication
1212
import org.jetbrains.kotlin.gradle.util.androidLibrary
13+
import org.jetbrains.kotlin.gradle.util.assertNoDiagnostics
1314
import org.jetbrains.kotlin.gradle.util.buildProjectWithMPP
1415
import org.jetbrains.kotlin.gradle.util.checkDiagnostics
1516
import kotlin.test.Test
@@ -52,6 +53,6 @@ class JvmWithJavaIsIncompatibleWithAndroidDiagnosticTest {
5253
project.multiplatformExtension.androidTarget()
5354
project.evaluate()
5455

55-
project.checkDiagnostics("JvmWithJavaIsIncompatibleWithAndroid-withJava-withoutAndroidPlugin")
56+
project.assertNoDiagnostics()
5657
}
5758
}

0 commit comments

Comments
 (0)