@@ -27,11 +27,14 @@ import com.android.ide.common.symbols.parseManifest
2727import com.android.manifmerger.ManifestMerger2
2828import com.android.manifmerger.ManifestProvider
2929import com.android.utils.appendCapitalized
30+ import com.github.jengelman.gradle.plugins.shadow.ShadowStats
31+ import com.github.jengelman.gradle.plugins.shadow.relocation.RelocatePathContext
3032import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator
3133import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
3234import org.gradle.api.Plugin
3335import org.gradle.api.Project
3436import org.gradle.api.artifacts.Configuration
37+ import org.gradle.api.file.Directory
3538import org.gradle.api.publish.maven.internal.publication.DefaultMavenPublication
3639import org.gradle.api.publish.maven.tasks.PublishToMavenRepository
3740import org.gradle.kotlin.dsl.support.unzipTo
@@ -68,20 +71,25 @@ open class GreasePlugin : Plugin<Project> {
6871 createConfigurations(false )
6972 createConfigurations(true )
7073
71- fun configure (variant : Variant , vararg configurations : Configuration ) {
72- configureVariantManifest(target, variant, configurations, log)
73- configureVariantJniLibs(target, variant, configurations, log)
74- configureVariantResources(target, variant, configurations, log)
75- configureVariantSources(target, variant, configurations, greaseExtension, log)
76- configureVariantAssets(target, variant, configurations, log)
77- configureVariantProguardFiles(target, variant, configurations, log)
74+ fun configure (variant : Variant , runtime : List <Configuration >, api : List <Configuration >) {
75+ configureVariantManifest(target, variant, runtime, log)
76+ configureVariantJniLibs(target, variant, runtime, log)
77+ configureVariantAidlParcelables(target, variant, runtime + api, log)
78+ configureVariantResources(target, variant, runtime, log)
79+ configureVariantSources(target, variant, runtime, greaseExtension, log)
80+ configureVariantAssets(target, variant, runtime, log)
81+ configureVariantProguardFiles(target, variant, runtime, log)
7882 }
7983 // Configure all variants.
8084 androidComponents.onVariants { variant ->
8185 val childLog = log.child(" configureVariant" )
8286 childLog.d { " Configuring variant ${variant.name} ..." }
8387 target.afterEvaluate {
84- configure(variant, target.greaseOf(variant), target.greaseOf(variant, true ))
88+ configure(
89+ variant = variant,
90+ runtime = listOf (target.greaseOf(variant), target.greaseOf(variant, true )),
91+ api = listOf (target.greaseApiOf(variant), target.greaseApiOf(variant, true ))
92+ )
8593 }
8694 }
8795 }
@@ -110,7 +118,7 @@ open class GreasePlugin : Plugin<Project> {
110118 private fun configureVariantManifest (
111119 target : Project ,
112120 variant : Variant ,
113- configurations : Array < out Configuration >,
121+ configurations : List < Configuration >,
114122 logger : Logger
115123 ) {
116124 val log = logger.child(" configureVariantManifest" )
@@ -210,7 +218,7 @@ open class GreasePlugin : Plugin<Project> {
210218 private fun configureVariantJniLibs (
211219 target : Project ,
212220 variant : Variant ,
213- configurations : Array < out Configuration >,
221+ configurations : List < Configuration >,
214222 logger : Logger
215223 ) {
216224 val log = logger.child(" configureVariantJniLibs" )
@@ -248,6 +256,37 @@ open class GreasePlugin : Plugin<Project> {
248256 }
249257 }
250258
259+ private fun configureVariantAidlParcelables (
260+ target : Project ,
261+ variant : Variant ,
262+ configurations : List <Configuration >,
263+ logger : Logger
264+ ) {
265+ val log = logger.child(" configureVariantAidlParcelables" )
266+ log.d { " Configuring variant ${variant.name} ..." }
267+ val creationConfig = variant.componentCreationConfigOrThrow()
268+ creationConfig.taskContainer.aidlCompileTask?.configure {
269+ val extraAidlFiles = configurations.artifactsOf(AndroidArtifacts .ArtifactType .AIDL )
270+ dependsOn(extraAidlFiles)
271+ fun injectAidlFiles () {
272+ log.d { " Executing for variant ${variant.name} and ${extraAidlFiles.files.size} roots..." }
273+ extraAidlFiles.files.forEach { inputRoot ->
274+ log.d { " Found aidl parcelables files root: $inputRoot " }
275+ val inputFiles = target.fileTree(inputRoot)
276+ target.copy {
277+ from(inputFiles)
278+ into(packagedDir.get())
279+ }
280+ }
281+ }
282+ if (sourceFiles.get().files.isNotEmpty()) {
283+ doLast { injectAidlFiles() }
284+ } else {
285+ injectAidlFiles()
286+ }
287+ }
288+ }
289+
251290 /* *
252291 * AARs ship with a file called R.txt which already includes all resource ids from dependencies,
253292 * so we shouldn't probably do nothing about it as it comes for free.
@@ -307,7 +346,7 @@ open class GreasePlugin : Plugin<Project> {
307346 private fun configureVariantResources (
308347 target : Project ,
309348 variant : Variant ,
310- configurations : Array < out Configuration >,
349+ configurations : List < Configuration >,
311350 logger : Logger
312351 ) {
313352
@@ -376,7 +415,7 @@ open class GreasePlugin : Plugin<Project> {
376415 private fun configureVariantSources (
377416 target : Project ,
378417 variant : Variant ,
379- configurations : Array < out Configuration >,
418+ configurations : List < Configuration >,
380419 greaseExtension : GreaseExtension ,
381420 logger : Logger
382421 ) {
@@ -386,6 +425,7 @@ open class GreasePlugin : Plugin<Project> {
386425 val creationConfig = variant.componentCreationConfigOrThrow()
387426
388427 val workdir = target.greaseBuildDir.get().dir(variant.name)
428+ workdir.asFile.deleteRecursively()
389429 val aarExtractWorkdir = workdir.dir(" extract" ).dir(" aar" )
390430 val jarExtractWorkdir = workdir.dir(" extract" ).dir(" jar" )
391431 val jarFileName = " classes.jar"
@@ -475,8 +515,8 @@ open class GreasePlugin : Plugin<Project> {
475515
476516 val relocationPrefix = greaseExtension.prefix.get()
477517 if (relocationPrefix.isNotEmpty()) {
478- greaseProcessTask.get().outputs.files
479- .asSequence()
518+ val sequence = greaseProcessTask.get().outputs.files.asSequence() + aarExtractWorkdir.dir( " aidl " ).asFile
519+ sequence
480520 .flatMap { inputFile -> inputFile.packageNames }
481521 .distinct()
482522 .map { packageName -> packageName to " ${relocationPrefix} .$packageName " }
@@ -502,13 +542,20 @@ open class GreasePlugin : Plugin<Project> {
502542 into(aarExtractWorkdir)
503543 }
504544
505- replacePackagesInFile (
545+ replacePackagesInManifest (
506546 aarExtractWorkdir.file(" AndroidManifest.xml" ).asFile,
507547 greaseShadowDir.file(" AndroidManifest.xml" ).asFile,
508548 relocators,
509549 target,
510550 )
511551
552+ relocateAidlFiles(
553+ aarExtractWorkdir.dir(" aidl" ),
554+ greaseShadowDir.dir(" aidl" ),
555+ relocators,
556+ target,
557+ )
558+
512559 val oldArchive = bundleAar.archiveFile.get().asFile
513560 val archiveParent = oldArchive.parentFile
514561 val archiveName = oldArchive.name
@@ -544,7 +591,7 @@ open class GreasePlugin : Plugin<Project> {
544591 }
545592 }
546593
547- private fun replacePackagesInFile (
594+ private fun replacePackagesInManifest (
548595 input : File ,
549596 output : File ,
550597 relocators : List <Relocator >,
@@ -573,6 +620,48 @@ open class GreasePlugin : Plugin<Project> {
573620 }
574621 }
575622
623+
624+ private fun relocateAidlFiles (
625+ inputDir : Directory ,
626+ outputDir : Directory ,
627+ relocators : List <Relocator >,
628+ target : Project ,
629+ ) {
630+ if (inputDir.asFileTree.isEmpty) return
631+
632+ inputDir.asFileTree.forEach { file ->
633+ val relocatePathContext = RelocatePathContext ().apply {
634+ stats = ShadowStats ()
635+ }
636+ val reader = file.bufferedReader()
637+ val relocatedPath = relocators
638+ .filterNot { it is RClassRelocator }
639+ .fold(file.toRelativeString(inputDir.asFile)) { acc, relocator ->
640+ relocator.relocatePath(relocatePathContext.apply { path = acc })
641+ }
642+ val writer = outputDir.asFile.file(relocatedPath).bufferedWriter()
643+ reader.useLines { strings ->
644+ strings
645+ .map { string ->
646+ relocators
647+ .filterNot { it is RClassRelocator }
648+ .fold(string) { acc, relocator ->
649+ relocator.applyToSourceContent(acc)
650+ }
651+ }.forEach {
652+ writer.write(it)
653+ writer.newLine()
654+ }
655+ }
656+ writer.close()
657+ }
658+ inputDir.asFile.deleteRecursively()
659+ target.copy {
660+ from(outputDir)
661+ into(inputDir)
662+ }
663+ }
664+
576665 /* *
577666 * Interesting tasks:
578667 * 1. generate<>Assets: See [MutableTaskContainer].
@@ -585,7 +674,7 @@ open class GreasePlugin : Plugin<Project> {
585674 private fun configureVariantAssets (
586675 target : Project ,
587676 variant : Variant ,
588- configurations : Array < out Configuration >,
677+ configurations : List < Configuration >,
589678 logger : Logger
590679 ) {
591680 val log = logger.child(" configureVariantAssets" )
@@ -639,7 +728,7 @@ open class GreasePlugin : Plugin<Project> {
639728 private fun configureVariantProguardFiles (
640729 target : Project ,
641730 variant : Variant ,
642- configurations : Array < out Configuration >,
731+ configurations : List < Configuration >,
643732 logger : Logger
644733 ) {
645734 val log = logger.child(" configureVariantProguardFiles" )
0 commit comments