@@ -27,11 +27,15 @@ 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.attributes.Usage
38+ import org.gradle.api.file.Directory
3539import org.gradle.api.publish.maven.internal.publication.DefaultMavenPublication
3640import org.gradle.api.publish.maven.tasks.PublishToMavenRepository
3741import org.gradle.kotlin.dsl.support.unzipTo
@@ -68,20 +72,25 @@ open class GreasePlugin : Plugin<Project> {
6872 createConfigurations(false )
6973 createConfigurations(true )
7074
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)
75+ fun configure (variant : Variant , runtime : List <Configuration >, api : List <Configuration >) {
76+ configureVariantManifest(target, variant, runtime, log)
77+ configureVariantJniLibs(target, variant, runtime, log)
78+ configureVariantAidlParcelables(target, variant, runtime + api, log)
79+ configureVariantResources(target, variant, runtime, log)
80+ configureVariantSources(target, variant, runtime, greaseExtension, log)
81+ configureVariantAssets(target, variant, runtime, log)
82+ configureVariantProguardFiles(target, variant, runtime, log)
7883 }
7984 // Configure all variants.
8085 androidComponents.onVariants { variant ->
8186 val childLog = log.child(" configureVariant" )
8287 childLog.d { " Configuring variant ${variant.name} ..." }
8388 target.afterEvaluate {
84- configure(variant, target.greaseOf(variant), target.greaseOf(variant, true ))
89+ configure(
90+ variant = variant,
91+ runtime = listOf (target.greaseOf(variant), target.greaseOf(variant, true )),
92+ api = listOf (target.greaseApiOf(variant), target.greaseApiOf(variant, true ))
93+ )
8594 }
8695 }
8796 }
@@ -110,7 +119,7 @@ open class GreasePlugin : Plugin<Project> {
110119 private fun configureVariantManifest (
111120 target : Project ,
112121 variant : Variant ,
113- configurations : Array < out Configuration >,
122+ configurations : List < Configuration >,
114123 logger : Logger
115124 ) {
116125 val log = logger.child(" configureVariantManifest" )
@@ -121,7 +130,7 @@ open class GreasePlugin : Plugin<Project> {
121130 target.locateTask(componentConfig.resolveTaskName(" process" , " Manifest" ))?.configure {
122131 val processManifestTask = this as ProcessLibraryManifest
123132
124- val extraManifests = configurations.artifactsOf(AndroidArtifacts .ArtifactType .MANIFEST )
133+ val extraManifests = configurations.artifactsOf(target, AndroidArtifacts .ArtifactType .MANIFEST )
125134 dependsOn(extraManifests)
126135
127136 // After the file is copied we can go on with the actual manifest merging.
@@ -210,7 +219,7 @@ open class GreasePlugin : Plugin<Project> {
210219 private fun configureVariantJniLibs (
211220 target : Project ,
212221 variant : Variant ,
213- configurations : Array < out Configuration >,
222+ configurations : List < Configuration >,
214223 logger : Logger
215224 ) {
216225 val log = logger.child(" configureVariantJniLibs" )
@@ -220,7 +229,7 @@ open class GreasePlugin : Plugin<Project> {
220229
221230 target.locateTask(creationConfig.resolveTaskName(" copy" , " JniLibsProjectAndLocalJars" ))?.configure {
222231 val copyJniTask = this as LibraryJniLibsTask
223- val extraJniLibs = configurations.artifactsOf(AndroidArtifacts .ArtifactType .JNI )
232+ val extraJniLibs = configurations.artifactsOf(target, AndroidArtifacts .ArtifactType .JNI )
224233 dependsOn(extraJniLibs)
225234
226235 fun injectJniLibs () {
@@ -248,6 +257,37 @@ open class GreasePlugin : Plugin<Project> {
248257 }
249258 }
250259
260+ private fun configureVariantAidlParcelables (
261+ target : Project ,
262+ variant : Variant ,
263+ configurations : List <Configuration >,
264+ logger : Logger
265+ ) {
266+ val log = logger.child(" configureVariantAidlParcelables" )
267+ log.d { " Configuring variant ${variant.name} ..." }
268+ val creationConfig = variant.componentCreationConfigOrThrow()
269+ creationConfig.taskContainer.aidlCompileTask?.configure {
270+ val extraAidlFiles = configurations.artifactsOf(target, AndroidArtifacts .ArtifactType .AIDL )
271+ dependsOn(extraAidlFiles)
272+ fun injectAidlFiles () {
273+ log.d { " Executing for variant ${variant.name} and ${extraAidlFiles.files.size} roots..." }
274+ extraAidlFiles.files.forEach { inputRoot ->
275+ log.d { " Found aidl parcelables files root: $inputRoot " }
276+ val inputFiles = target.fileTree(inputRoot)
277+ target.copy {
278+ from(inputFiles)
279+ into(packagedDir.get())
280+ }
281+ }
282+ }
283+ if (sourceFiles.get().files.isNotEmpty()) {
284+ doLast { injectAidlFiles() }
285+ } else {
286+ injectAidlFiles()
287+ }
288+ }
289+ }
290+
251291 /* *
252292 * AARs ship with a file called R.txt which already includes all resource ids from dependencies,
253293 * so we shouldn't probably do nothing about it as it comes for free.
@@ -307,7 +347,7 @@ open class GreasePlugin : Plugin<Project> {
307347 private fun configureVariantResources (
308348 target : Project ,
309349 variant : Variant ,
310- configurations : Array < out Configuration >,
350+ configurations : List < Configuration >,
311351 logger : Logger
312352 ) {
313353
@@ -321,7 +361,7 @@ open class GreasePlugin : Plugin<Project> {
321361 val resourcesMergingWorkdir = target.greaseBuildDir.get().dir(variant.name).dir(" resources" )
322362 val mergedResourcesDir = resourcesMergingWorkdir.dir(" merged" )
323363 val blameDir = resourcesMergingWorkdir.dir(" blame" )
324- val extraAndroidRes = configurations.artifactsOf(AndroidArtifacts .ArtifactType .ANDROID_RES )
364+ val extraAndroidRes = configurations.artifactsOf(target, AndroidArtifacts .ArtifactType .ANDROID_RES )
325365 dependsOn(extraAndroidRes)
326366
327367 outputs.upToDateWhen { false } // always execute
@@ -376,7 +416,7 @@ open class GreasePlugin : Plugin<Project> {
376416 private fun configureVariantSources (
377417 target : Project ,
378418 variant : Variant ,
379- configurations : Array < out Configuration >,
419+ configurations : List < Configuration >,
380420 greaseExtension : GreaseExtension ,
381421 logger : Logger
382422 ) {
@@ -386,6 +426,7 @@ open class GreasePlugin : Plugin<Project> {
386426 val creationConfig = variant.componentCreationConfigOrThrow()
387427
388428 val workdir = target.greaseBuildDir.get().dir(variant.name)
429+ workdir.asFile.deleteRecursively()
389430 val aarExtractWorkdir = workdir.dir(" extract" ).dir(" aar" )
390431 val jarExtractWorkdir = workdir.dir(" extract" ).dir(" jar" )
391432 val jarFileName = " classes.jar"
@@ -414,7 +455,7 @@ open class GreasePlugin : Plugin<Project> {
414455
415456 // There are many options here. PROCESSED_JAR, PROCESSED_AAR, CLASSES, CLASSES_JAR ...
416457 // CLASSES_JAR seems to be the best though it's not clear if it's jetified or not.
417- val extraJars = configurations.artifactsOf(AndroidArtifacts .ArtifactType .CLASSES_JAR )
458+ val extraJars = configurations.artifactsOf(target, AndroidArtifacts .ArtifactType .CLASSES_JAR )
418459 dependsOn(extraJars)
419460 dependsOn(greaseExpandTask)
420461
@@ -444,7 +485,7 @@ open class GreasePlugin : Plugin<Project> {
444485 ShadowJar ::class .java
445486 ) {
446487 val compileTask = creationConfig.taskContainer.javacTask
447- val extraManifests = configurations.artifactsOf(AndroidArtifacts .ArtifactType .MANIFEST )
488+ val extraManifests = configurations.artifactsOf(target, AndroidArtifacts .ArtifactType .MANIFEST )
448489 val greaseShadowDir = workdir.dir(" shadow" )
449490 val bundleAar = bundleLibraryTask?.get() as BundleAar
450491
@@ -475,8 +516,8 @@ open class GreasePlugin : Plugin<Project> {
475516
476517 val relocationPrefix = greaseExtension.prefix.get()
477518 if (relocationPrefix.isNotEmpty()) {
478- greaseProcessTask.get().outputs.files
479- .asSequence()
519+ val sequence = greaseProcessTask.get().outputs.files.asSequence() + aarExtractWorkdir.dir( " aidl " ).asFile
520+ sequence
480521 .flatMap { inputFile -> inputFile.packageNames }
481522 .distinct()
482523 .map { packageName -> packageName to " ${relocationPrefix} .$packageName " }
@@ -502,13 +543,20 @@ open class GreasePlugin : Plugin<Project> {
502543 into(aarExtractWorkdir)
503544 }
504545
505- replacePackagesInFile (
546+ replacePackagesInManifest (
506547 aarExtractWorkdir.file(" AndroidManifest.xml" ).asFile,
507548 greaseShadowDir.file(" AndroidManifest.xml" ).asFile,
508549 relocators,
509550 target,
510551 )
511552
553+ relocateAidlFiles(
554+ aarExtractWorkdir.dir(" aidl" ),
555+ greaseShadowDir.dir(" aidl" ),
556+ relocators,
557+ target,
558+ )
559+
512560 val oldArchive = bundleAar.archiveFile.get().asFile
513561 val archiveParent = oldArchive.parentFile
514562 val archiveName = oldArchive.name
@@ -544,7 +592,7 @@ open class GreasePlugin : Plugin<Project> {
544592 }
545593 }
546594
547- private fun replacePackagesInFile (
595+ private fun replacePackagesInManifest (
548596 input : File ,
549597 output : File ,
550598 relocators : List <Relocator >,
@@ -573,6 +621,48 @@ open class GreasePlugin : Plugin<Project> {
573621 }
574622 }
575623
624+
625+ private fun relocateAidlFiles (
626+ inputDir : Directory ,
627+ outputDir : Directory ,
628+ relocators : List <Relocator >,
629+ target : Project ,
630+ ) {
631+ if (inputDir.asFileTree.isEmpty) return
632+
633+ inputDir.asFileTree.forEach { file ->
634+ val relocatePathContext = RelocatePathContext ().apply {
635+ stats = ShadowStats ()
636+ }
637+ val reader = file.bufferedReader()
638+ val relocatedPath = relocators
639+ .filterNot { it is RClassRelocator }
640+ .fold(file.toRelativeString(inputDir.asFile)) { acc, relocator ->
641+ relocator.relocatePath(relocatePathContext.apply { path = acc })
642+ }
643+ val writer = outputDir.asFile.file(relocatedPath).bufferedWriter()
644+ reader.useLines { strings ->
645+ strings
646+ .map { string ->
647+ relocators
648+ .filterNot { it is RClassRelocator }
649+ .fold(string) { acc, relocator ->
650+ relocator.applyToSourceContent(acc)
651+ }
652+ }.forEach {
653+ writer.write(it)
654+ writer.newLine()
655+ }
656+ }
657+ writer.close()
658+ }
659+ inputDir.asFile.deleteRecursively()
660+ target.copy {
661+ from(outputDir)
662+ into(inputDir)
663+ }
664+ }
665+
576666 /* *
577667 * Interesting tasks:
578668 * 1. generate<>Assets: See [MutableTaskContainer].
@@ -585,14 +675,14 @@ open class GreasePlugin : Plugin<Project> {
585675 private fun configureVariantAssets (
586676 target : Project ,
587677 variant : Variant ,
588- configurations : Array < out Configuration >,
678+ configurations : List < Configuration >,
589679 logger : Logger
590680 ) {
591681 val log = logger.child(" configureVariantAssets" )
592682 log.d { " Configuring variant ${variant.name} ..." }
593683 val creationConfig = variant.componentCreationConfigOrThrow()
594684 creationConfig.taskContainer.mergeAssetsTask.configure {
595- val extraAssets = configurations.artifactsOf(AndroidArtifacts .ArtifactType .ASSETS )
685+ val extraAssets = configurations.artifactsOf(target, AndroidArtifacts .ArtifactType .ASSETS )
596686 dependsOn(extraAssets)
597687 fun injectAssets () {
598688 log.d { " Executing for variant ${variant.name} and ${extraAssets.files.size} roots..." }
@@ -639,7 +729,7 @@ open class GreasePlugin : Plugin<Project> {
639729 private fun configureVariantProguardFiles (
640730 target : Project ,
641731 variant : Variant ,
642- configurations : Array < out Configuration >,
732+ configurations : List < Configuration >,
643733 logger : Logger
644734 ) {
645735 val log = logger.child(" configureVariantProguardFiles" )
@@ -650,7 +740,7 @@ open class GreasePlugin : Plugin<Project> {
650740 // UNFILTERED_PROGUARD_RULES, FILTERED_PROGUARD_RULES, AAPT_PROGUARD_RULES, ...
651741 // UNFILTERED_PROGUARD_RULES is output of the AarTransform. FILTERED_PROGUARD_RULES
652742 // is processed by another transform and is probably what we want in the end.
653- val extraInputs = configurations.artifactsOf(AndroidArtifacts .ArtifactType .FILTERED_PROGUARD_RULES )
743+ val extraInputs = configurations.artifactsOf(target, AndroidArtifacts .ArtifactType .FILTERED_PROGUARD_RULES )
654744 dependsOn(extraInputs)
655745
656746 mergeFileTask.inputs.files(extraInputs + mergeFileTask.inputFiles.files)
0 commit comments