-
-
Notifications
You must be signed in to change notification settings - Fork 340
/
PlayPublisherPlugin.kt
722 lines (660 loc) · 34.3 KB
/
PlayPublisherPlugin.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
package com.github.triplet.gradle.play
import com.android.build.api.artifact.SingleArtifact
import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import com.android.build.gradle.AppPlugin
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import com.github.triplet.gradle.androidpublisher.ResolutionStrategy
import com.github.triplet.gradle.common.utils.safeMkdirs
import com.github.triplet.gradle.common.validation.validateRuntime
import com.github.triplet.gradle.play.internal.CliPlayPublisherExtension
import com.github.triplet.gradle.play.internal.INTERMEDIATES_OUTPUT_PATH
import com.github.triplet.gradle.play.internal.OUTPUT_PATH
import com.github.triplet.gradle.play.internal.PLAY_CONFIGS_PATH
import com.github.triplet.gradle.play.internal.PLAY_PATH
import com.github.triplet.gradle.play.internal.PRODUCTS_PATH
import com.github.triplet.gradle.play.internal.RELEASE_NAMES_PATH
import com.github.triplet.gradle.play.internal.RELEASE_NOTES_PATH
import com.github.triplet.gradle.play.internal.SUBSCRIPTIONS_PATH
import com.github.triplet.gradle.play.internal.buildExtension
import com.github.triplet.gradle.play.internal.flavorNameOrDefault
import com.github.triplet.gradle.play.internal.generateExtensionOverrideOrdering
import com.github.triplet.gradle.play.internal.getCommitEditTask
import com.github.triplet.gradle.play.internal.newTask
import com.github.triplet.gradle.play.internal.playPath
import com.github.triplet.gradle.play.internal.toConfig
import com.github.triplet.gradle.play.internal.toPriority
import com.github.triplet.gradle.play.internal.validateDebuggability
import com.github.triplet.gradle.play.tasks.Bootstrap
import com.github.triplet.gradle.play.tasks.GenerateResources
import com.github.triplet.gradle.play.tasks.InstallInternalSharingArtifact
import com.github.triplet.gradle.play.tasks.ProcessArtifactVersionCodes
import com.github.triplet.gradle.play.tasks.PromoteRelease
import com.github.triplet.gradle.play.tasks.PublishApk
import com.github.triplet.gradle.play.tasks.PublishBundle
import com.github.triplet.gradle.play.tasks.PublishInternalSharingApk
import com.github.triplet.gradle.play.tasks.PublishInternalSharingBundle
import com.github.triplet.gradle.play.tasks.PublishListings
import com.github.triplet.gradle.play.tasks.PublishProducts
import com.github.triplet.gradle.play.tasks.PublishSubscriptions
import com.github.triplet.gradle.play.tasks.internal.BootstrapLifecycleTask
import com.github.triplet.gradle.play.tasks.internal.BootstrapOptions
import com.github.triplet.gradle.play.tasks.internal.GlobalPublishableArtifactLifecycleTask
import com.github.triplet.gradle.play.tasks.internal.GlobalUploadableArtifactLifecycleTask
import com.github.triplet.gradle.play.tasks.internal.PlayApiService
import com.github.triplet.gradle.play.tasks.internal.PublishArtifactTaskBase
import com.github.triplet.gradle.play.tasks.internal.PublishTaskBase
import com.github.triplet.gradle.play.tasks.internal.PublishableTrackLifecycleTask
import com.github.triplet.gradle.play.tasks.internal.UpdatableTrackLifecycleTask
import com.github.triplet.gradle.play.tasks.internal.WriteTrackLifecycleTask
import org.gradle.api.DomainObjectSet
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.plugins.ExtensionAware
import org.gradle.api.provider.Provider
import org.gradle.api.services.BuildServiceRegistration
import org.gradle.build.event.BuildEventsListenerRegistry
import org.gradle.kotlin.dsl.container
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.domainObjectSet
import org.gradle.kotlin.dsl.findPlugin
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.invoke
import org.gradle.kotlin.dsl.mapProperty
import org.gradle.kotlin.dsl.named
import org.gradle.kotlin.dsl.newInstance
import org.gradle.kotlin.dsl.registerIfAbsent
import org.gradle.kotlin.dsl.setProperty
import org.gradle.kotlin.dsl.withType
import javax.inject.Inject
@Suppress("unused") // Used by Gradle
internal abstract class PlayPublisherPlugin @Inject constructor(
private val buildEventsListenerRegistry: BuildEventsListenerRegistry,
) : Plugin<Project> {
override fun apply(project: Project) {
project.validateRuntime()
project.extensions.create<PlayPublisherExtension>(PLAY_PATH, "default").apply {
enabled.convention(true)
defaultToAppBundles.convention(false)
commit.convention(true)
track.convention("internal")
resolutionStrategy.convention(ResolutionStrategy.FAIL)
}
project.plugins.withType<AppPlugin> {
applyInternal(project)
}
project.afterEvaluate {
if (project.plugins.findPlugin(AppPlugin::class) == null) {
throw IllegalStateException(
"The Android Gradle Plugin was not applied. Gradle Play Publisher " +
"cannot be configured.")
}
}
}
private fun applyInternal(project: Project) {
val executionDir = project.objects.directoryProperty()
.apply { set(project.rootDir) }
.get()
val cliOptionsExtension = project.objects.newInstance<CliPlayPublisherExtension>()
val bootstrapOptionsHolder = BootstrapOptions.Holder()
// ---------------------------------- START: GLOBAL TASKS ----------------------------------
val bootstrapAllTask = project.newTask<BootstrapLifecycleTask>(
"bootstrapListing",
"""
|Downloads the Play Store listing metadata for all variants.
| See https://github.com/Triple-T/gradle-play-publisher#quickstart
""".trimMargin(),
arrayOf(bootstrapOptionsHolder),
)
val publishAllTask = project.newTask<GlobalPublishableArtifactLifecycleTask>(
"publishApps",
"""
|Uploads APK or App Bundle and all Play Store metadata for all variants.
| See https://github.com/Triple-T/gradle-play-publisher#managing-artifacts
""".trimMargin(),
arrayOf(cliOptionsExtension, executionDir),
)
val publishApkAllTask = project.newTask<PublishableTrackLifecycleTask>(
"publishApk",
"""
|Uploads APK for all variants.
| See https://github.com/Triple-T/gradle-play-publisher#publishing-apks
""".trimMargin(),
arrayOf(cliOptionsExtension, executionDir),
)
val publishBundleAllTask = project.newTask<PublishableTrackLifecycleTask>(
"publishBundle",
"""
|Uploads App Bundle for all variants.
| See https://github.com/Triple-T/gradle-play-publisher#publishing-an-app-bundle
""".trimMargin(),
arrayOf(cliOptionsExtension, executionDir),
)
val publishInternalSharingApkAllTask = project.newTask<GlobalUploadableArtifactLifecycleTask>(
"uploadPrivateApk",
"""
|Uploads Internal Sharing APK for all variants.
| See https://github.com/Triple-T/gradle-play-publisher#uploading-an-internal-sharing-artifact
""".trimMargin(),
arrayOf(cliOptionsExtension, executionDir),
)
val publishInternalSharingBundleAllTask = project.newTask<GlobalUploadableArtifactLifecycleTask>(
"uploadPrivateBundle",
"""
|Uploads Internal Sharing App Bundle for all variants.
| See https://github.com/Triple-T/gradle-play-publisher#uploading-an-internal-sharing-artifact
""".trimMargin(),
arrayOf(cliOptionsExtension, executionDir),
)
val promoteReleaseAllTask = project.newTask<UpdatableTrackLifecycleTask>(
"promoteArtifact",
"""
|Promotes a release for all variants.
| See https://github.com/Triple-T/gradle-play-publisher#promoting-artifacts
""".trimMargin(),
arrayOf(cliOptionsExtension, executionDir),
)
val publishListingAllTask = project.newTask<WriteTrackLifecycleTask>(
"publishListing",
"""
|Uploads all Play Store metadata for all variants.
| See https://github.com/Triple-T/gradle-play-publisher#publishing-listings
""".trimMargin(),
arrayOf(cliOptionsExtension, executionDir),
)
val publishProductsAllTask = project.newTask<Task>(
"publishProducts",
"""
|Uploads all Play Store in-app products for all variants.
| See https://github.com/Triple-T/gradle-play-publisher#publishing-in-app-products
""".trimMargin(),
)
val publishSubscriptionsAllTask = project.newTask<Task>(
"publishSubscriptions",
"""
|Uploads all Play Store in-app subscriptions for all variants.
| See https://github.com/Triple-T/gradle-play-publisher#publishing-in-app-subscriptions
""".trimMargin(),
)
// ----------------------------------- END: GLOBAL TASKS -----------------------------------
val baseExtension = project.extensions.getByType<PlayPublisherExtension>()
val extensionContainer = project.container<PlayPublisherExtension>()
val android = project.extensions.getByType<BaseAppModuleExtension>()
(android as ExtensionAware).extensions.add(PLAY_CONFIGS_PATH, extensionContainer)
val androidExtension = project.extensions.getByType<ApplicationAndroidComponentsExtension>()
androidExtension.onVariants(androidExtension.selector().all()) v@{ variant ->
val taskVariantName = variant.name.capitalize()
val extensionStore = variant.buildExtension(
project,
extensionContainer,
baseExtension,
cliOptionsExtension,
)
val extension = extensionStore.getValue(variant.name)
project.logger.debug(
"Extension computed for variant $taskVariantName: ${extension.toConfig()}")
project.afterEvaluate {
project.logger.debug(
"Extension resolved for variant $taskVariantName: ${extension.toConfig()}")
}
if (!extension.enabled.get()) {
project.logger.info(
"Gradle Play Publisher is disabled for variant $taskVariantName.")
return@v
}
fun findApkFiles(): Provider<List<String>> = extension.artifactDir.map {
val customDir = it.asFile
if (customDir.isFile && customDir.extension == "apk") {
listOf(it.asFile.absolutePath)
} else {
it.asFileTree.matching {
include("*.apk")
}.map { it.absolutePath }
}
}.orElse(variant.artifacts.get(SingleArtifact.APK).map {
variant.artifacts.getBuiltArtifactsLoader().load(it)
?.elements?.map { it.outputFile }.sneakyNull()
})
fun findBundleFiles(): Provider<List<String>> = extension.artifactDir.map {
val customDir = it.asFile
if (customDir.isFile && customDir.extension == "aab") {
listOf(it.asFile.absolutePath)
} else {
it.asFileTree.matching {
include("*.aab")
}.map { it.absolutePath }
}
}.orElse(variant.artifacts.get(SingleArtifact.BUNDLE).map {
listOf(it.asFile.absolutePath)
})
@Suppress("UNCHECKED_CAST") // Needed for overload ambiguity
fun getArtifactDependenciesHack(
artifact: SingleArtifact<*>,
): Provider<*> = extension.artifactDir.map<String> {
""
}.orElse(variant.artifacts.get(artifact) as Provider<String>)
val appId = variant.applicationId.get()
val api = project.gradle.sharedServices.registerIfAbsent(
"playApi-$appId", PlayApiService::class) {
parameters.appId.set(appId)
parameters.editIdFile.set(project.layout.buildDirectory.file("$OUTPUT_PATH/$appId.txt"))
}
buildEventsListenerRegistry.onTaskCompletion(api)
project.gradle.sharedServices.registrations.named<
BuildServiceRegistration<PlayApiService, PlayApiService.Params>
>("playApi-$appId") {
val priorityProp = parameters._extensionPriority
val newPriority = extension.toPriority()
if (!priorityProp.isPresent || newPriority < priorityProp.get()) {
parameters.credentials.set(extension.serviceAccountCredentials)
priorityProp.set(newPriority)
}
}
fun PublishTaskBase.bindApi(api: Provider<PlayApiService>) {
usesService(api)
apiService.value(api).disallowChanges()
}
// ------------------- START: ALL BUILD TYPE VARIANT SPECIFIC TASKS -------------------
val publishInternalSharingApkTask = project.newTask<PublishInternalSharingApk>(
"upload${taskVariantName}PrivateApk",
"""
|Uploads Internal Sharing APK for variant $taskVariantName.
| See https://github.com/Triple-T/gradle-play-publisher#uploading-an-internal-sharing-artifact
""".trimMargin(),
arrayOf(extension, executionDir),
) {
bindApi(api)
apks.from(findApkFiles())
outputDirectory.set(project.layout.buildDirectory.dir(
"outputs/internal-sharing/apk/${variant.name}"))
dependsOn(getArtifactDependenciesHack(SingleArtifact.APK))
configure3pDeps(extension, taskVariantName)
}
publishInternalSharingApkAllTask { dependsOn(publishInternalSharingApkTask) }
val publishInternalSharingBundleTask = project.newTask<PublishInternalSharingBundle>(
"upload${taskVariantName}PrivateBundle",
"""
|Uploads Internal Sharing App Bundle for variant $taskVariantName.
| See https://github.com/Triple-T/gradle-play-publisher#uploading-an-internal-sharing-artifact
""".trimMargin(),
arrayOf(extension, executionDir),
) {
bindApi(api)
bundles.from(findBundleFiles())
outputDirectory.set(project.layout.buildDirectory.dir(
"outputs/internal-sharing/bundle/${variant.name}"))
dependsOn(getArtifactDependenciesHack(SingleArtifact.BUNDLE))
configure3pDeps(extension, taskVariantName)
}
publishInternalSharingBundleAllTask { dependsOn(publishInternalSharingBundleTask) }
project.newTask<InstallInternalSharingArtifact>(
"install${taskVariantName}PrivateArtifact",
"""
|Launches an intent to install an Internal Sharing artifact for variant $taskVariantName.
| See https://github.com/Triple-T/gradle-play-publisher#installing-internal-sharing-artifacts
""".trimMargin(),
arrayOf(android),
) {
uploadedArtifacts.set(if (extension.defaultToAppBundles.get()) {
publishInternalSharingBundleTask.flatMap { it.outputDirectory }
} else {
publishInternalSharingApkTask.flatMap { it.outputDirectory }
})
}
// -------------------- END: ALL BUILD TYPE VARIANT SPECIFIC TASKS --------------------
if (!variant.validateDebuggability()) return@v
val commitEditTask = project.getCommitEditTask(appId, extension, api)
// -------------------- START: RELEASE ONLY VARIANT SPECIFIC TASKS --------------------
val bootstrapTask = project.newTask<Bootstrap>(
"bootstrap${taskVariantName}Listing",
"""
|Downloads the Play Store listing metadata for variant $taskVariantName.
| See https://github.com/Triple-T/gradle-play-publisher#quickstart
""".trimMargin(),
arrayOf(extension, bootstrapOptionsHolder),
) {
bindApi(api)
srcDir.set(project.file("src/${variant.flavorNameOrDefault}/$PLAY_PATH"))
}
bootstrapAllTask { dependsOn(bootstrapTask) }
val resourceDir = project.newTask<GenerateResources>(
"generate${taskVariantName}PlayResources"
) {
resDir.set(project.layout.buildDirectory.dir(variant.playPath))
mustRunAfter(bootstrapTask)
}.also { task ->
// TODO(asaveau): remove once there's an API for sourceSets in the new model
android.applicationVariants
.matching { it.name == variant.name }
.whenObjectAdded {
val dirs = sourceSets.map {
project.layout.projectDirectory.dir("src/${it.name}/$PLAY_PATH")
}
task {
resSrcDirs.set(dirs)
resSrcTree.setFrom(dirs.map {
project.fileTree(it).apply { exclude("**/.*") }
})
}
}
}.flatMap {
it.resDir
}
val publishListingTask = project.newTask<PublishListings>(
"publish${taskVariantName}Listing",
"""
|Uploads all Play Store metadata for variant $taskVariantName.
| See https://github.com/Triple-T/gradle-play-publisher#publishing-listings
""".trimMargin(),
arrayOf(extension, executionDir),
) {
bindApi(api)
resDir.set(resourceDir)
finalizedBy(commitEditTask)
}
publishListingAllTask { dependsOn(publishListingTask) }
val publishProductsTask = project.newTask<PublishProducts>(
"publish${taskVariantName}Products",
"""
|Uploads all Play Store in-app products for variant $taskVariantName.
| See https://github.com/Triple-T/gradle-play-publisher#publishing-in-app-products
""".trimMargin(),
arrayOf(extension),
) {
bindApi(api)
productsDir.setFrom(resourceDir.map {
it.dir(PRODUCTS_PATH).asFileTree.matching { include("*.json") }
})
}
publishProductsAllTask { dependsOn(publishProductsTask) }
val publishSubscriptionsTask = project.newTask<PublishSubscriptions>(
"publish${taskVariantName}Subscriptions",
"""
|Uploads all Play Store in-app subscriptions for variant $taskVariantName.
| See https://github.com/Triple-T/gradle-play-publisher#publishing-in-app-subscriptions
""".trimMargin(),
arrayOf(extension),
) {
bindApi(api)
subscriptionsDir.setFrom(resourceDir.map {
it.dir(SUBSCRIPTIONS_PATH).asFileTree.matching { include("*.json") }
})
}
publishSubscriptionsAllTask { dependsOn(publishSubscriptionsTask) }
val isAuto = extension.resolutionStrategy.get() == ResolutionStrategy.AUTO
|| extension.resolutionStrategy.get() == ResolutionStrategy.AUTO_OFFSET
val staticVersionCodes = if (isAuto) {
variant.outputs.map { it.versionCode.get() }
} else {
emptyList()
}
val processArtifactVersionCodes = project.newTask<ProcessArtifactVersionCodes>(
"process${taskVariantName}VersionCodes",
constructorArgs = arrayOf(
extension,
extension.resolutionStrategy.get() == ResolutionStrategy.AUTO_OFFSET,
),
) {
bindApi(api)
versionCodes.set(staticVersionCodes)
playVersionCodes.set(project.layout.buildDirectory.file(
"$INTERMEDIATES_OUTPUT_PATH/${variant.name}/available-version-codes.txt"))
}
if (isAuto) {
for ((i, output) in variant.outputs.withIndex()) {
output.versionCode.set(processArtifactVersionCodes.map {
it.playVersionCodes.get().asFile.readLines()[i].toInt()
})
}
}
fun PublishArtifactTaskBase.configureInputs() {
bindApi(api)
releaseNotesDir.set(resourceDir.map {
it.dir(RELEASE_NOTES_PATH).also { it.asFile.safeMkdirs() }
})
consoleNamesDir.set(resourceDir.map {
it.dir(RELEASE_NAMES_PATH).also { it.asFile.safeMkdirs() }
})
}
val publishApkTask = project.newTask<PublishApk>(
"publish${taskVariantName}Apk",
"""
|Uploads APK for variant $taskVariantName.
| See https://github.com/Triple-T/gradle-play-publisher#publishing-apks
""".trimMargin(),
arrayOf(extension, executionDir),
) {
configureInputs()
apks.from(findApkFiles())
mappingFiles.from(extension.artifactDir.map { customDir ->
project.objects.fileCollection().from(customDir.asFileTree.matching {
include("mapping.txt", "*.mapping.txt")
})
}.orElse(project.provider {
variant.artifacts.get(SingleArtifact.OBFUSCATION_MAPPING_FILE).takeUnless {
extension.artifactDir.isPresent
}
}.map {
val files = project.objects.fileCollection()
if (it.isPresent) {
files.from(it)
}
files
}))
dependsOn(getArtifactDependenciesHack(SingleArtifact.APK))
finalizedBy(commitEditTask)
configure3pDeps(extension, taskVariantName)
nativeDebugSymbols.set(project.layout.buildDirectory.file(
"outputs/native-debug-symbols/${variant.name}/native-debug-symbols.zip"
).map {
it.takeIf { it.asFile.exists() }.sneakyNull()
})
dependsOn("merge${taskVariantName}NativeDebugMetadata")
}
publishApkAllTask { dependsOn(publishApkTask) }
val publishBundleTask = project.newTask<PublishBundle>(
"publish${taskVariantName}Bundle",
"""
|Uploads App Bundle for variant $taskVariantName.
| See https://github.com/Triple-T/gradle-play-publisher#publishing-an-app-bundle
""".trimMargin(),
arrayOf(extension, executionDir),
) {
configureInputs()
bundles.from(findBundleFiles())
dependsOn(getArtifactDependenciesHack(SingleArtifact.BUNDLE))
finalizedBy(commitEditTask)
configure3pDeps(extension, taskVariantName)
}
publishBundleAllTask { dependsOn(publishBundleTask) }
val promoteReleaseTask = project.newTask<PromoteRelease>(
"promote${taskVariantName}Artifact",
"""
|Promotes a release for variant $taskVariantName.
| See https://github.com/Triple-T/gradle-play-publisher#promoting-artifacts
""".trimMargin(),
arrayOf(extension, executionDir),
) {
configureInputs()
finalizedBy(commitEditTask)
mustRunAfter(publishApkTask)
mustRunAfter(publishBundleTask)
}
promoteReleaseAllTask { dependsOn(promoteReleaseTask) }
val publishTask = project.newTask<GlobalPublishableArtifactLifecycleTask>(
"publish${taskVariantName}Apps",
"""
|Uploads APK or App Bundle and all Play Store metadata for variant $taskVariantName.
| See https://github.com/Triple-T/gradle-play-publisher#managing-artifacts
""".trimMargin(),
arrayOf(extension, executionDir),
) {
dependsOn(if (extension.defaultToAppBundles.get()) {
publishBundleTask
} else {
publishApkTask
})
dependsOn(publishListingTask)
dependsOn(publishProductsTask)
dependsOn(publishSubscriptionsTask)
}
publishAllTask { dependsOn(publishTask) }
// --------------------- END: RELEASE ONLY VARIANT SPECIFIC TASKS ---------------------
val availableGlobals = setOfNotNull(
variant.flavorName,
variant.buildType,
*variant.productFlavors.map { (_, flavor) -> flavor }.toTypedArray()
).let {
it - variant.name
}
// ----------------------------- START: SEMI-GLOBAL TASKS -----------------------------
for (global in availableGlobals) {
val taskQualifier = global.capitalize()
// We want to disallow using the leaf extension past this point
@Suppress("NAME_SHADOWING")
val extension = extensionStore[global] ?: run e@{
val ordering = variant.generateExtensionOverrideOrdering()
var i = ordering.indexOf(global)
while (i < ordering.size) {
val nextExtension = extensionStore[ordering[++i]]
if (nextExtension != null) {
return@e nextExtension
}
}
error("Base extension not found.")
}
project.newTask<BootstrapLifecycleTask>(
"bootstrap${taskQualifier}Listing",
"""
|Downloads the Play Store listing metadata for all $taskQualifier variants.
| See https://github.com/Triple-T/gradle-play-publisher#quickstart
""".trimMargin(),
arrayOf(bootstrapOptionsHolder),
allowExisting = true,
).configure { dependsOn(bootstrapTask) }
project.newTask<GlobalPublishableArtifactLifecycleTask>(
"publish${taskQualifier}Apps",
"""
|Uploads APK or App Bundle and all Play Store metadata for all $taskQualifier variants.
| See https://github.com/Triple-T/gradle-play-publisher#managing-artifacts
""".trimMargin(),
arrayOf(extension, executionDir),
allowExisting = true,
).configure { dependsOn(publishTask) }
project.newTask<PublishableTrackLifecycleTask>(
"publish${taskQualifier}Apk",
"""
|Uploads APK for all $taskQualifier variants.
| See https://github.com/Triple-T/gradle-play-publisher#publishing-apks
""".trimMargin(),
arrayOf(extension, executionDir),
allowExisting = true,
).configure { dependsOn(publishApkTask) }
project.newTask<PublishableTrackLifecycleTask>(
"publish${taskQualifier}Bundle",
"""
|Uploads App Bundle for all $taskQualifier variants.
| See https://github.com/Triple-T/gradle-play-publisher#publishing-an-app-bundle
""".trimMargin(),
arrayOf(extension, executionDir),
allowExisting = true,
).configure { dependsOn(publishBundleTask) }
project.newTask<GlobalUploadableArtifactLifecycleTask>(
"upload${taskQualifier}PrivateApk",
"""
|Uploads Internal Sharing APK for all $taskQualifier variants.
| See https://github.com/Triple-T/gradle-play-publisher#uploading-an-internal-sharing-artifact
""".trimMargin(),
arrayOf(extension, executionDir),
allowExisting = true,
).configure { dependsOn(publishInternalSharingApkTask) }
project.newTask<GlobalUploadableArtifactLifecycleTask>(
"upload${taskQualifier}PrivateBundle",
"""
|Uploads Internal Sharing App Bundle for all $taskQualifier variants.
| See https://github.com/Triple-T/gradle-play-publisher#uploading-an-internal-sharing-artifact
""".trimMargin(),
arrayOf(extension, executionDir),
allowExisting = true,
).configure { dependsOn(publishInternalSharingBundleTask) }
project.newTask<UpdatableTrackLifecycleTask>(
"promote${taskQualifier}Artifact",
"""
|Promotes a release for all $taskQualifier variants.
| See https://github.com/Triple-T/gradle-play-publisher#promoting-artifacts
""".trimMargin(),
arrayOf(extension, executionDir),
allowExisting = true,
).configure { dependsOn(promoteReleaseTask) }
project.newTask<WriteTrackLifecycleTask>(
"publish${taskQualifier}Listing",
"""
|Uploads all Play Store metadata for all $taskQualifier variants.
| See https://github.com/Triple-T/gradle-play-publisher#publishing-listings
""".trimMargin(),
arrayOf(extension, executionDir),
allowExisting = true,
).configure { dependsOn(publishListingTask) }
project.newTask<Task>(
"publish${taskQualifier}Products",
"""
|Uploads all Play Store in-app products for all $taskQualifier variants.
| See https://github.com/Triple-T/gradle-play-publisher#publishing-in-app-products
""".trimMargin(),
allowExisting = true,
).configure { dependsOn(publishProductsTask) }
project.newTask<Task>(
"publish${taskQualifier}Subscriptions",
"""
|Uploads all Play Store in-app subscriptions for all $taskQualifier variants.
| See https://github.com/Triple-T/gradle-play-publisher#publishing-in-app-subscriptions
""".trimMargin(),
allowExisting = true,
).configure { dependsOn(publishSubscriptionsTask) }
}
// ----------------------------- END: SEMI-GLOBAL TASKS -----------------------------
}
project.afterEvaluate {
val allPossiblePlayConfigNames: Set<String> by lazy {
android.applicationVariants.flatMapTo(mutableSetOf()) { variant ->
listOf(variant.name, variant.buildType.name) +
variant.productFlavors.flatMap { listOfNotNull(it.name, it.dimension) }
}
}
for (configName in extensionContainer.names) {
if (configName !in allPossiblePlayConfigNames) {
project.logger.warn(
"Warning: Gradle Play Publisher playConfigs object '$configName' " +
"does not match a variant, flavor, dimension, or build type.")
}
}
}
}
private fun Task.configure3pDeps(extension: PlayPublisherExtension, variantName: String) {
fun maybeAddDependency(task: String) {
if (task in project.tasks.names) {
dependsOn(extension.artifactDir.map {
listOf<String>()
}.orElse(listOf(task)))
}
}
maybeAddDependency("uploadCrashlyticsMappingFile$variantName")
val bugsnagName = buildString {
var seenUpper = false
for (c in variantName) {
if (c.isUpperCase() && seenUpper) {
append('-')
append(c.toLowerCase())
} else {
append(c)
}
seenUpper = c.isUpperCase() || seenUpper
}
}
maybeAddDependency("uploadBugsnag${bugsnagName}Mapping")
maybeAddDependency("uploadSentryProguardMappings$variantName")
}
// TODO(asaveau): remove after https://github.com/gradle/gradle/issues/12388
@Suppress("UNCHECKED_CAST")
private fun <T> T?.sneakyNull() = this as T
}