-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
include kotlin_module files to final fat aar
- Loading branch information
Showing
5 changed files
with
169 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
grease/src/main/kotlin/io/deepmedia/tools/grease/KotlinModuleShadowTransformer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.deepmedia.tools.grease | ||
|
||
import com.github.jengelman.gradle.plugins.shadow.transformers.CacheableTransformer | ||
import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer | ||
import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext | ||
import org.gradle.api.file.FileTreeElement | ||
import kotlinx.metadata.jvm.KotlinModuleMetadata | ||
import kotlinx.metadata.jvm.UnstableMetadataApi | ||
import org.apache.tools.zip.ZipEntry | ||
import org.apache.tools.zip.ZipOutputStream | ||
|
||
// from kotlin sources | ||
|
||
@CacheableTransformer | ||
@OptIn(UnstableMetadataApi::class) | ||
internal class KotlinModuleShadowTransformer(private val logger: Logger) : Transformer { | ||
@Suppress("ArrayInDataClass") | ||
private data class Entry(val path: String, val bytes: ByteArray) | ||
|
||
private val data = mutableListOf<Entry>() | ||
|
||
override fun getName() = "KotlinModuleShadowTransformer" | ||
|
||
override fun canTransformResource(element: FileTreeElement): Boolean = | ||
element.path.substringAfterLast(".") == KOTLIN_MODULE | ||
|
||
override fun transform(context: TransformerContext) { | ||
fun relocate(content: String): String = | ||
context.relocators | ||
.filterNot { it is RClassRelocator } | ||
.fold(content) { acc, relocator -> relocator.applyToSourceContent(acc) } | ||
|
||
logger.i { "Transforming kotlin_module ${context.path}" } | ||
val metadata = KotlinModuleMetadata.read(context.`is`.readBytes()) | ||
val module = metadata.kmModule | ||
|
||
val packageParts = module.packageParts.toMap() | ||
module.packageParts.clear() | ||
packageParts.map { (fqName, parts) -> | ||
require(parts.multiFileClassParts.isEmpty()) { parts.multiFileClassParts } // There are no multi-file class parts in core | ||
|
||
val fileFacades = parts.fileFacades.toList() | ||
parts.fileFacades.clear() | ||
fileFacades.mapTo(parts.fileFacades) { relocate(it) } | ||
|
||
relocate(fqName) to parts | ||
}.toMap(module.packageParts) | ||
|
||
data += Entry(context.path, metadata.write()) | ||
} | ||
|
||
override fun hasTransformedResource(): Boolean = data.isNotEmpty() | ||
|
||
override fun modifyOutputStream(os: ZipOutputStream, preserveFileTimestamps: Boolean) { | ||
for ((path, bytes) in data) { | ||
os.putNextEntry(ZipEntry(path)) | ||
os.write(bytes) | ||
} | ||
data.clear() | ||
} | ||
|
||
companion object { | ||
const val KOTLIN_MODULE = "kotlin_module" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters