All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.tinkoff.plugins.buildmetrics.android.utils.BundleTool.kt Maven / Gradle / Ivy

package ru.tinkoff.plugins.buildmetrics.android.utils

import java.io.BufferedReader
import java.io.Serializable

/**
 * [bundletool](https://developer.android.com/studio/command-line/bundletool)
 */
interface BundleTool : Serializable {

    /**
     * Generate an APK set for all device configurations app supports from app bundle.
     *
     * @return exit value for the process. By convention, the value 0 indicates normal termination.
     */
    fun buildApks(bundlePath: String, outputPath: String): Int

    /**
     * @return estimated download sizes of APKs in an APK set as they would be served compressed over-the-wire.
     */
    fun getSize(apksPath: String): Map
}

internal class BundleToolImpl : BundleTool {

    override fun buildApks(bundlePath: String, outputPath: String): Int {
        val process = execCommandAndWait("bundletool build-apks --bundle=$bundlePath --output=$outputPath")
        return process.exitValue()
    }

    override fun getSize(apksPath: String): Map {
        val process = execCommandAndWait("bundletool get-size total --apks=$apksPath")
        return process.inputStream.bufferedReader()
            .use(BufferedReader::readText)
            .trim()
            .split('\n').let { lines ->
                // exec output format:
                // MIN,MAX
                // 123,456
                val keys = lines[0].split(',')
                val values = lines[1].split(',')
                check(keys.size == values.size) {
                    "Failed to estimate download sizes," +
                            "because 'keys.size != values.size (${keys.size} != ${values.size})'."
                }
                keys.mapIndexed { index, key ->
                    key to values[index].toLong()
                }.toMap()
            }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy