Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app-sizer/src/main/kotlin/com/grab/sizer/AppSizer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@ 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,
private val outputProvider: OutputProvider,
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()
Expand Down
48 changes: 48 additions & 0 deletions app-sizer/src/main/kotlin/com/grab/sizer/SizeCalculationMode.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
}
40 changes: 20 additions & 20 deletions app-sizer/src/main/kotlin/com/grab/sizer/analyzer/ApkAnalyzer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ internal class ApkAnalyzer @Inject constructor(
}

private fun generateReport(apks: Set<ApkFileInfo>, contributors: Set<Contributor>): 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)
Expand Down Expand Up @@ -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<Row> = 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<Row> = 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,
),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ internal class BasicApkAnalyzer @Inject constructor(
}

private fun Set<ApkFileInfo>.createApkReportRows(): List<Row> {
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(
Expand All @@ -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
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ internal class CodebaseAnalyzer @Inject constructor(

private fun Team.toReportRow(): Row = createRow(
name,
getDownloadSize(),
getSize(),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -93,8 +97,8 @@ internal class LargeFileAnalyzer @Inject constructor(
}

private fun Set<Contributor>.filterLargeFileContributors(): Set<Contributor> = 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()
Expand All @@ -110,7 +114,7 @@ internal class LargeFileAnalyzer @Inject constructor(
}

private fun List<Team>.sorByResources(): List<Team> = this.sortedBy {
it.resourcesDownloadSize + it.assetsDownloadSize
it.resourcesSize + it.assetsSize
}

private fun List<Team>.toReportRows(): List<Row> = map { it to it.modules }
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ internal class LibContentAnalyzer @Inject constructor(
createRow(
rowName = it.name,
name = it.name,
value = it.downloadSize,
value = it.size,
tag = type
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,25 +57,25 @@ internal class LibrariesAnalyzer @Inject constructor(
}

private fun generateReport(contributors: Set<Contributor>): 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) },
)
}

private fun Contributor.toReportItem(): ReportItem = ReportItem(
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<Contributor>): List<ReportItem> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ internal class ModuleAnalyzer @Inject constructor(

private fun generateReport(contributors: Set<Contributor>): 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,
Expand All @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading
Loading