11package com.google.devtools.ksp.gradle
22
3- import com.android.build.api.dsl.KotlinMultiplatformAndroidLibraryTarget
43import com.google.devtools.ksp.gradle.AndroidPluginIntegration.useLegacyVariantApi
54import org.gradle.api.InvalidUserCodeException
65import org.gradle.api.Project
76import org.gradle.api.artifacts.Configuration
87import org.jetbrains.kotlin.gradle.dsl.*
98import org.jetbrains.kotlin.gradle.plugin.*
9+ import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget
1010import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinCommonCompilation
1111import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation
1212
@@ -148,9 +148,7 @@ class KspConfigurations(private val project: Project) {
148148 * and things get worse when you add product flavors. So, we use AGP sets as the source of truth.
149149 */
150150 private fun decorateKotlinTarget (target : KotlinTarget , isKotlinMultiplatform : Boolean ) {
151- if (target.platformType == KotlinPlatformType .androidJvm &&
152- target !is KotlinMultiplatformAndroidLibraryTarget
153- ) {
151+ if (isPlainAndroidTarget(target) || isOldKmpAndroidTarget(target)) {
154152 if (project.useLegacyVariantApi() || isKotlinMultiplatform) {
155153 createAndroidSourceSetConfigurations(target.project, target)
156154 }
@@ -166,6 +164,24 @@ class KspConfigurations(private val project: Project) {
166164 }
167165 }
168166
167+ // check if this target is the old implementation of android kmp target (using com.android.library)
168+ // org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget
169+ private fun isOldKmpAndroidTarget (target : KotlinTarget ): Boolean {
170+ val isKotlinAndroidTargetClass = try {
171+ target is KotlinAndroidTarget
172+ } catch (e: Throwable ) {
173+ false
174+ }
175+ return target.platformType == KotlinPlatformType .androidJvm &&
176+ isMppProject() && isKotlinAndroidTargetClass
177+ }
178+
179+ private fun isPlainAndroidTarget (target : KotlinTarget ): Boolean {
180+ return target.platformType == KotlinPlatformType .androidJvm && ! isMppProject()
181+ }
182+
183+ private fun isMppProject () = project.pluginManager.hasPlugin(" kotlin-multiplatform" )
184+
169185 /* *
170186 * Returns the user-facing configurations involved in the given compilation.
171187 * We use [KotlinCompilation.kotlinSourceSets], not [KotlinCompilation.allKotlinSourceSets] for a few reasons:
@@ -184,9 +200,8 @@ class KspConfigurations(private val project: Project) {
184200 compilation.kotlinSourceSets.mapTo(results) {
185201 getKotlinConfigurationName(compilation, it)
186202 }
187- if (compilation.platformType == KotlinPlatformType .androidJvm &&
188- compilation.target !is KotlinMultiplatformAndroidLibraryTarget
189- ) {
203+
204+ if (isPlainAndroidTarget(compilation.target) || isOldKmpAndroidTarget(compilation.target)) {
190205 compilation as KotlinJvmAndroidCompilation
191206 AndroidPluginIntegration .getCompilationSourceSets(compilation).mapTo(results) {
192207 getAndroidConfigurationName(compilation.target, it)
0 commit comments