From 12905c5dcb0f701c60a4c130f44d373a9bdac2cb Mon Sep 17 00:00:00 2001 From: johnsonlee Date: Sun, 12 May 2019 02:03:46 +0800 Subject: [PATCH] Refactor for redundant resources reducing --- .../CompressionVariantProcessor.kt | 47 ++++++++++--------- ...ndancy.kt => RemoveRedundantFlatImages.kt} | 30 +++--------- .../task/compression/RemoveRedundantImages.kt | 25 ++++++++++ 3 files changed, 56 insertions(+), 46 deletions(-) rename booster-task-compression/src/main/kotlin/com/didiglobal/booster/task/compression/{ReduceRedundancy.kt => RemoveRedundantFlatImages.kt} (52%) create mode 100644 booster-task-compression/src/main/kotlin/com/didiglobal/booster/task/compression/RemoveRedundantImages.kt diff --git a/booster-task-compression/src/main/kotlin/com/didiglobal/booster/task/compression/CompressionVariantProcessor.kt b/booster-task-compression/src/main/kotlin/com/didiglobal/booster/task/compression/CompressionVariantProcessor.kt index c93576be9..b6acf2630 100644 --- a/booster-task-compression/src/main/kotlin/com/didiglobal/booster/task/compression/CompressionVariantProcessor.kt +++ b/booster-task-compression/src/main/kotlin/com/didiglobal/booster/task/compression/CompressionVariantProcessor.kt @@ -34,26 +34,26 @@ import java.util.zip.ZipOutputStream * Represents a variant processor for resources compression, the task dependency graph shows as below: * * ``` - * +------------------+ - * | processResources | - * +--------+---------+ - * | - * +-----------+------------+ - * | | - * v v - * +----------+----------+ +---------+---------+ - * | compressResources | | compressAssets | - * +----------+----------+ +---------+---------+ - * | | - * v v - * +----------+----------+ +---------+---------+ - * | reduceResources | | mergeAssets | - * +----------+----------+ +-------------------+ - * | - * v - * +----------+----------+ - * | mergeResources | - * +---------------------+ + * +------------------+ + * | processResources | + * +--------+---------+ + * | + * +--------------+-----------+ + * | | + * v v + * +-------------+------------+ +---------+---------+ + * | compressResources | | compressAssets | + * +-------------+------------+ +---------+---------+ + * | | + * v v + * +-------------+------------+ +---------+---------+ + * | removeRedundantResources | | mergeAssets | + * +-------------+------------+ +-------------------+ + * | + * v + * +-------------+------------+ + * | mergeResources | + * +--------------------------+ * * ``` * @@ -63,15 +63,16 @@ import java.util.zip.ZipOutputStream class CompressionVariantProcessor : VariantProcessor { override fun process(variant: BaseVariant) { + val aapt2 = variant.project.aapt2Enabled + val pngFilter = if (aapt2) ::isFlatPng else ::isPng val results = CompressionResult() val processRes = variant.project.tasks.withType(ProcessAndroidResources::class.java).findByName("process${variant.name.capitalize()}Resources")?.doLast { compressProcessedRes(variant, results) generateReport(variant, results) } - val aapt2 = variant.project.aapt2Enabled - val pngFilter = if (aapt2) ::isFlatPng else ::isPng - val reduceRedundancy = variant.project.tasks.create("reduce${variant.name.capitalize()}Redundancy", ReduceRedundancy::class.java) { + val klassRemoveRedundantFlatImages = if (aapt2) RemoveRedundantFlatImages::class else RemoveRedundantImages::class + val reduceRedundancy = variant.project.tasks.create("remove${variant.name.capitalize()}RedundantResources", klassRemoveRedundantFlatImages.java) { it.outputs.upToDateWhen { false } it.variant = variant it.results = results diff --git a/booster-task-compression/src/main/kotlin/com/didiglobal/booster/task/compression/ReduceRedundancy.kt b/booster-task-compression/src/main/kotlin/com/didiglobal/booster/task/compression/RemoveRedundantFlatImages.kt similarity index 52% rename from booster-task-compression/src/main/kotlin/com/didiglobal/booster/task/compression/ReduceRedundancy.kt rename to booster-task-compression/src/main/kotlin/com/didiglobal/booster/task/compression/RemoveRedundantFlatImages.kt index 95f5f3eb0..c158c0485 100644 --- a/booster-task-compression/src/main/kotlin/com/didiglobal/booster/task/compression/ReduceRedundancy.kt +++ b/booster-task-compression/src/main/kotlin/com/didiglobal/booster/task/compression/RemoveRedundantFlatImages.kt @@ -1,34 +1,24 @@ package com.didiglobal.booster.task.compression -import android.aapt.pb.internal.ResourcesInternal -import com.android.build.gradle.api.BaseVariant -import org.gradle.api.DefaultTask import org.gradle.api.tasks.TaskAction import java.io.File +import java.util.stream.Collectors /** * Represents a task for redundant resources reducing * * @author johnsonlee */ -internal open class ReduceRedundancy : DefaultTask() { - - lateinit var variant: BaseVariant - - lateinit var results: CompressionResult - - lateinit var sources: () -> Collection - - lateinit var retained: () -> Collection> +internal open class RemoveRedundantFlatImages : RemoveRedundantImages() { @TaskAction - fun run() { - val resources = sources().asSequence().map { + override fun run() { + val resources = sources().parallelStream().map { it to it.metadata - } + }.collect(Collectors.toSet()) - resources.filterNot { - it.second == null + resources.filter { + it.second != null }.groupBy({ it.second!!.resourceName.substringAfterLast('/') }, { @@ -42,12 +32,6 @@ internal open class ReduceRedundancy : DefaultTask() { results.add(Triple(it.first, File(it.second.sourcePath).length(), 0)) } } - - this.retained = { - resources.filter { - it.first.exists() - }.toList() - } } } diff --git a/booster-task-compression/src/main/kotlin/com/didiglobal/booster/task/compression/RemoveRedundantImages.kt b/booster-task-compression/src/main/kotlin/com/didiglobal/booster/task/compression/RemoveRedundantImages.kt new file mode 100644 index 000000000..85ce788d2 --- /dev/null +++ b/booster-task-compression/src/main/kotlin/com/didiglobal/booster/task/compression/RemoveRedundantImages.kt @@ -0,0 +1,25 @@ +package com.didiglobal.booster.task.compression + +import android.aapt.pb.internal.ResourcesInternal +import com.android.build.gradle.api.BaseVariant +import org.gradle.api.DefaultTask +import org.gradle.api.tasks.TaskAction +import java.io.File + +/** + * Represents a task for redundant resources reducing + */ +internal open class RemoveRedundantImages: DefaultTask() { + + lateinit var variant: BaseVariant + + lateinit var results: CompressionResult + + lateinit var sources: () -> Collection + + @TaskAction + open fun run() { + TODO("Reducing redundant resources without aapt2 enabled has not supported yet") + } + +}