Support APK splits in metadata

This commit is contained in:
Torsten Grote 2020-10-05 16:40:47 -03:00 committed by Chirayu Desai
parent 46e8a46c63
commit af2bf4f60a
5 changed files with 38 additions and 0 deletions

View file

@ -70,6 +70,7 @@ data class PackageMetadata(
internal val system: Boolean = false,
internal val version: Long? = null,
internal val installer: String? = null,
internal val splits: List<ApkSplit>? = null,
internal val sha256: String? = null,
internal val signatures: List<String>? = null
) {
@ -78,11 +79,19 @@ data class PackageMetadata(
}
}
data class ApkSplit(
val name: String,
val sha256: String
// There's also a revisionCode, but it doesn't seem to be used just yet
)
internal const val JSON_PACKAGE_TIME = "time"
internal const val JSON_PACKAGE_STATE = "state"
internal const val JSON_PACKAGE_SYSTEM = "system"
internal const val JSON_PACKAGE_VERSION = "version"
internal const val JSON_PACKAGE_INSTALLER = "installer"
internal const val JSON_PACKAGE_SPLITS = "splits"
internal const val JSON_PACKAGE_SPLIT_NAME = "name"
internal const val JSON_PACKAGE_SHA256 = "sha256"
internal const val JSON_PACKAGE_SIGNATURES = "signatures"

View file

@ -103,6 +103,7 @@ class MetadataManager(
system = packageInfo.isSystemApp(),
version = packageMetadata.version,
installer = packageMetadata.installer,
splits = packageMetadata.splits,
sha256 = packageMetadata.sha256,
signatures = packageMetadata.signatures
)

View file

@ -112,6 +112,7 @@ internal class MetadataReaderImpl(private val crypto: Crypto) : MetadataReader {
system = pSystem,
version = if (pVersion == 0L) null else pVersion,
installer = if (pInstaller == "") null else pInstaller,
splits = getSplits(p),
sha256 = if (pSha256 == "") null else pSha256,
signatures = signatures
)
@ -130,4 +131,18 @@ internal class MetadataReaderImpl(private val crypto: Crypto) : MetadataReader {
}
}
private fun getSplits(p: JSONObject): List<ApkSplit>? {
val jsonSplits = p.optJSONArray(JSON_PACKAGE_SPLITS) ?: return null
val splits = ArrayList<ApkSplit>(jsonSplits.length())
for (i in 0 until jsonSplits.length()) {
val jsonApkSplit = jsonSplits.getJSONObject(i)
val apkSplit = ApkSplit(
name = jsonApkSplit.getString(JSON_PACKAGE_SPLIT_NAME),
sha256 = jsonApkSplit.getString(JSON_PACKAGE_SHA256)
)
splits.add(apkSplit)
}
return splits
}
}

View file

@ -45,6 +45,14 @@ internal class MetadataWriterImpl(private val crypto: Crypto) : MetadataWriter {
}
packageMetadata.version?.let { put(JSON_PACKAGE_VERSION, it) }
packageMetadata.installer?.let { put(JSON_PACKAGE_INSTALLER, it) }
packageMetadata.splits?.let { splits ->
put(JSON_PACKAGE_SPLITS, JSONArray().apply {
for (split in splits) put(JSONObject().apply {
put(JSON_PACKAGE_SPLIT_NAME, split.name)
put(JSON_PACKAGE_SHA256, split.sha256)
})
})
}
packageMetadata.sha256?.let { put(JSON_PACKAGE_SHA256, it) }
packageMetadata.signatures?.let { put(JSON_PACKAGE_SIGNATURES, JSONArray(it)) }
})

View file

@ -54,6 +54,11 @@ internal class MetadataWriterDecoderTest {
state = APK_AND_DATA,
version = Random.nextLong(),
installer = getRandomString(),
splits = listOf(
ApkSplit(getRandomString(), getRandomString()),
ApkSplit(getRandomString(), getRandomString()),
ApkSplit(getRandomString(), getRandomString())
),
sha256 = getRandomString(),
signatures = listOf(getRandomString(), getRandomString())
)