1 /* 2 * Copyright 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.build.testConfiguration 18 19 import com.google.common.hash.Hashing 20 import com.google.common.io.BaseEncoding 21 import com.google.gson.GsonBuilder 22 23 /** 24 * List of App APKs required to install the app. 25 * 26 * APKs must be installed group by group in same order as listed. 27 */ 28 data class AppApksModel(val apkGroups: List<ApkFileGroup>) { 29 30 @Suppress("UnstableApiUsage") // guava Hashing is marked as @Beta sha256null31 fun sha256(): String? { 32 val shaHashes = apkGroups.flatMap(ApkFileGroup::apks).map(ApkFile::sha256) 33 if (shaHashes.isEmpty()) { 34 return null 35 } 36 37 if (shaHashes.size == 1) { 38 return shaHashes[0] 39 } 40 41 val hasher = Hashing.sha256().newHasher() 42 shaHashes.forEach { hasher.putString(it, Charsets.UTF_8) } 43 return BaseEncoding.base16().lowerCase().encode(hasher.hash().asBytes()) 44 } 45 toJsonnull46 fun toJson(): String = GsonBuilder().setPrettyPrinting().create().toJson(this) 47 48 companion object { 49 fun fromJson(json: String): AppApksModel = 50 GsonBuilder().create().fromJson(json, AppApksModel::class.java) 51 } 52 } 53 54 /** Group of APKs / Splits that needs to be installed together. */ 55 data class ApkFileGroup(val apks: List<ApkFile>) { isUsingApkSplitsnull56 fun isUsingApkSplits(): Boolean = apks.size > 1 57 } 58 59 /** Single APK / Split file */ 60 data class ApkFile(val name: String, val sha256: String = "") 61 62 internal fun singleFileAppApksModel(name: String, sha256: String = ""): AppApksModel = 63 AppApksModel(apkGroups = listOf(singleFileApkFileGroup(name, sha256))) 64 65 internal fun singleFileApkFileGroup(name: String, sha256: String = ""): ApkFileGroup = 66 ApkFileGroup(apks = listOf(ApkFile(name, sha256))) 67