Import Android modules only if Android Plugin for IDEA is missing

This commit is contained in:
Vyacheslav Karpukhin
2020-01-22 18:11:22 +01:00
parent ad314e93eb
commit 663c8e5a46
2 changed files with 29 additions and 11 deletions
@@ -7,6 +7,8 @@ package org.jetbrains.kotlin.idea.configuration
import com.google.common.graph.GraphBuilder
import com.google.common.graph.Graphs
import com.intellij.ide.plugins.PluginManager
import com.intellij.openapi.extensions.PluginId
import com.intellij.openapi.externalSystem.model.DataNode
import com.intellij.openapi.externalSystem.model.ProjectKeys
import com.intellij.openapi.externalSystem.model.project.*
@@ -34,7 +36,6 @@ import org.jetbrains.kotlin.config.ExternalSystemTestTask
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.gradle.*
import org.jetbrains.kotlin.idea.configuration.GradlePropertiesFileFacade.Companion.KOTLIN_NOT_IMPORTED_COMMON_SOURCE_SETS_SETTING
import org.jetbrains.kotlin.idea.configuration.KotlinMPPGradleProjectResolver.Companion.fullName
import org.jetbrains.kotlin.idea.platform.IdePlatformKindTooling
import org.jetbrains.kotlin.util.removeSuffixIfPresent
import org.jetbrains.kotlin.utils.addToStdlib.firstNotNullResult
@@ -64,6 +65,10 @@ open class KotlinMPPGradleProjectResolver : AbstractProjectResolverExtension() {
return setOf(KotlinMPPGradleModel::class.java)
}
override fun getExtraCommandLineArgs(): List<String> {
return if (!androidPluginPresent) listOf("-Pkotlin.include.android.dependencies=true") else emptyList()
}
override fun createModule(gradleModule: IdeaModule, projectDataNode: DataNode<ProjectData>): DataNode<ModuleData> {
return super.createModule(gradleModule, projectDataNode).also {
initializeModuleData(gradleModule, it, projectDataNode, resolverCtx)
@@ -225,6 +230,7 @@ open class KotlinMPPGradleProjectResolver : AbstractProjectResolverExtension() {
val proxyObjectCloningCache = WeakHashMap<Any, Any>()
private var nativeDebugAdvertised = false
private val androidPluginPresent = PluginManager.getPlugin(PluginId.findId("org.jetbrains.android"))?.isEnabled ?: false
fun initializeModuleData(
gradleModule: IdeaModule,
@@ -276,7 +282,7 @@ open class KotlinMPPGradleProjectResolver : AbstractProjectResolverExtension() {
val sourceSetToCompilationData = LinkedHashMap<KotlinSourceSet, MutableSet<GradleSourceSetData>>()
for (target in mppModel.targets) {
// if (target.platform == KotlinPlatform.ANDROID) continue
if (androidPluginPresent && target.platform == KotlinPlatform.ANDROID) continue
if (target.name == KotlinTarget.METADATA_TARGET_NAME) continue
val targetData = KotlinTargetData(target.name).also {
it.archiveFile = target.jar?.archiveFile
@@ -350,7 +356,7 @@ open class KotlinMPPGradleProjectResolver : AbstractProjectResolverExtension() {
val ignoreCommonSourceSets by lazy { externalProject.notImportedCommonSourceSets() }
for (sourceSet in mppModel.sourceSets.values) {
// if (sourceSet.actualPlatforms.platforms.singleOrNull() == KotlinPlatform.ANDROID) continue
if (androidPluginPresent && sourceSet.actualPlatforms.platforms.singleOrNull() == KotlinPlatform.ANDROID) continue
if (sourceSet.actualPlatforms.supports(KotlinPlatform.COMMON) && ignoreCommonSourceSets) continue
val moduleId = getKotlinModuleId(gradleModule, sourceSet, resolverCtx)
val existingSourceSetDataNode = sourceSetMap[moduleId]?.first
@@ -448,7 +454,10 @@ open class KotlinMPPGradleProjectResolver : AbstractProjectResolverExtension() {
.toMap()
if (resolverCtx.getExtraProject(gradleModule, ExternalProject::class.java) == null) return
processSourceSets(gradleModule, mppModel, ideModule, resolverCtx) { dataNode, sourceSet ->
if (dataNode == null /*|| sourceSet.actualPlatforms.platforms.singleOrNull() == KotlinPlatform.ANDROID*/) return@processSourceSets
if (dataNode == null || androidPluginPresent &&
sourceSet.actualPlatforms.platforms.singleOrNull() == KotlinPlatform.ANDROID
) return@processSourceSets
createContentRootData(
sourceSet.sourceDirs,
sourceSet.sourceType,
@@ -558,7 +567,7 @@ open class KotlinMPPGradleProjectResolver : AbstractProjectResolverExtension() {
}
val closedSourceSetGraph = Graphs.transitiveClosure(sourceSetGraph)
for (sourceSet in closedSourceSetGraph.nodes()) {
val isAndroid = false //sourceSet.actualPlatforms.platforms.singleOrNull() == KotlinPlatform.ANDROID
val isAndroid = androidPluginPresent && sourceSet.actualPlatforms.platforms.singleOrNull() == KotlinPlatform.ANDROID
val fromDataNode = if (isAndroid) {
ideModule
} else {
@@ -578,7 +587,7 @@ open class KotlinMPPGradleProjectResolver : AbstractProjectResolverExtension() {
sourceSetInfo.addSourceSets(dependeeSourceSets, selfName, gradleModule, resolverCtx)
}
}
// if (sourceSet.actualPlatforms.platforms.singleOrNull() == KotlinPlatform.ANDROID) continue
if (androidPluginPresent && sourceSet.actualPlatforms.platforms.singleOrNull() == KotlinPlatform.ANDROID) continue
for (dependeeSourceSet in dependeeSourceSets) {
val toDataNode = getSiblingKotlinModuleData(dependeeSourceSet, gradleModule, ideModule, resolverCtx) ?: continue
addDependency(fromDataNode, toDataNode, dependeeSourceSet.isTestModule)
@@ -747,7 +756,7 @@ open class KotlinMPPGradleProjectResolver : AbstractProjectResolverExtension() {
}
}
for (target in mppModel.targets) {
// if (target.platform == KotlinPlatform.ANDROID) continue
if (androidPluginPresent && target.platform == KotlinPlatform.ANDROID) continue
for (compilation in target.compilations) {
val moduleId = getKotlinModuleId(gradleModule, compilation, resolverCtx)
val moduleDataNode = sourceSetsMap[moduleId] ?: continue
@@ -140,10 +140,7 @@ class KotlinMPPGradleModelBuilder : ModelBuilderService {
@Suppress("UNCHECKED_CAST")
val sourceSets =
(getSourceSets(kotlinExt) as? NamedDomainObjectContainer<Named>)?.asMap?.values ?: emptyList<Named>()
val getAndroidSourceSetDependencies = kotlinExt.javaClass.getMethodOrNull("getAndroidSourceSetDependencies", Project::class.java)
@Suppress("UNCHECKED_CAST") val androidDeps =
getAndroidSourceSetDependencies?.let { it(kotlinExt, project) } as Map<String, List<File>>?
val androidDeps = buildAndroidDeps(kotlinExt, project)
// Some performance optimisation: do not build metadata dependencies if source set is not common
val doBuildMetadataDependencies =
@@ -173,6 +170,18 @@ class KotlinMPPGradleModelBuilder : ModelBuilderService {
}
}
private fun buildAndroidDeps(kotlinExt: Any, project: Project): Map<String, List<File>>? {
val includeAndroidDeps = project.properties["kotlin.include.android.dependencies"]?.toString()?.toBoolean() == true
if (includeAndroidDeps) {
val getAndroidSourceSetDependencies =
kotlinExt.javaClass.getMethodOrNull("getAndroidSourceSetDependencies", Project::class.java)
@Suppress("UNCHECKED_CAST")
return getAndroidSourceSetDependencies?.let { it(kotlinExt, project) } as Map<String, List<File>>?
} else {
return emptyMap()
}
}
private fun buildSourceSet(
gradleSourceSet: Named,
dependencyResolver: DependencyResolver,