Skip to content

Commit 8764eec

Browse files
committed
Fix publication WIP
1 parent 9fa322f commit 8764eec

File tree

10 files changed

+170
-120
lines changed

10 files changed

+170
-120
lines changed

buildSrc/private/src/main/kotlin/androidx/build/AndroidXImplPlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ abstract class AndroidXImplPlugin @Inject constructor() : Plugin<Project> {
218218
it.configureWithAndroidXExtension(androidXExtension)
219219
}
220220
project.configureConstraintsWithinGroup(androidXExtension)
221-
project.validateProjectParser(androidXExtension)
221+
if (!org.jetbrains.androidx.build.isJetBrainsForkStructureEnabled(project)) project.validateProjectParser(androidXExtension)
222222
project.validateAllArchiveInputsRecognized()
223223
project.afterEvaluate {
224224
if (androidXExtension.shouldPublishSbom()) {

buildSrc/private/src/main/kotlin/org/jetbrains/androidx/build/JetBrainsAndroidXImplPlugin.kt

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
package org.jetbrains.androidx.build
2020

21-
import androidx.build.AndroidXExtension
22-
import androidx.build.AndroidXMultiplatformExtension
2321
import androidx.build.multiplatformExtension
2422
import javax.inject.Inject
2523
import kotlinx.validation.ApiValidationExtension
@@ -29,12 +27,14 @@ import org.gradle.api.Project
2927
import org.gradle.api.component.SoftwareComponentFactory
3028
import org.gradle.kotlin.dsl.apply
3129
import org.gradle.kotlin.dsl.create
30+
import org.jetbrains.kotlin.gradle.ExternalKotlinTargetApi
3231
import org.jetbrains.kotlin.gradle.InternalKotlinGradlePluginApi
3332
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
3433
import org.jetbrains.kotlin.gradle.plugin.KotlinMultiplatformPluginWrapper
3534
import org.jetbrains.kotlin.gradle.plugin.mpp.AbstractKotlinTarget
3635
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
3736
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinSoftwareComponentWithCoordinatesAndPublication
37+
import org.jetbrains.kotlin.gradle.plugin.mpp.external.DecoratedExternalKotlinTarget
3838
import org.jetbrains.kotlin.konan.target.KonanTarget
3939

4040
open class JetBrainsExtensions(
@@ -139,22 +139,15 @@ class JetBrainsAndroidXImplPlugin @Inject constructor(
139139

140140
@Suppress("UNREACHABLE_CODE", "UNUSED_VARIABLE")
141141
override fun apply(project: Project) {
142-
check(project.plugins.hasPlugin("AndroidXPlugin")) {
143-
"JetBrainsAndroidXPlugin should be applied after AndroidXPlugin"
144-
}
145-
146-
val androidxExtension =
147-
project.extensions.getByType(AndroidXExtension::class.java)
148-
val androidxMultiplatformExtension =
149-
project.extensions.getByType(AndroidXMultiplatformExtension::class.java)
150-
project.changeMavenCoordinatesToJetBrains(androidxExtension)
151-
project.configureMavenArtifactUpload(
152-
androidxExtension, androidxMultiplatformExtension, componentFactory)
153-
project.configureDependencyVerification()
142+
if (isJetBrainsForkStructureEnabled(project)) {
143+
project.changeMavenCoordinatesToJetBrains()
144+
project.configureMavenArtifactUpload(componentFactory)
145+
project.configureDependencyVerification()
154146

155-
project.plugins.all { plugin ->
156-
if (plugin is KotlinMultiplatformPluginWrapper) {
157-
onKotlinMultiplatformPluginApplied(project)
147+
project.plugins.all { plugin ->
148+
if (plugin is KotlinMultiplatformPluginWrapper) {
149+
onKotlinMultiplatformPluginApplied(project)
150+
}
158151
}
159152
}
160153
}
@@ -178,11 +171,8 @@ class JetBrainsAndroidXImplPlugin @Inject constructor(
178171
}
179172
}
180173

174+
@OptIn(ExternalKotlinTargetApi::class)
181175
private fun enableArtifactRedirectionPublishing(project: Project) {
182-
/*
183-
FIXME: Deprecation + warnings as errors
184-
'fun DependencyHandler.create(...): ExternalModuleDependency' is deprecated. Use single-string notation instead.
185-
186176
val redirection = project.artifactRedirection() ?: return
187177

188178
val ext = project.multiplatformExtension ?: error("expected a multiplatform project")
@@ -199,19 +189,20 @@ private fun enableArtifactRedirectionPublishing(project: Project) {
199189
configuration.name.startsWith(it, ignoreCase = true)
200190
}
201191
val targetVersion = redirection.versionForTargetOrDefault(targetName ?: "")
202-
project.dependencies.create(
203-
redirection.groupId, project.name, targetVersion
204-
)
192+
project.dependencies.create("${redirection.groupId}:${project.name}:${targetVersion}") as org.gradle.api.artifacts.ModuleDependency
205193
}
206194
}
207195

208196
@OptIn(InternalKotlinGradlePluginApi::class)
209197
ext.targets.all { target ->
210198
if (target.name.lowercase() in redirection.targetNames) {
211-
project.publishAndroidxReference(target as AbstractKotlinTarget, newRootComponent)
199+
if (target is AbstractKotlinTarget) {
200+
project.setupRedirection(target, newRootComponent)
201+
} else if (target is DecoratedExternalKotlinTarget) {
202+
project.setupRedirection(target, newRootComponent)
203+
}
212204
}
213205
}
214-
*/
215206
}
216207

217208
@OptIn(ExperimentalBCVApi::class)

buildSrc/private/src/main/kotlin/org/jetbrains/androidx/build/JetBrainsAndroidXRedirectingPublicationHelpers.kt

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.jetbrains.androidx.build
1818

19+
import com.android.utils.mapValuesNotNull
1920
import org.gradle.api.Project
2021
import org.jetbrains.kotlin.gradle.plugin.mpp.*
2122
import org.gradle.api.publish.PublishingExtension
@@ -33,11 +34,16 @@ import org.gradle.api.capabilities.Capability
3334
import org.gradle.api.component.ComponentWithCoordinates
3435
import org.gradle.api.component.ComponentWithVariants
3536
import org.gradle.api.component.SoftwareComponent
37+
import org.gradle.api.internal.artifacts.DefaultModuleIdentifier
38+
import org.gradle.api.internal.artifacts.DefaultModuleVersionIdentifier
3639
import org.gradle.api.internal.component.SoftwareComponentInternal
3740
import org.gradle.api.internal.component.UsageContext
3841
import org.gradle.api.publish.maven.MavenPublication
3942
import org.gradle.api.publish.maven.tasks.AbstractPublishToMaven
43+
import org.jetbrains.kotlin.gradle.ExternalKotlinTargetApi
4044
import org.jetbrains.kotlin.gradle.InternalKotlinGradlePluginApi
45+
import org.jetbrains.kotlin.gradle.plugin.KotlinTargetComponent
46+
import org.jetbrains.kotlin.gradle.plugin.mpp.external.DecoratedExternalKotlinTarget
4147

4248
/**
4349
* Usage that should be added to rootSoftwareComponent to represent target-specific variants
@@ -57,8 +63,17 @@ internal class CustomUsage(
5763
override fun getGlobalExcludes(): Set<ExcludeRule> = emptySet()
5864
}
5965

66+
@OptIn(InternalKotlinGradlePluginApi::class, ExternalKotlinTargetApi::class)
67+
internal fun Project.setupRedirection(target: DecoratedExternalKotlinTarget, newRootComponent: CustomRootComponent) {
68+
setupRedirection(target.kotlinComponents, newRootComponent)
69+
}
70+
@OptIn(InternalKotlinGradlePluginApi::class)
71+
internal fun Project.setupRedirection(target: AbstractKotlinTarget, newRootComponent: CustomRootComponent) {
72+
setupRedirection(target.kotlinComponents, newRootComponent)
73+
}
74+
6075
@OptIn(InternalKotlinGradlePluginApi::class)
61-
internal fun Project.publishAndroidxReference(target: AbstractKotlinTarget, newRootComponent: CustomRootComponent) {
76+
internal fun Project.setupRedirection(kotlinComponents: Set<KotlinTargetComponent>, newRootComponent: CustomRootComponent) {
6277
afterEvaluate {
6378
extensions.getByType(PublishingExtension::class.java).apply {
6479
val kotlinMultiplatform = publications
@@ -79,7 +94,7 @@ internal fun Project.publishAndroidxReference(target: AbstractKotlinTarget, newR
7994
if (it.publication.name == "kotlinMultiplatform") it.enabled = false
8095
}
8196

82-
target.kotlinComponents.forEach { component ->
97+
kotlinComponents.forEach { component ->
8398
val componentName = component.name
8499

85100
if (component is KotlinVariant)
@@ -185,10 +200,8 @@ internal fun Project.originalToRedirectedDependency(
185200
* org.jetbrains.compose.collection-internal:collection-jvm=androidx.collection:collection-jvm:1.5.0-beta01
186201
* ...
187202
*/
188-
/* FIXME: Restore after fixing for new buildSrc state
189203
val projectDefined =
190-
getProjectsMap()
191-
.values
204+
JetBrainsPublication.projectPathToLibrary.keys
192205
.mapNotNull { project.findProject(it) }
193206
.flatMap { project ->
194207
val redirecting = project.artifactRedirection() ?: return@flatMap emptyList()
@@ -206,7 +219,6 @@ internal fun Project.originalToRedirectedDependency(
206219
}
207220
}
208221
.associate { it }
209-
*/
210222

211223
fun mainConfiguration() =
212224
configurations.find { it.name == "${componentName}RuntimeClasspath" } ?:
@@ -226,7 +238,6 @@ internal fun Project.originalToRedirectedDependency(
226238
* https://youtrack.jetbrains.com/issue/CMP-7764/Redirection-of-artifacts-breaks-poms-for-multiplatform-libraries-that-use-them
227239
* After it is resolved, externalWithHeuristic shouldn't be needed.
228240
*/
229-
/* FIXME: Restore after fixing for new buildSrc state
230241
val externalWithHeuristic = mainConfiguration()
231242
.resolvedConfiguration
232243
.firstLevelModuleDependencies
@@ -235,6 +246,4 @@ internal fun Project.originalToRedirectedDependency(
235246
.mapValuesNotNull { it.value.findRedirectedDependencyHeuristically()?.module?.id }
236247

237248
return projectDefined + externalWithHeuristic
238-
*/
239-
return emptyMap()
240249
}

buildSrc/private/src/main/kotlin/org/jetbrains/androidx/build/JetBrainsAndroidXRootImplPlugin.kt

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
package org.jetbrains.androidx.build
2020

21+
import androidx.build.AndroidXExtension
22+
import androidx.build.Publish
23+
import androidx.build.RunApiTasks
24+
import androidx.build.SoftwareType.ConfigurableSoftwareType
2125
import javax.inject.Inject
2226
import org.gradle.api.Plugin
2327
import org.gradle.api.Project
@@ -29,16 +33,30 @@ class JetBrainsAndroidXRootImplPlugin @Inject constructor(
2933
val componentFactory: SoftwareComponentFactory
3034
) : Plugin<Project> {
3135
override fun apply(project: Project) {
32-
project.allprojects {
33-
it.tasks.configureEach {
36+
project.allprojects { subproject ->
37+
subproject.tasks.configureEach {
3438
if (it.name == "kotlinStoreYarnLock") it.enabled = false
3539
if (it.name == "kotlinWasmStoreYarnLock") it.enabled = false
3640
}
3741

3842
// Never cache test results
39-
it.tasks.withType<AbstractTestTask>().configureEach {
43+
subproject.tasks.withType<AbstractTestTask>().configureEach {
4044
it.outputs.upToDateWhen { false }
4145
}
46+
47+
// Disable Androidx publication, as the fork publication is configured
48+
// by JetBrainsPublication.
49+
//
50+
// It disables Androidx checks for publishing libraries that are not needed or
51+
// conflict with the fork publication
52+
subproject.afterEvaluate {
53+
val androidxExtension = subproject.extensions.findByType(AndroidXExtension::class.java)
54+
androidxExtension?.type = ConfigurableSoftwareType(
55+
name = "JB Library",
56+
publish = Publish.NONE,
57+
checkApi = RunApiTasks.No("JB Library"),
58+
)
59+
}
4260
}
4361
}
4462
}

buildSrc/private/src/main/kotlin/org/jetbrains/androidx/build/JetBrainsMavenCoordinatesChanger.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,21 @@ import androidx.build.AndroidXExtension
2020
import androidx.build.Version
2121
import org.gradle.api.Project
2222

23-
fun Project.changeMavenCoordinatesToJetBrains(androidxExtension: AndroidXExtension) {
23+
fun Project.changeMavenCoordinatesToJetBrains() {
2424
// we are interested in changing coordinates only for what we publish
2525
val component = JetBrainsPublication.projectPathToComponent[path] ?: return
2626
val versions = JetBrainsVersionsService.versions(project)
27+
val androidxExtension = project.extensions.getByType(AndroidXExtension::class.java)
2728

28-
group = JetBrainsPublication.mavenGroupFor(path, androidxExtension.mavenGroup?.group) ?: group
29-
version = Version(versions.versionOf(component.library()))
29+
val group = JetBrainsPublication.mavenGroupFor(path, androidxExtension.mavenGroup?.group) ?: group
30+
val version = Version(versions.versionOf(component.library()))
31+
this.group = group
32+
this.version = version
33+
34+
// we need to set them again in case they are overridden in build.gradle
35+
// for example, version can be overriden via `androidx { mavenVersion = ... }`
36+
afterEvaluate {
37+
this.group = group
38+
this.version = version
39+
}
3040
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.jetbrains.androidx.build
18+
19+
import org.gradle.api.Project
20+
21+
/**
22+
* True if the JetBrains build code is added:
23+
* - publication
24+
* - version/group changes
25+
* - JB-only API validation
26+
*/
27+
fun isJetBrainsForkStructureEnabled(project: Project) =
28+
project.findProperty("jetbrains.forkStructureEnabled") == "true"

0 commit comments

Comments
 (0)