Skip to content

Commit

Permalink
Refactor for redundant resources reducing
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsonlee committed May 11, 2019
1 parent d85c9cc commit 12905c5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
* +--------------------------+
*
* ```
*
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<File>

lateinit var retained: () -> Collection<Pair<File, ResourcesInternal.CompiledFile?>>
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('/')
}, {
Expand All @@ -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()
}
}

}
Original file line number Diff line number Diff line change
@@ -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<File>

@TaskAction
open fun run() {
TODO("Reducing redundant resources without aapt2 enabled has not supported yet")
}

}

0 comments on commit 12905c5

Please sign in to comment.