From f3bdb6bdabf30f41e4b6aea23ed2d44038d5aa27 Mon Sep 17 00:00:00 2001 From: 0xera <56160164+0xera@users.noreply.github.com> Date: Fri, 17 Jan 2025 15:57:32 +0300 Subject: [PATCH] added a size calculation mode option to support calculation using raw size --- .../main/kotlin/com/grab/sizer/AppSizer.kt | 4 +- .../com/grab/sizer/SizeCalculationMode.kt | 48 +++++++++++++++++++ .../com/grab/sizer/analyzer/ApkAnalyzer.kt | 40 ++++++++-------- .../com/grab/sizer/analyzer/BasicAnalyzer.kt | 24 +++++----- .../grab/sizer/analyzer/CodebaseAnalyzer.kt | 2 +- .../grab/sizer/analyzer/LargeFileAnalyzer.kt | 14 ++++-- .../grab/sizer/analyzer/LibContentAnalyzer.kt | 2 +- .../grab/sizer/analyzer/LibrariesAnalyzer.kt | 17 ++++--- .../com/grab/sizer/analyzer/ModuleAnalyzer.kt | 4 +- .../com/grab/sizer/analyzer/ReportItem.kt | 12 ++--- .../sizer/analyzer/model/ClassFileInfo.kt | 14 +++++- .../grab/sizer/analyzer/model/Contributor.kt | 20 ++++---- .../grab/sizer/analyzer/model/RawFileInfo.kt | 16 +++++-- .../com/grab/sizer/analyzer/model/Team.kt | 42 ++++++++-------- .../com/grab/sizer/di/AnalyzerComponent.kt | 2 + .../com/grab/sizer/parser/AarFileParser.kt | 11 +++-- .../com/grab/sizer/parser/ApkFileParser.kt | 9 ++-- .../com/grab/sizer/parser/DexFileInfo.kt | 20 +++++--- .../com/grab/sizer/parser/DexFileParser.kt | 14 ++++-- .../com/grab/sizer/parser/JarFileParser.kt | 12 +++-- .../com/grab/sizer/parser/JarStreamParser.kt | 18 ++++--- .../kotlin/com/grab/sizer/report/ReportExt.kt | 36 +++++++------- .../com/grab/sizer/analyzer/Project1Data.kt | 36 +++++++------- .../kotlin/com/grab/sizer/analyzer/Utils.kt | 12 +++-- .../sizer/parser/DefaultAarFileParserTest.kt | 3 +- .../sizer/parser/DefaultApkFileParserTest.kt | 9 ++-- .../sizer/parser/DefaultJarFileParserTest.kt | 3 +- .../kotlin/com/grab/sizer/AnalyzerCommand.kt | 9 +++- .../com/grab/plugin/sizer/ProjectParams.kt | 4 ++ .../plugin/sizer/tasks/AppSizeAnalysisTask.kt | 7 ++- 30 files changed, 295 insertions(+), 169 deletions(-) create mode 100644 app-sizer/src/main/kotlin/com/grab/sizer/SizeCalculationMode.kt diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/AppSizer.kt b/app-sizer/src/main/kotlin/com/grab/sizer/AppSizer.kt index 6a50553..306d170 100644 --- a/app-sizer/src/main/kotlin/com/grab/sizer/AppSizer.kt +++ b/app-sizer/src/main/kotlin/com/grab/sizer/AppSizer.kt @@ -31,6 +31,7 @@ import com.grab.sizer.di.DaggerAnalyzerComponent import com.grab.sizer.utils.InputProvider import com.grab.sizer.utils.Logger import com.grab.sizer.utils.OutputProvider +import com.grab.sizer.SizeCalculationMode class AppSizer( private val inputProvider: InputProvider, @@ -38,12 +39,13 @@ class AppSizer( private val libName: String?, private val logger: Logger ) { - fun process(option: AnalyticsOption) { + fun process(option: AnalyticsOption, sizeCalculationMode: SizeCalculationMode) { val analyzerComponent = DaggerAnalyzerComponent.factory() .create( inputProvider = inputProvider, outputProvider = outputProvider, libName = libName, + sizeCalculationMode = sizeCalculationMode, logger = logger ) val analyzerMap = analyzerComponent.analyzerMap() diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/SizeCalculationMode.kt b/app-sizer/src/main/kotlin/com/grab/sizer/SizeCalculationMode.kt new file mode 100644 index 0000000..bdaccd8 --- /dev/null +++ b/app-sizer/src/main/kotlin/com/grab/sizer/SizeCalculationMode.kt @@ -0,0 +1,48 @@ +/* + * MIT License + * + * Copyright (c) 2025. Grabtaxi Holdings Pte Ltd (GRAB), All rights reserved. + * + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE + */ + +package com.grab.sizer + +/** + * Represents how to calculate sizes of files + */ +enum class SizeCalculationMode { + RAW, + DOWNLOADABLE; + + companion object { + /** + * Converts a string value to the corresponding SizeCalculationMode. + * If the value does not match any predefined options, the DOWNLOADABLE option is chosen. + */ + fun fromString(value: String?): SizeCalculationMode = when (value) { + "raw" -> RAW + "downloadable" -> DOWNLOADABLE + else -> DOWNLOADABLE + } + } +} \ No newline at end of file diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/ApkAnalyzer.kt b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/ApkAnalyzer.kt index 0293f89..c521dfa 100644 --- a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/ApkAnalyzer.kt +++ b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/ApkAnalyzer.kt @@ -68,7 +68,7 @@ internal class ApkAnalyzer @Inject constructor( } private fun generateReport(apks: Set, contributors: Set): Report { - val contributorList = contributors.sortedBy { it.getDownloadSize() } + val contributorList = contributors.sortedBy { it.getSize() } val apkReportRow = createApkReportRow(apks) val totalLibsReport = totalLibrariesReport(contributorList) val libComponentReport = libComponentReport(totalLibsReport) @@ -96,58 +96,58 @@ internal class ApkAnalyzer @Inject constructor( ): ReportItem = ReportItem( id = CODE_BASE_ID, name = CODE_BASE_ID, - totalDownloadSize = apkReport.totalDownloadSize - totalLibsReport.totalDownloadSize, - otherDownloadSize = apkReport.otherDownloadSize - totalLibsReport.otherDownloadSize, - resourceDownloadSize = apkReport.resourceDownloadSize - totalLibsReport.resourceDownloadSize, - nativeLibDownloadSize = apkReport.nativeLibDownloadSize - totalLibsReport.nativeLibDownloadSize, - assetDownloadSize = apkReport.assetDownloadSize - totalLibsReport.assetDownloadSize, - classesDownloadSize = apkReport.classesDownloadSize - totalLibsReport.classesDownloadSize, + totalSize = apkReport.totalSize - totalLibsReport.totalSize, + otherSize = apkReport.otherSize - totalLibsReport.otherSize, + resourceSize = apkReport.resourceSize - totalLibsReport.resourceSize, + nativeLibSize = apkReport.nativeLibSize - totalLibsReport.nativeLibSize, + assetSize = apkReport.assetSize - totalLibsReport.assetSize, + classesSize = apkReport.classesSize - totalLibsReport.classesSize, ) private fun Contributor.toReportItem(): ReportItem = ReportItem( name = File(path).nameWithoutExtension, extraInfo = path.substring(path.indexOf("files-2.1/") + 9), id = File(path).nameWithoutExtension, - totalDownloadSize = getDownloadSize(), - classesDownloadSize = classDownloadSize, - nativeLibDownloadSize = nativeLibDownloadSize, - resourceDownloadSize = resourcesDownloadSize, - assetDownloadSize = assetsDownloadSize, - otherDownloadSize = othersDownloadSize, + totalSize = getSize(), + classesSize = classSize, + nativeLibSize = nativeLibSize, + resourceSize = resourcesSize, + assetSize = assetsSize, + otherSize = othersSize, ) private fun libComponentReport(allLibReport: ReportItem): List = listOf( createRow( name = "android-java-libraries", - value = allLibReport.totalDownloadSize - allLibReport.nativeLibDownloadSize + value = allLibReport.totalSize - allLibReport.nativeLibSize ), createRow( name = "native-libraries", - value = allLibReport.nativeLibDownloadSize + value = allLibReport.nativeLibSize ) ) private fun codeBaseComponentReport(codeBaseReport: ReportItem): List = listOf( createRow( name = "codebase-kotlin-java", - value = codeBaseReport.classesDownloadSize, + value = codeBaseReport.classesSize, ), createRow( name = "codebase-resources", - value = codeBaseReport.resourceDownloadSize, + value = codeBaseReport.resourceSize, ), createRow( name = "codebase-assets", - value = codeBaseReport.assetDownloadSize, + value = codeBaseReport.assetSize, ), createRow( name = "codebase-native", - value = codeBaseReport.nativeLibDownloadSize, + value = codeBaseReport.nativeLibSize, ), createRow( name = "others", - value = codeBaseReport.otherDownloadSize, + value = codeBaseReport.otherSize, ), ) diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/BasicAnalyzer.kt b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/BasicAnalyzer.kt index 6f45e14..042bc63 100644 --- a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/BasicAnalyzer.kt +++ b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/BasicAnalyzer.kt @@ -58,14 +58,14 @@ internal class BasicApkAnalyzer @Inject constructor( } private fun Set.createApkReportRows(): List { - val resourceDownloadSize = flatMap { it.resources }.sumOf { it.downloadSize } - val nativeLibDownloadSize = flatMap { it.nativeLibs }.sumOf { it.downloadSize } - val assetDownloadSize = flatMap { it.assets }.sumOf { it.downloadSize } - val otherDownloadSize = flatMap { it.others }.sumOf { it.downloadSize } - val dexDownloadFile = flatMap { it.dexes }.sumOf { it.downloadSize } - val classDownloadSize = flatMap { it.dexes }.flatMap { it.classes }.sumOf { it.downloadSize } + val resourceSize = flatMap { it.resources }.sumOf { it.size } + val nativeLibSize = flatMap { it.nativeLibs }.sumOf { it.size } + val assetSize = flatMap { it.assets }.sumOf { it.size } + val otherSize = flatMap { it.others }.sumOf { it.size } + val dexSize = flatMap { it.dexes }.sumOf { it.size } + val classSize = flatMap { it.dexes }.flatMap { it.classes }.sumOf { it.downloadSize } val total = - resourceDownloadSize + nativeLibDownloadSize + assetDownloadSize + otherDownloadSize + classDownloadSize + resourceSize + nativeLibSize + assetSize + otherSize + classSize return listOf( createRow( @@ -74,23 +74,23 @@ internal class BasicApkAnalyzer @Inject constructor( ), createRow( name = "resource", - value = resourceDownloadSize + value = resourceSize ), createRow( name = "native_lib", - value = nativeLibDownloadSize + value = nativeLibSize ), createRow( name = "asset", - value = assetDownloadSize + value = assetSize ), createRow( name = "other", - value = otherDownloadSize + value = otherSize ), createRow( name = "code", - value = dexDownloadFile + value = dexSize ) ) } diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/CodebaseAnalyzer.kt b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/CodebaseAnalyzer.kt index 2aeee68..d66f110 100644 --- a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/CodebaseAnalyzer.kt +++ b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/CodebaseAnalyzer.kt @@ -94,7 +94,7 @@ internal class CodebaseAnalyzer @Inject constructor( private fun Team.toReportRow(): Row = createRow( name, - getDownloadSize(), + getSize(), ) } diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/LargeFileAnalyzer.kt b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/LargeFileAnalyzer.kt index c3d51f9..e93318c 100644 --- a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/LargeFileAnalyzer.kt +++ b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/LargeFileAnalyzer.kt @@ -28,7 +28,11 @@ package com.grab.sizer.analyzer import com.grab.sizer.analyzer.mapper.ApkComponentProcessor -import com.grab.sizer.analyzer.model.* +import com.grab.sizer.analyzer.model.Contributor +import com.grab.sizer.analyzer.model.Team +import com.grab.sizer.analyzer.model.castToClass +import com.grab.sizer.analyzer.model.castToRawFile +import com.grab.sizer.analyzer.model.toTeams import com.grab.sizer.parser.DataParser import com.grab.sizer.parser.getAars import com.grab.sizer.parser.getJars @@ -93,8 +97,8 @@ internal class LargeFileAnalyzer @Inject constructor( } private fun Set.filterLargeFileContributors(): Set = map { - val resources = it.resources.filter { file -> file.downloadSize >= largeFileThreshold }.toSet() - val assets = it.assets.filter { file -> file.downloadSize >= largeFileThreshold }.toSet() + val resources = it.resources.filter { file -> file.size >= largeFileThreshold }.toSet() + val assets = it.assets.filter { file -> file.size >= largeFileThreshold }.toSet() return@map it.copy(resources = resources, assets = assets) }.filter { it.resources.isNotEmpty() || it.assets.isNotEmpty() } .toSet() @@ -110,7 +114,7 @@ internal class LargeFileAnalyzer @Inject constructor( } private fun List.sorByResources(): List = this.sortedBy { - it.resourcesDownloadSize + it.assetsDownloadSize + it.resourcesSize + it.assetsSize } private fun List.toReportRows(): List = map { it to it.modules } @@ -122,7 +126,7 @@ internal class LargeFileAnalyzer @Inject constructor( val fileName = segmentPaths.last() createRow( name = fileName, - value = res.downloadSize, + value = res.size, owner = pair.first.name, tag = module.tag, rowName = fileName diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/LibContentAnalyzer.kt b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/LibContentAnalyzer.kt index 312fe75..dd9215c 100644 --- a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/LibContentAnalyzer.kt +++ b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/LibContentAnalyzer.kt @@ -82,7 +82,7 @@ internal class LibContentAnalyzer @Inject constructor( createRow( rowName = it.name, name = it.name, - value = it.downloadSize, + value = it.size, tag = type ) } diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/LibrariesAnalyzer.kt b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/LibrariesAnalyzer.kt index e0a2260..d6b20a1 100644 --- a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/LibrariesAnalyzer.kt +++ b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/LibrariesAnalyzer.kt @@ -29,7 +29,6 @@ package com.grab.sizer.analyzer import com.grab.sizer.analyzer.mapper.ApkComponentProcessor import com.grab.sizer.analyzer.model.Contributor -import com.grab.sizer.parser.ApkFileInfo import com.grab.sizer.parser.DataParser import com.grab.sizer.report.Report import java.io.File @@ -58,12 +57,12 @@ internal class LibrariesAnalyzer @Inject constructor( } private fun generateReport(contributors: Set): Report { - val contributorList = contributors.sortedBy { it.getDownloadSize() } + val contributorList = contributors.sortedBy { it.getSize() } val listOfReport = reportPerLibrary(contributorList) return Report( id = LIBRARY_METRICS_ID, name = LIBRARY_METRICS_ID, - rows = listOfReport.map { reportItem -> createRow(reportItem.name, reportItem.totalDownloadSize) }, + rows = listOfReport.map { reportItem -> createRow(reportItem.name, reportItem.totalSize) }, ) } @@ -71,12 +70,12 @@ internal class LibrariesAnalyzer @Inject constructor( name = tag, extraInfo = path.substring(path.indexOf("files-2.1/") + 9), id = File(path).nameWithoutExtension, - totalDownloadSize = getDownloadSize(), - classesDownloadSize = classDownloadSize, - nativeLibDownloadSize = nativeLibDownloadSize, - resourceDownloadSize = resourcesDownloadSize, - assetDownloadSize = assetsDownloadSize, - otherDownloadSize = othersDownloadSize, + totalSize = getSize(), + classesSize = classSize, + nativeLibSize = nativeLibSize, + resourceSize = resourcesSize, + assetSize = assetsSize, + otherSize = othersSize, ) private fun reportPerLibrary(data: List): List = diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/ModuleAnalyzer.kt b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/ModuleAnalyzer.kt index f1b436c..c344ed5 100644 --- a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/ModuleAnalyzer.kt +++ b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/ModuleAnalyzer.kt @@ -79,7 +79,7 @@ internal class ModuleAnalyzer @Inject constructor( private fun generateReport(contributors: Set): Report = contributors.toModules() .run { - val sortedTeamsReport = sortedBy { it.getDownloadSize() } + val sortedTeamsReport = sortedBy { it.getSize() } .map { it.toReportItem(teamMapping.moduleToTeamMap) } return Report( id = METRICS_ID_MODULES, @@ -92,7 +92,7 @@ internal class ModuleAnalyzer @Inject constructor( reportItems.map { reportItem -> createRow( name = reportItem.id, - value = reportItem.totalDownloadSize, + value = reportItem.totalSize, owner = reportItem.owner ?: NOT_AVAILABLE_VALUE, rowName = reportItem.name ) diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/ReportItem.kt b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/ReportItem.kt index c166b25..6c51ee0 100644 --- a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/ReportItem.kt +++ b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/ReportItem.kt @@ -30,13 +30,13 @@ package com.grab.sizer.analyzer internal data class ReportItem( val id: String, - val totalDownloadSize: Long, + val totalSize: Long, val name: String = "", val extraInfo: String = "", val owner: String? = null, - val classesDownloadSize: Long = 0L, - val nativeLibDownloadSize: Long = 0L, - val resourceDownloadSize: Long = 0L, - val assetDownloadSize: Long = 0L, - val otherDownloadSize: Long = 0L, + val classesSize: Long = 0L, + val nativeLibSize: Long = 0L, + val resourceSize: Long = 0L, + val assetSize: Long = 0L, + val otherSize: Long = 0L, ) \ No newline at end of file diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/model/ClassFileInfo.kt b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/model/ClassFileInfo.kt index c9a7bed..82cfc11 100644 --- a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/model/ClassFileInfo.kt +++ b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/model/ClassFileInfo.kt @@ -27,19 +27,29 @@ package com.grab.sizer.analyzer.model +import com.grab.sizer.SizeCalculationMode + /** * Represents a class file in the aar/jar/apk file * * @property name The name of the class. - * @property size The original, uncompressed size of the class file. + * @property rawSize The original, uncompressed size of the class file. * @property downloadSize The size of the class file in the binary that is downloadable from Google Play. */ data class ClassFileInfo( override val name: String, - override val size: Long, + override val rawSize: Long, override val downloadSize: Long = 0, + private val sizeCalculationMode: SizeCalculationMode ) : FileInfo { + + override val size: Long + get() = when (sizeCalculationMode) { + SizeCalculationMode.RAW -> rawSize + SizeCalculationMode.DOWNLOADABLE -> downloadSize + } + override fun equals(other: Any?): Boolean { if (other is ClassFileInfo) return name == other.name return super.equals(other) diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/model/Contributor.kt b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/model/Contributor.kt index db052d9..8c9b250 100644 --- a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/model/Contributor.kt +++ b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/model/Contributor.kt @@ -52,24 +52,24 @@ data class Contributor( get() = originalOwner.tag val path: String get() = originalOwner.path - // Calculates the sum of the download sizes of all resources - val resourcesDownloadSize: Long by lazy { resources.sumOf { resource -> resource.downloadSize } } + // Calculates the sum of the sizes of all resources + val resourcesSize: Long by lazy { resources.sumOf { resource -> resource.size } } - // Calculates the sum of the download sizes of all native libraries - val nativeLibDownloadSize: Long by lazy { nativeLibs.sumOf { lib -> lib.downloadSize } } + // Calculates the sum of the sizes of all native libraries + val nativeLibSize: Long by lazy { nativeLibs.sumOf { lib -> lib.size } } // Calculates the sum of the download sizes of all assets - val assetsDownloadSize: Long by lazy { assets.sumOf { asset -> asset.downloadSize } } + val assetsSize: Long by lazy { assets.sumOf { asset -> asset.size } } // Calculates the sum of the download sizes of all "other" files - val othersDownloadSize: Long by lazy { others.sumOf { other -> other.downloadSize } } + val othersSize: Long by lazy { others.sumOf { other -> other.size } } // Calculates the sum of the sizes of all classes - val classDownloadSize: Long by lazy { classes.sumOf { clazz -> clazz.downloadSize } } + val classSize: Long by lazy { classes.sumOf { clazz -> clazz.size } } - // Calculates the total downloadable size of all component types (assets, resources, native libraries, classes, others). - fun getDownloadSize(): Long = - resourcesDownloadSize + nativeLibDownloadSize + assetsDownloadSize + othersDownloadSize + classDownloadSize + // Calculates the total size of all component types (assets, resources, native libraries, classes, others). + fun getSize(): Long = + resourcesSize + nativeLibSize + assetsSize + othersSize + classSize override fun equals(other: Any?): Boolean { if (other is Contributor) { diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/model/RawFileInfo.kt b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/model/RawFileInfo.kt index ef95b7f..1008517 100644 --- a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/model/RawFileInfo.kt +++ b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/model/RawFileInfo.kt @@ -27,6 +27,7 @@ package com.grab.sizer.analyzer.model +import com.grab.sizer.SizeCalculationMode import java.io.File @@ -42,11 +43,13 @@ enum class FileType { * * @property name The name of the file/class. * @property downloadSize The size of the file in the binary that is downloadable from Google Play.(zipped APKs) - * @property size The original size of the file. + * @property rawSize The original size of the file. + * @property size Original or downloadable size depending on plugin settings */ interface FileInfo { val name: String val downloadSize: Long + val rawSize: Long val size: Long } @@ -55,13 +58,20 @@ interface FileInfo { * * @property path The path to the raw file within the aar/jar file. * @property downloadSize The size of the file in the downloadable binary from Google Play. - * @property size The original, uncompressed size of the file. + * @property rawSize The original, uncompressed size of the file. */ data class RawFileInfo( val path: String, override val downloadSize: Long, - override val size: Long + override val rawSize: Long, + private val sizeCalculationMode: SizeCalculationMode ) : FileInfo { + + override val size: Long get() = when (sizeCalculationMode) { + SizeCalculationMode.RAW -> rawSize + SizeCalculationMode.DOWNLOADABLE -> downloadSize + } + val type: FileType get() = when { path.startsWith("/res/") -> FileType.RESOURCE diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/model/Team.kt b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/model/Team.kt index 68ca52a..29fd32a 100644 --- a/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/model/Team.kt +++ b/app-sizer/src/main/kotlin/com/grab/sizer/analyzer/model/Team.kt @@ -35,12 +35,12 @@ data class Team( val name: String, val modules: List ) { - val resourcesDownloadSize: Long by lazy { modules.sumOf { contributor -> contributor.resourcesDownloadSize } } - val nativeLibDownloadSize: Long by lazy { modules.sumOf { contributor -> contributor.nativeLibDownloadSize } } - val assetsDownloadSize: Long by lazy { modules.sumOf { contributor -> contributor.assetsDownloadSize } } - val othersDownloadSize: Long by lazy { modules.sumOf { contributor -> contributor.othersDownloadSize } } - val classDownloadSize: Long by lazy { modules.sumOf { contributor -> contributor.classDownloadSize } } - fun getDownloadSize(): Long = modules.sumOf { it.getDownloadSize() } + val resourcesSize: Long by lazy { modules.sumOf { contributor -> contributor.resourcesSize } } + val nativeLibSize: Long by lazy { modules.sumOf { contributor -> contributor.nativeLibSize } } + val assetsSize: Long by lazy { modules.sumOf { contributor -> contributor.assetsSize } } + val othersSize: Long by lazy { modules.sumOf { contributor -> contributor.othersSize } } + val classSize: Long by lazy { modules.sumOf { contributor -> contributor.classSize } } + fun getSize(): Long = modules.sumOf { it.getSize() } } data class Module( @@ -51,14 +51,14 @@ data class Module( get() = owner.tag val path: String get() = owner.path - val resourcesDownloadSize: Long by lazy { contributors.sumOf { contributor -> contributor.resourcesDownloadSize } } - val nativeLibDownloadSize: Long by lazy { contributors.sumOf { contributor -> contributor.nativeLibDownloadSize } } - val assetsDownloadSize: Long by lazy { contributors.sumOf { contributor -> contributor.assetsDownloadSize } } - val othersDownloadSize: Long by lazy { contributors.sumOf { contributor -> contributor.othersDownloadSize } } - val classDownloadSize: Long by lazy { contributors.sumOf { contributor -> contributor.classDownloadSize } } + val resourcesSize: Long by lazy { contributors.sumOf { contributor -> contributor.resourcesSize } } + val nativeLibSize: Long by lazy { contributors.sumOf { contributor -> contributor.nativeLibSize } } + val assetsSize: Long by lazy { contributors.sumOf { contributor -> contributor.assetsSize } } + val othersSize: Long by lazy { contributors.sumOf { contributor -> contributor.othersSize } } + val classSize: Long by lazy { contributors.sumOf { contributor -> contributor.classSize } } - fun getDownloadSize(): Long = - resourcesDownloadSize + nativeLibDownloadSize + assetsDownloadSize + othersDownloadSize + classDownloadSize + fun getSize(): Long = + resourcesSize + nativeLibSize + assetsSize + othersSize + classSize } internal fun Set.toTeams(teamMapping: TeamMapping): List { @@ -72,8 +72,8 @@ internal fun Set.toTeams(teamMapping: TeamMapping): List { internal fun List.sort(): List { return sortedWith { o1, o2 -> - val size1 = o1.getDownloadSize() - val size2 = o2.getDownloadSize() + val size1 = o1.getSize() + val size2 = o2.getSize() if (size1 > size2) -1 else if (size1 < size2) 1 else 0 @@ -97,11 +97,11 @@ internal fun Module.toReportItem(moduleToTeamMap: Map): ReportIt id = tag, owner = moduleToTeamMap[tag], extraInfo = "Sum up all codebase for $tag", - totalDownloadSize = getDownloadSize(), - classesDownloadSize = classDownloadSize, - nativeLibDownloadSize = nativeLibDownloadSize, - resourceDownloadSize = resourcesDownloadSize, - assetDownloadSize = assetsDownloadSize, - otherDownloadSize = othersDownloadSize + totalSize = getSize(), + classesSize = classSize, + nativeLibSize = nativeLibSize, + resourceSize = resourcesSize, + assetSize = assetsSize, + otherSize = othersSize ) diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/di/AnalyzerComponent.kt b/app-sizer/src/main/kotlin/com/grab/sizer/di/AnalyzerComponent.kt index c2668e0..34a25e2 100644 --- a/app-sizer/src/main/kotlin/com/grab/sizer/di/AnalyzerComponent.kt +++ b/app-sizer/src/main/kotlin/com/grab/sizer/di/AnalyzerComponent.kt @@ -35,6 +35,7 @@ import com.grab.sizer.report.ReportWriter import com.grab.sizer.utils.InputProvider import com.grab.sizer.utils.Logger import com.grab.sizer.utils.OutputProvider +import com.grab.sizer.SizeCalculationMode import dagger.BindsInstance import dagger.Component import javax.inject.Named @@ -61,6 +62,7 @@ interface AnalyzerComponent { fun create( @BindsInstance inputProvider: InputProvider, @BindsInstance outputProvider: OutputProvider, + @BindsInstance sizeCalculationMode: SizeCalculationMode, @BindsInstance @Named(NAMED_LIB_NAME) libName: String?, @BindsInstance logger: Logger, ): AnalyzerComponent diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/parser/AarFileParser.kt b/app-sizer/src/main/kotlin/com/grab/sizer/parser/AarFileParser.kt index 2a01c7f..ce5f478 100644 --- a/app-sizer/src/main/kotlin/com/grab/sizer/parser/AarFileParser.kt +++ b/app-sizer/src/main/kotlin/com/grab/sizer/parser/AarFileParser.kt @@ -31,6 +31,7 @@ import com.grab.sizer.analyzer.model.FileType import com.grab.sizer.analyzer.model.RawFileInfo import com.grab.sizer.di.AppScope import com.grab.sizer.utils.SizerInputFile +import com.grab.sizer.SizeCalculationMode import java.util.zip.ZipFile import javax.inject.Inject @@ -48,7 +49,10 @@ interface AarFileParser { * For more about the AAR file format, see: http://tools.android.com/tech-docs/new-build-system/aar-format */ @AppScope -class DefaultAarFileParser @Inject constructor(private val jarParser: JarStreamParser) : AarFileParser { +class DefaultAarFileParser @Inject constructor( + private val jarParser: JarStreamParser, + private val sizeCalculationMode: SizeCalculationMode +) : AarFileParser { private fun parse(sizerInputFile: SizerInputFile): AarFileInfo { ZipFile(sizerInputFile.file).use { zipFile -> @@ -62,8 +66,9 @@ class DefaultAarFileParser @Inject constructor(private val jarParser: JarStreamP val entry = entries.nextElement() val fileInfo = RawFileInfo( path = entry.getPath(), - size = entry.size, - downloadSize = -1, + rawSize = entry.size, + downloadSize = entry.size, + sizeCalculationMode = sizeCalculationMode ) when (fileInfo.type) { diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/parser/ApkFileParser.kt b/app-sizer/src/main/kotlin/com/grab/sizer/parser/ApkFileParser.kt index a16945d..b2ee269 100644 --- a/app-sizer/src/main/kotlin/com/grab/sizer/parser/ApkFileParser.kt +++ b/app-sizer/src/main/kotlin/com/grab/sizer/parser/ApkFileParser.kt @@ -31,6 +31,7 @@ import com.android.tools.apk.analyzer.ApkSizeCalculator import com.grab.sizer.analyzer.model.FileType import com.grab.sizer.analyzer.model.RawFileInfo import com.grab.sizer.di.AppScope +import com.grab.sizer.SizeCalculationMode import shadow.bundletool.com.android.tools.proguard.ProguardMap import java.io.File import java.nio.file.Path @@ -55,7 +56,8 @@ internal interface ApkFileParser { @AppScope internal class DefaultApkFileParser @Inject constructor( private val dexFileParser: DexFileParser, - private val apkSizeCalculator: ApkSizeCalculator + private val apkSizeCalculator: ApkSizeCalculator, + private val sizeCalculationMode: SizeCalculationMode ) : ApkFileParser { override fun parseApks(apks: Sequence, proguardMap: ProguardMap): Set = apks .map { apkFile -> parse(apkFile, proguardMap) } @@ -83,7 +85,8 @@ internal class DefaultApkFileParser @Inject constructor( val fileInfo = RawFileInfo( path = path, downloadSize = downloadSize, - size = rawSize + rawSize = rawSize, + sizeCalculationMode = sizeCalculationMode ) when (fileInfo.type) { @@ -116,7 +119,7 @@ internal class DefaultApkFileParser @Inject constructor( private fun ApkSizeCalculator.parseSize(path: Path): ApkSizeInfo = ApkSizeInfo( downloadSize = getFullApkDownloadSize(path), - size = getFullApkDownloadSize(path), + size = getFullApkRawSize(path), downloadFileSizeMap = getDownloadSizePerFile(path), rawFileSizeMap = getRawSizePerFile(path) ) diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/parser/DexFileInfo.kt b/app-sizer/src/main/kotlin/com/grab/sizer/parser/DexFileInfo.kt index 1d06715..31790b8 100644 --- a/app-sizer/src/main/kotlin/com/grab/sizer/parser/DexFileInfo.kt +++ b/app-sizer/src/main/kotlin/com/grab/sizer/parser/DexFileInfo.kt @@ -28,7 +28,9 @@ package com.grab.sizer.parser import com.grab.sizer.analyzer.model.ClassFileInfo +import com.grab.sizer.analyzer.model.FileInfo import com.grab.sizer.analyzer.model.RawFileInfo +import com.grab.sizer.SizeCalculationMode /** * A data class that represents a dex file parsed from the APK by [ApkFileParser]. @@ -37,16 +39,22 @@ import com.grab.sizer.analyzer.model.RawFileInfo * @property name The name of the dex file. * @property downloadSize The download size of the dex file. * @property classes A set of ClassFileInfo objects representing classes contained in the dex file. - * @property size The size of the dex file. + * @property rawSize The size of the dex file. */ data class DexFileInfo( - val name: String, - val downloadSize: Long, + override val name: String, + override val downloadSize: Long, val classes: Set, val others: Set = emptySet(), - val size: Long, -) { + override val rawSize: Long, + private val sizeCalculationMode: SizeCalculationMode +) : FileInfo { + + override val size: Long get() = when (sizeCalculationMode) { + SizeCalculationMode.RAW -> rawSize + SizeCalculationMode.DOWNLOADABLE -> downloadSize + } // The total size of classes and other files in the dex file (computed lazily). - val classSize: Long by lazy { classes.sumOf { it.size } + others.sumOf { it.size } } + val classSize: Long by lazy { classes.sumOf { it.rawSize } + others.sumOf { it.rawSize } } } diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/parser/DexFileParser.kt b/app-sizer/src/main/kotlin/com/grab/sizer/parser/DexFileParser.kt index 2ee1439..d769f2a 100644 --- a/app-sizer/src/main/kotlin/com/grab/sizer/parser/DexFileParser.kt +++ b/app-sizer/src/main/kotlin/com/grab/sizer/parser/DexFileParser.kt @@ -32,6 +32,7 @@ import com.grab.sizer.analyzer.model.ClassFileInfo import com.grab.sizer.di.AppScope import com.grab.sizer.utils.Logger import com.grab.sizer.utils.log +import com.grab.sizer.SizeCalculationMode import org.jf.dexlib2.dexbacked.DexBackedClassDef import org.jf.dexlib2.dexbacked.DexBackedDexFile import shadow.bundletool.com.android.tools.proguard.ProguardMap @@ -63,7 +64,8 @@ internal interface DexFileParser { */ @AppScope internal class DefaultDexFileParser @Inject constructor( - private val logger: Logger + private val logger: Logger, + private val sizeCalculationMode: SizeCalculationMode ) : DexFileParser { override fun parse( entry: ZipEntry, @@ -79,13 +81,14 @@ internal class DefaultDexFileParser @Inject constructor( val path = entry.getPath() val dexDownloadSize = apkSizeInfo.downloadFileSizeMap[path] ?: 0 - val dexClassesSize = classes.sumOf { it.size } + val dexClassesSize = classes.sumOf { it.rawSize } val ratio = dexDownloadSize.toDouble() / dexClassesSize return DexFileInfo( name = path, downloadSize = dexDownloadSize, - size = apkSizeInfo.rawFileSizeMap[path] ?: 0, - classes = classes.map { it.copy(downloadSize = (it.size * ratio).toLong()) }.toSet() + rawSize = apkSizeInfo.rawFileSizeMap[path] ?: 0, + classes = classes.map { it.copy(downloadSize = (it.rawSize * ratio).toLong()) }.toSet(), + sizeCalculationMode = sizeCalculationMode ) } @@ -96,7 +99,8 @@ internal class DefaultDexFileParser @Inject constructor( } return ClassFileInfo( name = proguardMap?.getClassName(className) ?: className, - size = classDef.size.toLong() + rawSize = classDef.size.toLong(), + sizeCalculationMode = sizeCalculationMode ) } } \ No newline at end of file diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/parser/JarFileParser.kt b/app-sizer/src/main/kotlin/com/grab/sizer/parser/JarFileParser.kt index 0dbd7c6..e68fe86 100644 --- a/app-sizer/src/main/kotlin/com/grab/sizer/parser/JarFileParser.kt +++ b/app-sizer/src/main/kotlin/com/grab/sizer/parser/JarFileParser.kt @@ -32,6 +32,7 @@ import com.grab.sizer.analyzer.model.FileType import com.grab.sizer.analyzer.model.RawFileInfo import com.grab.sizer.di.AppScope import com.grab.sizer.utils.SizerInputFile +import com.grab.sizer.SizeCalculationMode import java.util.zip.ZipFile import javax.inject.Inject @@ -46,7 +47,9 @@ interface JarFileParser { } @AppScope -class DefaultJarFileParser @Inject constructor() : JarFileParser { +class DefaultJarFileParser @Inject constructor( + private val sizeCalculationMode: SizeCalculationMode +) : JarFileParser { private fun parse(sizerInputFile: SizerInputFile): JarFileInfo { ZipFile(sizerInputFile.file).use { zipFile -> val entries = zipFile.entries() @@ -57,12 +60,13 @@ class DefaultJarFileParser @Inject constructor() : JarFileParser { val entry = entries.nextElement() val fileInfo = RawFileInfo( path = entry.getPath(), - size = entry.size, - downloadSize = -1 + rawSize = entry.size, + downloadSize = entry.size, + sizeCalculationMode = sizeCalculationMode ) when (fileInfo.type) { FileType.NATIVE_LIB -> nativeLibs.add(fileInfo) - FileType.CLASS -> classes.add(entry.toClass()) + FileType.CLASS -> classes.add(entry.toClass(sizeCalculationMode)) else -> others.add(fileInfo) } } diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/parser/JarStreamParser.kt b/app-sizer/src/main/kotlin/com/grab/sizer/parser/JarStreamParser.kt index c899e08..6bc3688 100644 --- a/app-sizer/src/main/kotlin/com/grab/sizer/parser/JarStreamParser.kt +++ b/app-sizer/src/main/kotlin/com/grab/sizer/parser/JarStreamParser.kt @@ -27,6 +27,7 @@ package com.grab.sizer.parser +import com.grab.sizer.SizeCalculationMode import com.grab.sizer.analyzer.model.ClassFileInfo import com.grab.sizer.analyzer.model.FileType import com.grab.sizer.analyzer.model.RawFileInfo @@ -52,7 +53,9 @@ interface JarStreamParser { } @AppScope -class DefaultJarStreamParser @Inject constructor() : JarStreamParser { +class DefaultJarStreamParser @Inject constructor( + private val sizeCalculationMode: SizeCalculationMode +) : JarStreamParser { override fun parse(jarEntry: ZipEntry, inputStream: InputStream): JarFileInfo { ZipInputStream(inputStream).use { entries -> val others = mutableSetOf() @@ -61,11 +64,12 @@ class DefaultJarStreamParser @Inject constructor() : JarStreamParser { while (entry != null) { val fileInfo = RawFileInfo( path = entry.getPath(), - size = entry.size, - downloadSize = -1 + rawSize = entry.size, + downloadSize = entry.size, + sizeCalculationMode = sizeCalculationMode ) when (fileInfo.type) { - FileType.CLASS -> classes.add(entry.toClass()) + FileType.CLASS -> classes.add(entry.toClass(sizeCalculationMode)) else -> others.add(fileInfo) } entry = entries.nextEntry @@ -82,14 +86,16 @@ class DefaultJarStreamParser @Inject constructor() : JarStreamParser { } } -internal fun ZipEntry.toClass(): ClassFileInfo { +internal fun ZipEntry.toClass(sizeCalculationMode: SizeCalculationMode): ClassFileInfo { return ClassFileInfo( /** * Convert ZipEntry name to class name * Example: "/com/grab/sample/dummy/DummyClass1.class" -> "com.grab.sample.dummy.DummyClass1" */ name = name.replace('/', '.').removeSuffix(".class"), - size = size + rawSize = size, + downloadSize = size, + sizeCalculationMode = sizeCalculationMode ) } diff --git a/app-sizer/src/main/kotlin/com/grab/sizer/report/ReportExt.kt b/app-sizer/src/main/kotlin/com/grab/sizer/report/ReportExt.kt index cf2f2ab..1650e8c 100644 --- a/app-sizer/src/main/kotlin/com/grab/sizer/report/ReportExt.kt +++ b/app-sizer/src/main/kotlin/com/grab/sizer/report/ReportExt.kt @@ -31,35 +31,35 @@ import com.grab.sizer.analyzer.ReportItem import com.grab.sizer.parser.ApkFileInfo internal fun Set.apksSizeReport(): ReportItem { - val resourceDownloadSize = flatMap { it.resources }.sumOf { it.downloadSize } - val nativeLibDownloadSize = flatMap { it.nativeLibs }.sumOf { it.downloadSize } - val assetDownloadSize = flatMap { it.assets }.sumOf { it.downloadSize } - val otherDownloadSize = flatMap { it.others }.sumOf { it.downloadSize } - val classDownloadSize = flatMap { it.dexes }.flatMap { it.classes }.sumOf { it.downloadSize } + val resourceSize = flatMap { it.resources }.sumOf { it.size } + val nativeLibSize = flatMap { it.nativeLibs }.sumOf { it.size } + val assetSize = flatMap { it.assets }.sumOf { it.size } + val otherSize = flatMap { it.others }.sumOf { it.size } + val classSize = flatMap { it.dexes }.flatMap { it.classes }.sumOf { it.size } val total = - resourceDownloadSize + nativeLibDownloadSize + assetDownloadSize + otherDownloadSize + classDownloadSize + resourceSize + nativeLibSize + assetSize + otherSize + classSize return ReportItem( id = "apk", - totalDownloadSize = total, + totalSize = total, name = "Apks", - resourceDownloadSize = resourceDownloadSize, - nativeLibDownloadSize = nativeLibDownloadSize, - assetDownloadSize = assetDownloadSize, - otherDownloadSize = otherDownloadSize, - classesDownloadSize = classDownloadSize, + resourceSize = resourceSize, + nativeLibSize = nativeLibSize, + assetSize = assetSize, + otherSize = otherSize, + classesSize = classSize, extraInfo = "Apk breakdown by component sizer" ) } internal fun Set.toReportField(): List { - val resourceDownloadSize = flatMap { it.resources }.sumOf { it.downloadSize } - val nativeLibDownloadSize = flatMap { it.nativeLibs }.sumOf { it.downloadSize } - val assetDownloadSize = flatMap { it.assets }.sumOf { it.downloadSize } - val otherDownloadSize = flatMap { it.others }.sumOf { it.downloadSize } - val classDownloadSize = flatMap { it.dexes }.flatMap { it.classes }.sumOf { it.downloadSize } + val resourceSize = flatMap { it.resources }.sumOf { it.size } + val nativeLibSize = flatMap { it.nativeLibs }.sumOf { it.size } + val assetSize = flatMap { it.assets }.sumOf { it.size } + val otherSize = flatMap { it.others }.sumOf { it.size } + val classSize = flatMap { it.dexes }.flatMap { it.classes }.sumOf { it.downloadSize } val total = - resourceDownloadSize + nativeLibDownloadSize + assetDownloadSize + otherDownloadSize + classDownloadSize + resourceSize + nativeLibSize + assetSize + otherSize + classSize return listOf( TagField( name = FIELD_KEY_CONTRIBUTOR, diff --git a/app-sizer/src/test/kotlin/com/grab/sizer/analyzer/Project1Data.kt b/app-sizer/src/test/kotlin/com/grab/sizer/analyzer/Project1Data.kt index 959c880..5aa6b08 100644 --- a/app-sizer/src/test/kotlin/com/grab/sizer/analyzer/Project1Data.kt +++ b/app-sizer/src/test/kotlin/com/grab/sizer/analyzer/Project1Data.kt @@ -27,6 +27,7 @@ package com.grab.sizer.analyzer +import com.grab.sizer.SizeCalculationMode import com.grab.sizer.analyzer.mapper.createEmptyAar import com.grab.sizer.analyzer.mapper.createEmptyApkInfo import com.grab.sizer.analyzer.mapper.createEmptyJar @@ -37,38 +38,38 @@ private const val TEAM_2 = "team2" open class Project1Data { private object Assets { - val libAar1Asset1 = createRawFileInfo(path = "/assets/asset_resource_1.xml", downloadSize = 10, size = 30) - val moduleAar1Asset1 = createRawFileInfo(path = "/assets/asset_resource_2.xml", downloadSize = 20, size = 60) - val moduleAar2Asset1 = createRawFileInfo(path = "/assets/asset_resource_3.xml", downloadSize = 30, size = 90) + val libAar1Asset1 = createRawFileInfo(path = "/assets/asset_resource_1.xml", downloadSize = 10, rawSize = 30) + val moduleAar1Asset1 = createRawFileInfo(path = "/assets/asset_resource_2.xml", downloadSize = 20, rawSize = 60) + val moduleAar2Asset1 = createRawFileInfo(path = "/assets/asset_resource_3.xml", downloadSize = 30, rawSize = 90) } private object Classes { - val libAar1Class1 = createClassFileInfo(name = "com.grab.test.HelloWorld", downloadSize = 5, size = 20) - val libJar1Class1 = createClassFileInfo(name = "com.grab.test.TestClass2", downloadSize = 7, size = 30) - val moduleAar2Class1 = createClassFileInfo(name = "com.grab.test.TestClass3", downloadSize = 9, size = 40) - val moduleJar1Class1 = createClassFileInfo(name = "com.grab.test.TestClass4", downloadSize = 13, size = 60) - val moduleJar2Class1 = createClassFileInfo(name = "com.grab.test.TestClass5", downloadSize = 18, size = 80) + val libAar1Class1 = createClassFileInfo(name = "com.grab.test.HelloWorld", downloadSize = 5, rawSize = 20) + val libJar1Class1 = createClassFileInfo(name = "com.grab.test.TestClass2", downloadSize = 7, rawSize = 30) + val moduleAar2Class1 = createClassFileInfo(name = "com.grab.test.TestClass3", downloadSize = 9, rawSize = 40) + val moduleJar1Class1 = createClassFileInfo(name = "com.grab.test.TestClass4", downloadSize = 13, rawSize = 60) + val moduleJar2Class1 = createClassFileInfo(name = "com.grab.test.TestClass5", downloadSize = 18, rawSize = 80) } private object NativeLibs { - val libAar2NativeLib1 = createRawFileInfo(path = "/lib/armeabi-v7a/sample.so", downloadSize = 10, size = 20) + val libAar2NativeLib1 = createRawFileInfo(path = "/lib/armeabi-v7a/sample.so", downloadSize = 10, rawSize = 20) val moduleAar1NativeLib1 = - createRawFileInfo(path = "/lib/armeabi-v7a/sample2.so", downloadSize = 50, size = 100) - val moduleAar2NativeLib1 = createRawFileInfo(path = "/lib/armeabi-v7a/sample3.so", downloadSize = 30, size = 90) + createRawFileInfo(path = "/lib/armeabi-v7a/sample2.so", downloadSize = 50, rawSize = 100) + val moduleAar2NativeLib1 = createRawFileInfo(path = "/lib/armeabi-v7a/sample3.so", downloadSize = 30, rawSize = 90) } private object Resources { val libAar2Resource1 = - createRawFileInfo(path = "/res/drawable-xlarge-port-hdpi-v4/ic_test.xml", downloadSize = 30, size = 90) + createRawFileInfo(path = "/res/drawable-xlarge-port-hdpi-v4/ic_test.xml", downloadSize = 30, rawSize = 90) val moduleAar2Resource1 = - createRawFileInfo(path = "/res/animator/test_animator.xml", downloadSize = 20, size = 40) - val moduleAar1Resource1 = createRawFileInfo(path = "/res/font/test_font.xml", downloadSize = 20, size = 40) + createRawFileInfo(path = "/res/animator/test_animator.xml", downloadSize = 20, rawSize = 40) + val moduleAar1Resource1 = createRawFileInfo(path = "/res/font/test_font.xml", downloadSize = 20, rawSize = 40) } private object Others { val moduleJar2Other1 = - createRawFileInfo(path = "play-services-detection.properties", downloadSize = 10, size = 20) - val moduleAar1Other1 = createRawFileInfo(path = "build-data.properties", downloadSize = 10, size = 30) + createRawFileInfo(path = "play-services-detection.properties", downloadSize = 10, rawSize = 20) + val moduleAar1Other1 = createRawFileInfo(path = "build-data.properties", downloadSize = 10, rawSize = 30) } private val dexFile = DexFileInfo( @@ -81,7 +82,8 @@ open class Project1Data { Classes.moduleJar1Class1, Classes.moduleJar2Class1 ), - size = 200 + rawSize = 200, + sizeCalculationMode = SizeCalculationMode.DOWNLOADABLE ) /** diff --git a/app-sizer/src/test/kotlin/com/grab/sizer/analyzer/Utils.kt b/app-sizer/src/test/kotlin/com/grab/sizer/analyzer/Utils.kt index 4200d40..4222d62 100644 --- a/app-sizer/src/test/kotlin/com/grab/sizer/analyzer/Utils.kt +++ b/app-sizer/src/test/kotlin/com/grab/sizer/analyzer/Utils.kt @@ -27,6 +27,7 @@ package com.grab.sizer.analyzer +import com.grab.sizer.SizeCalculationMode import com.grab.sizer.analyzer.mapper.* import com.grab.sizer.analyzer.model.ClassFileInfo import com.grab.sizer.analyzer.model.RawFileInfo @@ -54,15 +55,16 @@ internal class MapperComponent { } -internal fun createRawFileInfo(path: String, downloadSize: Long = 50, size: Long = 150) = - RawFileInfo(path = path, downloadSize = downloadSize, size = size) +internal fun createRawFileInfo(path: String, downloadSize: Long = 50, rawSize: Long = 150) = + RawFileInfo(path = path, downloadSize = downloadSize, rawSize = rawSize, sizeCalculationMode = SizeCalculationMode.DOWNLOADABLE) internal fun createEmptyDexFileInfo(name: String = "dex"): DexFileInfo = DexFileInfo( name = name, downloadSize = 100, - size = 200, + rawSize = 200, classes = emptySet(), + sizeCalculationMode = SizeCalculationMode.DOWNLOADABLE ) -internal fun createClassFileInfo(name: String, downloadSize: Long = 100, size: Long = 300) = - ClassFileInfo(name = name, downloadSize = downloadSize, size = size) \ No newline at end of file +internal fun createClassFileInfo(name: String, downloadSize: Long = 100, rawSize: Long = 300) = + ClassFileInfo(name = name, downloadSize = downloadSize, rawSize = rawSize, sizeCalculationMode = SizeCalculationMode.DOWNLOADABLE) \ No newline at end of file diff --git a/app-sizer/src/test/kotlin/com/grab/sizer/parser/DefaultAarFileParserTest.kt b/app-sizer/src/test/kotlin/com/grab/sizer/parser/DefaultAarFileParserTest.kt index 4f4b3e4..79539b5 100644 --- a/app-sizer/src/test/kotlin/com/grab/sizer/parser/DefaultAarFileParserTest.kt +++ b/app-sizer/src/test/kotlin/com/grab/sizer/parser/DefaultAarFileParserTest.kt @@ -1,5 +1,6 @@ package com.grab.sizer.parser +import com.grab.sizer.SizeCalculationMode import com.grab.sizer.utils.SizerInputFile import org.junit.Assert.* import org.junit.Before @@ -21,7 +22,7 @@ class DefaultAarFileParserTest { @Before fun setup() { mockJarParser = MockJarStreamParser() - aarFileParser = DefaultAarFileParser(mockJarParser) + aarFileParser = DefaultAarFileParser(mockJarParser, SizeCalculationMode.DOWNLOADABLE) } @Test diff --git a/app-sizer/src/test/kotlin/com/grab/sizer/parser/DefaultApkFileParserTest.kt b/app-sizer/src/test/kotlin/com/grab/sizer/parser/DefaultApkFileParserTest.kt index 64171ea..b03f42b 100644 --- a/app-sizer/src/test/kotlin/com/grab/sizer/parser/DefaultApkFileParserTest.kt +++ b/app-sizer/src/test/kotlin/com/grab/sizer/parser/DefaultApkFileParserTest.kt @@ -1,6 +1,7 @@ package com.grab.sizer.parser import com.android.tools.apk.analyzer.ApkSizeCalculator +import com.grab.sizer.SizeCalculationMode import org.junit.Before import org.junit.Rule import org.junit.Test @@ -27,7 +28,7 @@ class DefaultApkFileParserTest { fun setup() { mockDexFileParser = MockDexFileParser() mockApkSizeCalculator = MockApkSizeCalculator() - apkFileParser = DefaultApkFileParser(mockDexFileParser, mockApkSizeCalculator) + apkFileParser = DefaultApkFileParser(mockDexFileParser, mockApkSizeCalculator, SizeCalculationMode.DOWNLOADABLE) } @Test @@ -78,8 +79,8 @@ class DefaultApkFileParserTest { mockApkSizeCalculator.mockApkSizeInfo = createMockApkSizeInfo() mockDexFileParser.setDexFileInfos( listOf( - DexFileInfo("classes.dex", 1000, emptySet(), emptySet(), 500), - DexFileInfo("classes2.dex", 1000, emptySet(), emptySet(), 500) + DexFileInfo("classes.dex", 1000, emptySet(), emptySet(), 500, SizeCalculationMode.DOWNLOADABLE), + DexFileInfo("classes2.dex", 1000, emptySet(), emptySet(), 500, SizeCalculationMode.DOWNLOADABLE) ) ) @@ -117,7 +118,7 @@ class DefaultApkFileParserTest { )) mockApkSizeCalculator.mockApkSizeInfo = createMockApkSizeInfo() mockDexFileParser.setDexFileInfos( - listOf(DexFileInfo("classes.dex", 1000, emptySet(), emptySet(), 500)) + listOf(DexFileInfo("classes.dex", 1000, emptySet(), emptySet(), 500, SizeCalculationMode.DOWNLOADABLE)) ) val result = apkFileParser.parseApks(sequenceOf(apk), ProguardMap()) diff --git a/app-sizer/src/test/kotlin/com/grab/sizer/parser/DefaultJarFileParserTest.kt b/app-sizer/src/test/kotlin/com/grab/sizer/parser/DefaultJarFileParserTest.kt index eb344e3..3f0c308 100644 --- a/app-sizer/src/test/kotlin/com/grab/sizer/parser/DefaultJarFileParserTest.kt +++ b/app-sizer/src/test/kotlin/com/grab/sizer/parser/DefaultJarFileParserTest.kt @@ -1,5 +1,6 @@ package com.grab.sizer.parser +import com.grab.sizer.SizeCalculationMode import com.grab.sizer.utils.SizerInputFile import org.junit.Assert.* import org.junit.Before @@ -18,7 +19,7 @@ class DefaultJarFileParserTest { @Before fun setup() { - jarFileParser = DefaultJarFileParser() + jarFileParser = DefaultJarFileParser(SizeCalculationMode.DOWNLOADABLE) } @Test diff --git a/cli/src/main/kotlin/com/grab/sizer/AnalyzerCommand.kt b/cli/src/main/kotlin/com/grab/sizer/AnalyzerCommand.kt index 56118fa..a2557fc 100644 --- a/cli/src/main/kotlin/com/grab/sizer/AnalyzerCommand.kt +++ b/cli/src/main/kotlin/com/grab/sizer/AnalyzerCommand.kt @@ -53,7 +53,6 @@ class AnalyzerCommand : CliktCommand() { """.trimIndent() ) - private val reportOption by option() .switch( "--libraries" to AnalyticsOption.LIBRARIES, @@ -65,6 +64,12 @@ class AnalyzerCommand : CliktCommand() { "--lib-content" to AnalyticsOption.LIB_CONTENT, ).default(AnalyticsOption.DEFAULT) + private val sizeCalculationMode by option() + .switch( + "--raw-size" to SizeCalculationMode.RAW, + "--downloadable-size" to SizeCalculationMode.DOWNLOADABLE, + ).default(SizeCalculationMode.DOWNLOADABLE) + override fun run() { val config = ConfigYmlLoader().load(settingFile) .also { @@ -83,7 +88,7 @@ class AnalyzerCommand : CliktCommand() { outputProvider = CliOutputProvider(config, apkDirectory.nameWithoutExtension), libName = libName, logger = logger - ).process(reportOption) + ).process(reportOption, sizeCalculationMode) } } diff --git a/sizer-gradle-plugin/src/main/kotlin/com/grab/plugin/sizer/ProjectParams.kt b/sizer-gradle-plugin/src/main/kotlin/com/grab/plugin/sizer/ProjectParams.kt index 5a589a5..243c91c 100644 --- a/sizer-gradle-plugin/src/main/kotlin/com/grab/plugin/sizer/ProjectParams.kt +++ b/sizer-gradle-plugin/src/main/kotlin/com/grab/plugin/sizer/ProjectParams.kt @@ -28,16 +28,19 @@ package com.grab.plugin.sizer import com.grab.sizer.AnalyticsOption +import com.grab.sizer.SizeCalculationMode import org.gradle.api.Project private const val DEVICE_NAME_PARAM = "deviceName" private const val PIPELINE_ID_PARAM = "pipeline" private const val OPTION_PARAM = "option" +private const val SIZE_PARAM = "size_mode" private const val LIBRARY_NAME_PARAM = "library" private const val DEVICE_SPEC_PARAM = "deviceSpec" internal interface ProjectParams { fun option(): AnalyticsOption + fun sizeMode(): SizeCalculationMode fun libraryName(): String? } @@ -45,5 +48,6 @@ internal fun Project.params(): ProjectParams = DefaultProjectParams(this) private class DefaultProjectParams(private val project: Project) : ProjectParams { override fun option(): AnalyticsOption = AnalyticsOption.fromString(project.findProperty(OPTION_PARAM) as String?) + override fun sizeMode(): SizeCalculationMode = SizeCalculationMode.fromString(project.findProperty(SIZE_PARAM) as String?) override fun libraryName(): String? = (project.findProperty(LIBRARY_NAME_PARAM) as String?) } \ No newline at end of file diff --git a/sizer-gradle-plugin/src/main/kotlin/com/grab/plugin/sizer/tasks/AppSizeAnalysisTask.kt b/sizer-gradle-plugin/src/main/kotlin/com/grab/plugin/sizer/tasks/AppSizeAnalysisTask.kt index 351809c..658963e 100644 --- a/sizer-gradle-plugin/src/main/kotlin/com/grab/plugin/sizer/tasks/AppSizeAnalysisTask.kt +++ b/sizer-gradle-plugin/src/main/kotlin/com/grab/plugin/sizer/tasks/AppSizeAnalysisTask.kt @@ -43,6 +43,7 @@ import com.grab.plugin.sizer.utils.PluginOutputProvider import com.grab.sizer.AnalyticsOption import com.grab.sizer.AppSizer import com.grab.sizer.report.ProjectInfo +import com.grab.sizer.SizeCalculationMode import com.grab.sizer.report.db.DatabaseRetentionPolicy import com.grab.sizer.report.db.InfluxDBConfig import org.gradle.api.DefaultTask @@ -75,6 +76,9 @@ internal abstract class AppSizeAnalysisTask : DefaultTask() { @get:Input abstract val option: Property + @get:Input + abstract val sizeCalculationMode: Property + @get:OutputDirectory abstract val outputDirectory: DirectoryProperty @@ -124,7 +128,7 @@ internal abstract class AppSizeAnalysisTask : DefaultTask() { outputProvider = createOutputProvider(projectInfo), libName = libName.orNull, logger = PluginLogger(project), - ).process(option.get()) + ).process(option.get(), sizeCalculationMode.get()) } } @@ -171,6 +175,7 @@ internal abstract class AppSizeAnalysisTask : DefaultTask() { this.archiveDepJsonFile.set(generateArchivesListTask.map { it.archiveDepFile.get() }) this.libName.set(project.params().libraryName()) this.option.set(project.params().option()) + this.sizeCalculationMode.set(project.params().sizeMode()) if (pluginExtension.metrics.influxDBExtension.url.isPresent) { this.influxDBConfig.set(pluginExtension.metrics.influxDBExtension.toInfluxDBConfig()) }