[KPM] Implement kotlin-gradle-plugin-kpm-android module for prototyping

This commit is contained in:
sebastian.sellmair
2022-01-19 11:53:55 +01:00
committed by Space
parent 590bf6ed9a
commit d5823a5f7b
10 changed files with 246 additions and 0 deletions
+1
View File
@@ -360,6 +360,7 @@ val projectsWithDisabledFirBootstrap = coreLibProjects + listOf(
val gradlePluginProjects = listOf(
":kotlin-gradle-plugin",
":kotlin-gradle-plugin-api",
":kotlin-gradle-plugin-kpm-android",
":kotlin-allopen",
":kotlin-annotation-processing-gradle",
":kotlin-noarg",
+42
View File
@@ -211,6 +211,11 @@
<sha256 value="160a290f3ebbf2ebe64589cebd70a566ef985a629957e575697ac7285b5fcd38" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.android" name="zipflinger" version="7.0.0">
<artifact name="zipflinger-7.0.0.jar">
<sha256 value="cd8d4638becb47db52ba4ad262bc351fa04b63b28f9004cb450fe2224a171630" origin="Generated by Gradle because artifact wasn't signed"/>
</artifact>
</component>
<component group="com.android.databinding" name="baseLibrary" version="3.0.0">
<artifact name="baseLibrary-3.0.0.jar">
<md5 value="8c1b7f526a6f0ac1d69618fa86f68b96" origin="Generated by Gradle"/>
@@ -595,6 +600,11 @@
<sha256 value="980990434e96bf73e3b7f443edffb71b3577ff4a06f5519b9f16eac8623af8a2" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.android.tools.build" name="apksig" version="7.0.0">
<artifact name="apksig-7.0.0.jar">
<sha256 value="aa7a0b89702d6ad4f007b700073a0399491f89fabe1a970c7aac958f07b3b5e9" origin="Generated by Gradle because artifact wasn't signed"/>
</artifact>
</component>
<component group="com.android.tools.build" name="apkzlib" version="3.4.0">
<artifact name="apkzlib-3.4.0.jar">
<md5 value="dd6747dcfa57156e04fe0f95b4615973" origin="Generated by Gradle"/>
@@ -611,6 +621,11 @@
<sha256 value="777b00b3fdf4ec23df055064f0cec47edc4a1bb4507e8925efcf68282aa6a219" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.android.tools.build" name="apkzlib" version="7.0.0">
<artifact name="apkzlib-7.0.0.jar">
<sha256 value="6810246fdce15c321cbfb732f55bf2b917137bf8c329c487644ac544e973c95d" origin="Generated by Gradle because artifact wasn't signed"/>
</artifact>
</component>
<component group="com.android.tools.build" name="builder" version="0.4.2">
<artifact name="builder-0.4.2.jar">
<md5 value="6e66dcb30be51299f38c54902d703a53" origin="Generated by Gradle"/>
@@ -653,6 +668,11 @@
<sha256 value="309c4de4aa7d46d85874b812d58de5de340c547f4d5eb4377b66b67e78189026" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.android.tools.build" name="builder" version="7.0.0">
<artifact name="builder-7.0.0.jar">
<sha256 value="febbefcac07d699c169d44aa782cd2e55b937e3095b3f41a74166b333a9b4c67" origin="Generated by Gradle because artifact wasn't signed"/>
</artifact>
</component>
<component group="com.android.tools.build" name="builder-model" version="0.4.2">
<artifact name="builder-model-0.4.2.jar">
<md5 value="b382f24e693de565dd37af9515687fde" origin="Generated by Gradle"/>
@@ -689,6 +709,11 @@
<sha256 value="0df48112c60f8327d78794082238ecc5cebb78a7e5c3231c2081cc0ab2263016" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.android.tools.build" name="builder-model" version="7.0.0">
<artifact name="builder-model-7.0.0.jar">
<sha256 value="5d7d296bd3b4b0d4ed11efb2385e1767b1334a3d7388cdfbfcba0e324b6d681d" origin="Generated by Gradle because artifact wasn't signed"/>
</artifact>
</component>
<component group="com.android.tools.build" name="builder-test-api" version="0.4.2">
<artifact name="builder-test-api-0.4.2.jar">
<md5 value="ae2e1c1d4ab8490e8942b9168ed63823" origin="Generated by Gradle"/>
@@ -777,6 +802,11 @@
<sha256 value="5e252829277a43c693fedbf7b28fdffcf835381fac302a79c74b90f1304c77e8" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.android.tools.build" name="gradle" version="7.0.0">
<artifact name="gradle-7.0.0.jar">
<sha256 value="b50fa738ce4743e20a6b1ae6fd53d50179f3541da12a28eb94006b4f7c79f2e9" origin="Generated by Gradle because artifact wasn't signed"/>
</artifact>
</component>
<component group="com.android.tools.build" name="gradle-api" version="3.0.0">
<artifact name="gradle-api-3.0.0.jar">
<md5 value="ee78dc861a3738080239b44dc85ef496" origin="Generated by Gradle"/>
@@ -803,6 +833,11 @@
<sha256 value="6698c4d15afa82d98ee3aea0482718b68dd34f433286a4dfbdfdca7c05678837" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.android.tools.build" name="gradle-api" version="7.0.0">
<artifact name="gradle-api-7.0.0.jar">
<sha256 value="314bcc4a79be0e0c98e7c1801d0c07fd94c7ff8cc865318da6476dde28de9213" origin="Generated by Gradle because artifact wasn't signed"/>
</artifact>
</component>
<component group="com.android.tools.build" name="gradle-core" version="3.0.0">
<artifact name="gradle-core-3.0.0.jar">
<md5 value="efed57189f6981bb347ae8cdc7dda8a6" origin="Generated by Gradle"/>
@@ -859,6 +894,11 @@
<sha256 value="07f502a131cbd79439b7fe61deac3760eae993338be88ef6ddee9714a48e6152" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.android.tools.build" name="manifest-merger" version="30.0.0">
<artifact name="manifest-merger-30.0.0.jar">
<sha256 value="e25e8d6eb580005fbce34fdf480ab899bad41a2f85600e6825193ac2cc9568c6" origin="Generated by Gradle because artifact wasn't signed"/>
</artifact>
</component>
<component group="com.android.tools.build" name="transform-api" version="2.0.0-deprecated-use-gradle-api">
<artifact name="transform-api-2.0.0-deprecated-use-gradle-api.jar">
<md5 value="d1d09b275a0523524baa768664214518" origin="Generated by Gradle"/>
@@ -1861,6 +1901,7 @@
</component>
<component group="com.gradle.enterprise" name="test-distribution-gradle-plugin" version="2.1">
<artifact name="test-distribution-gradle-plugin-2.1.jar">
<pgp value="314fe82e5a4c5377bca2edec5208812e1e4a6db0"/>
<md5 value="d8198138d6ac31b37e6e8c35b9e8ac47" origin="Generated by Gradle"/>
<sha256 value="8e80896b5816499d7245ee384d77f0d152614d8520274fb9b043e59bd33349ea" origin="Generated by Gradle"/>
</artifact>
@@ -5787,6 +5828,7 @@
</component>
<component group="org.gradle" name="test-retry-gradle-plugin" version="1.2.0">
<artifact name="test-retry-gradle-plugin-1.2.0.jar">
<pgp value="314fe82e5a4c5377bca2edec5208812e1e4a6db0"/>
<md5 value="89114b03bb55ade9f5e0cd9bdc08a17d" origin="https://plugins.gradle.org/m2/org/gradle/test-retry-gradle-plugin/1.2.0/test-retry-gradle-plugin-1.2.0.jar.md5"/>
<sha256 value="cff8b303685341472cb80c996bceb142f2a5584c54fd5ab6e09ad611f98d40ee" origin="https://plugins.gradle.org/m2/org/gradle/test-retry-gradle-plugin/1.2.0/"/>
</artifact>
@@ -0,0 +1,27 @@
plugins {
kotlin("jvm")
`java-gradle-plugin`
`maven-publish`
}
dependencies {
implementation(gradleKotlinDsl())
compileOnly(project(":kotlin-gradle-plugin"))
compileOnly("com.android.tools.build:gradle:7.0.0")
}
gradlePlugin {
plugins {
create("kotlinAndroidKpmPlugin") {
id = "kotlin-kpm-android"
implementationClass = "org.jetbrains.kotlin.gradle.android.KotlinAndroidKpmPlugin"
}
}
}
/* This module is just for local development / prototyping and demos */
if (!kotlinBuildProperties.isTeamcityBuild) {
tasks.register("install") {
dependsOn(tasks.named("publishToMavenLocal"))
}
}
@@ -0,0 +1,16 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.android
import com.android.build.gradle.BaseExtension
import org.gradle.api.Project
import org.gradle.api.file.FileCollection
import org.gradle.kotlin.dsl.getByType
import java.util.concurrent.Callable
internal fun Project.getAndroidRuntimeJars(): FileCollection {
return project.files(Callable { project.extensions.getByType<BaseExtension>().bootClasspath })
}
@@ -0,0 +1,30 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@file:Suppress("DEPRECATION")
@file:OptIn(ExternalVariantApi::class)
package org.jetbrains.kotlin.gradle.android
import com.android.build.gradle.AppExtension
import com.android.build.gradle.LibraryExtension
import org.gradle.kotlin.dsl.findByType
import org.jetbrains.kotlin.gradle.plugin.mpp.external.ExternalVariantApi
import org.jetbrains.kotlin.gradle.plugin.mpp.external.project
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinPm20ProjectExtension
fun KotlinPm20ProjectExtension.android() {
project.extensions.findByType<AppExtension>()?.applicationVariants?.all { androidVariant ->
main { createKotlinAndroidVariant(androidVariant) }
test { createKotlinAndroidVariant(androidVariant.unitTestVariant ?: return@test) }
instrumentedTest { createKotlinAndroidVariant(androidVariant.testVariant ?: return@instrumentedTest) }
}
project.extensions.findByType<LibraryExtension>()?.libraryVariants?.all { androidVariant ->
main { createKotlinAndroidVariant(androidVariant) }
test { createKotlinAndroidVariant(androidVariant.unitTestVariant ?: return@test) }
instrumentedTest { createKotlinAndroidVariant(androidVariant.testVariant ?: return@instrumentedTest) }
}
}
@@ -0,0 +1,17 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@file:Suppress("unused")
package org.jetbrains.kotlin.gradle.android
import org.gradle.api.Plugin
import org.gradle.api.Project
class KotlinAndroidKpmPlugin : Plugin<Project> {
override fun apply(target: Project) {
target.logger.quiet("Applied Kotlin/Android KPM prototype. This is just a proof of concept implementation.")
}
}
@@ -0,0 +1,81 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@file:Suppress("DEPRECATION")
@file:OptIn(ExternalVariantApi::class)
package org.jetbrains.kotlin.gradle.android
import com.android.build.api.attributes.BuildTypeAttr
import com.android.build.gradle.api.BaseVariant
import com.android.build.gradle.api.LibraryVariant
import com.android.build.gradle.internal.publishing.AndroidArtifacts
import org.gradle.api.attributes.java.TargetJvmEnvironment
import org.gradle.api.attributes.java.TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE
import org.gradle.kotlin.dsl.named
import org.jetbrains.kotlin.gradle.plugin.mpp.external.ExternalVariantApi
import org.jetbrains.kotlin.gradle.plugin.mpp.external.createExternalJvmVariant
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.*
fun KotlinGradleModule.createKotlinAndroidVariant(androidVariant: BaseVariant) {
val androidOutgoingArtifacts = FragmentArtifacts<KotlinJvmVariant> { fragment ->
variants.create("classes") { variant ->
variant.attributes.attribute(AndroidArtifacts.ARTIFACT_TYPE, AndroidArtifacts.ArtifactType.CLASSES_JAR.type)
variant.artifact(project.provider { fragment.compilationOutputs.classesDirs.singleFile }) {
it.builtBy(fragment.compilationOutputs.classesDirs)
}
}
if (androidVariant is LibraryVariant) {
variants.create("aar") { variant ->
variant.attributes.attribute(AndroidArtifacts.ARTIFACT_TYPE, AndroidArtifacts.ArtifactType.AAR.type)
variant.artifact(androidVariant.packageLibraryProvider)
}
}
}
val androidElementsAttributes = FragmentAttributes<KotlinJvmVariant> {
attribute(BuildTypeAttr.ATTRIBUTE, project.objects.named(androidVariant.buildType.name))
attribute(TARGET_JVM_ENVIRONMENT_ATTRIBUTE, project.objects.named(TargetJvmEnvironment.ANDROID))
}
val kotlinVariant = createExternalJvmVariant(
"android${androidVariant.buildType.name.capitalize()}", KotlinJvmVariantConfig(
/* Only swap out configuration that is used. Default setup shall still be applied */
compileDependencies = DefaultKotlinCompileDependenciesDefinition
.withConfigurationProvider { androidVariant.compileConfiguration },
/* Only swap out configuration that is used. Default setup shall still be applied */
runtimeDependencies = DefaultKotlinRuntimeDependenciesDefinition
.withConfigurationProvider { androidVariant.runtimeConfiguration },
/* Add android artifacts and attributes */
apiElements = DefaultKotlinApiElementsDefinition + androidElementsAttributes + androidOutgoingArtifacts,
/* Add android artifacts and attributes */
runtimeElements = DefaultKotlinRuntimeElementsDefinition + androidElementsAttributes + androidOutgoingArtifacts,
/* For now: Just publish 'release' (non-debuggable) variants */
publicationConfigurator = if (androidVariant.buildType.isDebuggable) KotlinPublicationConfigurator.NoPublication else
KotlinPublicationConfigurator.SingleVariantPublication
)
)
// "Disable" configurations from plain Android plugin
project.configurations.findByName("${androidVariant.name}ApiElements")?.isCanBeConsumed = false
project.configurations.findByName("${androidVariant.name}RuntimeElements")?.isCanBeConsumed = false
// TODO: Move this into configurator!
kotlinVariant.refines(androidCommon)
val mainBytecodeKey = androidVariant.registerPreJavacGeneratedBytecode(
kotlinVariant.compilationOutputs.classesDirs
)
kotlinVariant.compileDependencyFiles = project.files(
androidVariant.getCompileClasspath(mainBytecodeKey),
project.getAndroidRuntimeJars()
)
}
@@ -0,0 +1,14 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.android
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinGradleFragment
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinGradleModule
val KotlinGradleModule.androidCommon: KotlinGradleFragment
get() = fragments.findByName("android") ?: fragments.create("android") { fragment ->
fragment.refines(common)
}
@@ -0,0 +1,16 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.android
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinGradleModule
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinPm20ProjectExtension
val KotlinPm20ProjectExtension.instrumentedTest
get() = modules.maybeCreate("instrumentedTest")
fun KotlinPm20ProjectExtension.instrumentedTest(configure: KotlinGradleModule.() -> Unit) {
instrumentedTest.apply(configure)
}
+2
View File
@@ -232,6 +232,7 @@ include ":benchmarks",
":kotlin-gradle-statistics",
":kotlin-gradle-build-metrics",
":kotlin-gradle-plugin",
":kotlin-gradle-plugin-kpm-android",
":kotlin-gradle-plugin-model",
":kotlin-gradle-plugin-test-utils-embeddable",
":kotlin-gradle-plugin-integration-tests",
@@ -630,6 +631,7 @@ project(':kotlin-gradle-plugin-npm-versions-codegen').projectDir = "$rootDir/lib
project(':kotlin-gradle-statistics').projectDir = "$rootDir/libraries/tools/kotlin-gradle-statistics" as File
project(':kotlin-gradle-build-metrics').projectDir = "$rootDir/libraries/tools/kotlin-gradle-build-metrics" as File
project(':kotlin-gradle-plugin').projectDir = "$rootDir/libraries/tools/kotlin-gradle-plugin" as File
project(':kotlin-gradle-plugin-kpm-android').projectDir = "$rootDir/libraries/tools/kotlin-gradle-plugin-kpm-android" as File
project(':kotlin-gradle-plugin-model').projectDir = "$rootDir/libraries/tools/kotlin-gradle-plugin-model" as File
project(':kotlin-gradle-plugin-test-utils-embeddable').projectDir = "$rootDir/libraries/tools/kotlin-gradle-plugin-test-utils-embeddable" as File
project(':kotlin-gradle-plugin-integration-tests').projectDir = "$rootDir/libraries/tools/kotlin-gradle-plugin-integration-tests" as File