Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 1 | // Copyright 2020 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // Copies all the entries (APKs/APEXes) matching the target configuration from the given |
| 16 | // APK set into a zip file. Run it without arguments to see usage details. |
| 17 | package main |
| 18 | |
| 19 | import ( |
| 20 | "flag" |
| 21 | "fmt" |
| 22 | "io" |
| 23 | "log" |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 24 | "math" |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 25 | "os" |
| 26 | "regexp" |
Jaewoong Jung | 11c1e0f | 2020-06-29 19:18:44 -0700 | [diff] [blame] | 27 | "sort" |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 28 | "strings" |
| 29 | |
Dan Willemsen | 4591b64 | 2021-05-24 14:24:12 -0700 | [diff] [blame] | 30 | "google.golang.org/protobuf/proto" |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 31 | |
Sam Delmerico | b74f0a0 | 2022-09-16 11:59:56 -0400 | [diff] [blame] | 32 | "android/soong/cmd/extract_apks/bundle_proto" |
Dan Willemsen | 4591b64 | 2021-05-24 14:24:12 -0700 | [diff] [blame] | 33 | android_bundle_proto "android/soong/cmd/extract_apks/bundle_proto" |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 34 | "android/soong/third_party/zip" |
| 35 | ) |
| 36 | |
| 37 | type TargetConfig struct { |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 38 | sdkVersion int32 |
| 39 | screenDpi map[android_bundle_proto.ScreenDensity_DensityAlias]bool |
| 40 | // Map holding <ABI alias>:<its sequence number in the flag> info. |
| 41 | abis map[android_bundle_proto.Abi_AbiAlias]int |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 42 | allowPrereleased bool |
| 43 | stem string |
Pranav Gupta | 51645ff | 2023-03-20 16:19:53 -0700 | [diff] [blame] | 44 | skipSdkCheck bool |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // An APK set is a zip archive. An entry 'toc.pb' describes its contents. |
| 48 | // It is a protobuf message BuildApkResult. |
| 49 | type Toc *android_bundle_proto.BuildApksResult |
| 50 | |
| 51 | type ApkSet struct { |
| 52 | path string |
| 53 | reader *zip.ReadCloser |
| 54 | entries map[string]*zip.File |
| 55 | } |
| 56 | |
| 57 | func newApkSet(path string) (*ApkSet, error) { |
| 58 | apkSet := &ApkSet{path: path, entries: make(map[string]*zip.File)} |
| 59 | var err error |
| 60 | if apkSet.reader, err = zip.OpenReader(apkSet.path); err != nil { |
| 61 | return nil, err |
| 62 | } |
| 63 | for _, f := range apkSet.reader.File { |
| 64 | apkSet.entries[f.Name] = f |
| 65 | } |
| 66 | return apkSet, nil |
| 67 | } |
| 68 | |
| 69 | func (apkSet *ApkSet) getToc() (Toc, error) { |
| 70 | var err error |
| 71 | tocFile, ok := apkSet.entries["toc.pb"] |
| 72 | if !ok { |
| 73 | return nil, fmt.Errorf("%s: APK set should have toc.pb entry", apkSet.path) |
| 74 | } |
| 75 | rc, err := tocFile.Open() |
| 76 | if err != nil { |
| 77 | return nil, err |
| 78 | } |
Ray Chin | 0523ee8 | 2023-09-21 20:32:06 +0800 | [diff] [blame] | 79 | bytes, err := io.ReadAll(rc) |
| 80 | if err != nil { |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 81 | return nil, err |
| 82 | } |
| 83 | rc.Close() |
| 84 | buildApksResult := new(android_bundle_proto.BuildApksResult) |
| 85 | if err = proto.Unmarshal(bytes, buildApksResult); err != nil { |
| 86 | return nil, err |
| 87 | } |
| 88 | return buildApksResult, nil |
| 89 | } |
| 90 | |
| 91 | func (apkSet *ApkSet) close() { |
| 92 | apkSet.reader.Close() |
| 93 | } |
| 94 | |
| 95 | // Matchers for selection criteria |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 96 | |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 97 | type abiTargetingMatcher struct { |
| 98 | *android_bundle_proto.AbiTargeting |
| 99 | } |
| 100 | |
| 101 | func (m abiTargetingMatcher) matches(config TargetConfig) bool { |
| 102 | if m.AbiTargeting == nil { |
| 103 | return true |
| 104 | } |
| 105 | if _, ok := config.abis[android_bundle_proto.Abi_UNSPECIFIED_CPU_ARCHITECTURE]; ok { |
| 106 | return true |
| 107 | } |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 108 | // Find the one that appears first in the abis flags. |
| 109 | abiIdx := math.MaxInt32 |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 110 | for _, v := range m.GetValue() { |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 111 | if i, ok := config.abis[v.Alias]; ok { |
| 112 | if i < abiIdx { |
| 113 | abiIdx = i |
| 114 | } |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 117 | if abiIdx == math.MaxInt32 { |
| 118 | return false |
| 119 | } |
| 120 | // See if any alternatives appear before the above one. |
| 121 | for _, a := range m.GetAlternatives() { |
| 122 | if i, ok := config.abis[a.Alias]; ok { |
| 123 | if i < abiIdx { |
| 124 | // There is a better alternative. Skip this one. |
| 125 | return false |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | return true |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | type apkDescriptionMatcher struct { |
| 133 | *android_bundle_proto.ApkDescription |
| 134 | } |
| 135 | |
Sam Delmerico | b48d57b | 2022-11-22 17:47:59 -0500 | [diff] [blame] | 136 | func (m apkDescriptionMatcher) matches(config TargetConfig, allAbisMustMatch bool) bool { |
| 137 | return m.ApkDescription == nil || (apkTargetingMatcher{m.Targeting}).matches(config, allAbisMustMatch) |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | type apkTargetingMatcher struct { |
| 141 | *android_bundle_proto.ApkTargeting |
| 142 | } |
| 143 | |
Sam Delmerico | b48d57b | 2022-11-22 17:47:59 -0500 | [diff] [blame] | 144 | func (m apkTargetingMatcher) matches(config TargetConfig, allAbisMustMatch bool) bool { |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 145 | return m.ApkTargeting == nil || |
| 146 | (abiTargetingMatcher{m.AbiTargeting}.matches(config) && |
| 147 | languageTargetingMatcher{m.LanguageTargeting}.matches(config) && |
| 148 | screenDensityTargetingMatcher{m.ScreenDensityTargeting}.matches(config) && |
| 149 | sdkVersionTargetingMatcher{m.SdkVersionTargeting}.matches(config) && |
Sam Delmerico | b48d57b | 2022-11-22 17:47:59 -0500 | [diff] [blame] | 150 | multiAbiTargetingMatcher{m.MultiAbiTargeting}.matches(config, allAbisMustMatch)) |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | type languageTargetingMatcher struct { |
| 154 | *android_bundle_proto.LanguageTargeting |
| 155 | } |
| 156 | |
| 157 | func (m languageTargetingMatcher) matches(_ TargetConfig) bool { |
| 158 | if m.LanguageTargeting == nil { |
| 159 | return true |
| 160 | } |
| 161 | log.Fatal("language based entry selection is not implemented") |
| 162 | return false |
| 163 | } |
| 164 | |
| 165 | type moduleMetadataMatcher struct { |
| 166 | *android_bundle_proto.ModuleMetadata |
| 167 | } |
| 168 | |
| 169 | func (m moduleMetadataMatcher) matches(config TargetConfig) bool { |
| 170 | return m.ModuleMetadata == nil || |
| 171 | (m.GetDeliveryType() == android_bundle_proto.DeliveryType_INSTALL_TIME && |
| 172 | moduleTargetingMatcher{m.Targeting}.matches(config) && |
| 173 | !m.IsInstant) |
| 174 | } |
| 175 | |
| 176 | type moduleTargetingMatcher struct { |
| 177 | *android_bundle_proto.ModuleTargeting |
| 178 | } |
| 179 | |
| 180 | func (m moduleTargetingMatcher) matches(config TargetConfig) bool { |
| 181 | return m.ModuleTargeting == nil || |
| 182 | (sdkVersionTargetingMatcher{m.SdkVersionTargeting}.matches(config) && |
| 183 | userCountriesTargetingMatcher{m.UserCountriesTargeting}.matches(config)) |
| 184 | } |
| 185 | |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 186 | // A higher number means a higher priority. |
| 187 | // This order must be kept identical to bundletool's. |
| 188 | var multiAbiPriorities = map[android_bundle_proto.Abi_AbiAlias]int{ |
| 189 | android_bundle_proto.Abi_ARMEABI: 1, |
| 190 | android_bundle_proto.Abi_ARMEABI_V7A: 2, |
| 191 | android_bundle_proto.Abi_ARM64_V8A: 3, |
| 192 | android_bundle_proto.Abi_X86: 4, |
| 193 | android_bundle_proto.Abi_X86_64: 5, |
| 194 | android_bundle_proto.Abi_MIPS: 6, |
| 195 | android_bundle_proto.Abi_MIPS64: 7, |
| 196 | } |
| 197 | |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 198 | type multiAbiTargetingMatcher struct { |
| 199 | *android_bundle_proto.MultiAbiTargeting |
| 200 | } |
| 201 | |
Sam Delmerico | b74f0a0 | 2022-09-16 11:59:56 -0400 | [diff] [blame] | 202 | type multiAbiValue []*bundle_proto.Abi |
| 203 | |
| 204 | func (m multiAbiValue) compare(other multiAbiValue) int { |
| 205 | min := func(a, b int) int { |
| 206 | if a < b { |
| 207 | return a |
| 208 | } |
| 209 | return b |
| 210 | } |
| 211 | |
| 212 | sortAbis := func(abiSlice multiAbiValue) func(i, j int) bool { |
| 213 | return func(i, j int) bool { |
| 214 | // sort priorities greatest to least |
| 215 | return multiAbiPriorities[abiSlice[i].Alias] > multiAbiPriorities[abiSlice[j].Alias] |
| 216 | } |
| 217 | } |
| 218 | |
Sam Delmerico | b48d57b | 2022-11-22 17:47:59 -0500 | [diff] [blame] | 219 | sortedM := append(multiAbiValue{}, m...) |
| 220 | sort.Slice(sortedM, sortAbis(sortedM)) |
| 221 | sortedOther := append(multiAbiValue{}, other...) |
| 222 | sort.Slice(sortedOther, sortAbis(sortedOther)) |
Sam Delmerico | b74f0a0 | 2022-09-16 11:59:56 -0400 | [diff] [blame] | 223 | |
Sam Delmerico | b48d57b | 2022-11-22 17:47:59 -0500 | [diff] [blame] | 224 | for i := 0; i < min(len(sortedM), len(sortedOther)); i++ { |
| 225 | if multiAbiPriorities[sortedM[i].Alias] > multiAbiPriorities[sortedOther[i].Alias] { |
Sam Delmerico | b74f0a0 | 2022-09-16 11:59:56 -0400 | [diff] [blame] | 226 | return 1 |
| 227 | } |
Sam Delmerico | b48d57b | 2022-11-22 17:47:59 -0500 | [diff] [blame] | 228 | if multiAbiPriorities[sortedM[i].Alias] < multiAbiPriorities[sortedOther[i].Alias] { |
Sam Delmerico | b74f0a0 | 2022-09-16 11:59:56 -0400 | [diff] [blame] | 229 | return -1 |
| 230 | } |
| 231 | } |
| 232 | |
Sam Delmerico | b48d57b | 2022-11-22 17:47:59 -0500 | [diff] [blame] | 233 | return len(sortedM) - len(sortedOther) |
Sam Delmerico | b74f0a0 | 2022-09-16 11:59:56 -0400 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // this logic should match the logic in bundletool at |
| 237 | // https://github.com/google/bundletool/blob/ae0fc0162fd80d92ef8f4ef4527c066f0106942f/src/main/java/com/android/tools/build/bundletool/device/MultiAbiMatcher.java#L43 |
| 238 | // (note link is the commit at time of writing; but logic should always match the latest) |
Sam Delmerico | b48d57b | 2022-11-22 17:47:59 -0500 | [diff] [blame] | 239 | func (t multiAbiTargetingMatcher) matches(config TargetConfig, allAbisMustMatch bool) bool { |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 240 | if t.MultiAbiTargeting == nil { |
| 241 | return true |
| 242 | } |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 243 | if _, ok := config.abis[android_bundle_proto.Abi_UNSPECIFIED_CPU_ARCHITECTURE]; ok { |
| 244 | return true |
| 245 | } |
Sam Delmerico | b74f0a0 | 2022-09-16 11:59:56 -0400 | [diff] [blame] | 246 | |
| 247 | multiAbiIsValid := func(m multiAbiValue) bool { |
Sam Delmerico | b48d57b | 2022-11-22 17:47:59 -0500 | [diff] [blame] | 248 | numValid := 0 |
Sam Delmerico | b74f0a0 | 2022-09-16 11:59:56 -0400 | [diff] [blame] | 249 | for _, abi := range m { |
Sam Delmerico | b48d57b | 2022-11-22 17:47:59 -0500 | [diff] [blame] | 250 | if _, ok := config.abis[abi.Alias]; ok { |
| 251 | numValid += 1 |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
Sam Delmerico | b48d57b | 2022-11-22 17:47:59 -0500 | [diff] [blame] | 254 | if numValid == 0 { |
| 255 | return false |
| 256 | } else if numValid > 0 && !allAbisMustMatch { |
| 257 | return true |
| 258 | } else { |
| 259 | return numValid == len(m) |
| 260 | } |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 261 | } |
Sam Delmerico | b74f0a0 | 2022-09-16 11:59:56 -0400 | [diff] [blame] | 262 | |
| 263 | // ensure that the current value is valid for our config |
| 264 | valueSetContainsViableAbi := false |
| 265 | multiAbiSet := t.GetValue() |
| 266 | for _, multiAbi := range multiAbiSet { |
| 267 | if multiAbiIsValid(multiAbi.GetAbi()) { |
| 268 | valueSetContainsViableAbi = true |
Sam Delmerico | b48d57b | 2022-11-22 17:47:59 -0500 | [diff] [blame] | 269 | break |
Sam Delmerico | b74f0a0 | 2022-09-16 11:59:56 -0400 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
| 273 | if !valueSetContainsViableAbi { |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 274 | return false |
| 275 | } |
Sam Delmerico | b74f0a0 | 2022-09-16 11:59:56 -0400 | [diff] [blame] | 276 | |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 277 | // See if there are any matching alternatives with a higher priority. |
Sam Delmerico | b74f0a0 | 2022-09-16 11:59:56 -0400 | [diff] [blame] | 278 | for _, altMultiAbi := range t.GetAlternatives() { |
| 279 | if !multiAbiIsValid(altMultiAbi.GetAbi()) { |
| 280 | continue |
| 281 | } |
| 282 | |
| 283 | for _, multiAbi := range multiAbiSet { |
| 284 | valueAbis := multiAbiValue(multiAbi.GetAbi()) |
| 285 | altAbis := multiAbiValue(altMultiAbi.GetAbi()) |
| 286 | if valueAbis.compare(altAbis) < 0 { |
| 287 | // An alternative has a higher priority, don't use this one |
| 288 | return false |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | } |
Sam Delmerico | b74f0a0 | 2022-09-16 11:59:56 -0400 | [diff] [blame] | 292 | |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 293 | return true |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | type screenDensityTargetingMatcher struct { |
| 297 | *android_bundle_proto.ScreenDensityTargeting |
| 298 | } |
| 299 | |
| 300 | func (m screenDensityTargetingMatcher) matches(config TargetConfig) bool { |
| 301 | if m.ScreenDensityTargeting == nil { |
| 302 | return true |
| 303 | } |
| 304 | if _, ok := config.screenDpi[android_bundle_proto.ScreenDensity_DENSITY_UNSPECIFIED]; ok { |
| 305 | return true |
| 306 | } |
| 307 | for _, v := range m.GetValue() { |
| 308 | switch x := v.GetDensityOneof().(type) { |
| 309 | case *android_bundle_proto.ScreenDensity_DensityAlias_: |
| 310 | if _, ok := config.screenDpi[x.DensityAlias]; ok { |
| 311 | return true |
| 312 | } |
| 313 | default: |
| 314 | log.Fatal("For screen density, only DPI name based entry selection (e.g. HDPI, XHDPI) is implemented") |
| 315 | } |
| 316 | } |
| 317 | return false |
| 318 | } |
| 319 | |
| 320 | type sdkVersionTargetingMatcher struct { |
| 321 | *android_bundle_proto.SdkVersionTargeting |
| 322 | } |
| 323 | |
| 324 | func (m sdkVersionTargetingMatcher) matches(config TargetConfig) bool { |
| 325 | const preReleaseVersion = 10000 |
Pranav Gupta | 51645ff | 2023-03-20 16:19:53 -0700 | [diff] [blame] | 326 | // TODO (b274518686) This check should only be used while SHA based targeting is active |
| 327 | // Once we have switched to an SDK version, this can be changed to throw an error if |
| 328 | // it was accidentally set |
| 329 | if config.skipSdkCheck == true { |
| 330 | return true |
| 331 | } |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 332 | if m.SdkVersionTargeting == nil { |
| 333 | return true |
| 334 | } |
| 335 | if len(m.Value) > 1 { |
| 336 | log.Fatal(fmt.Sprintf("sdk_version_targeting should not have multiple values:%#v", m.Value)) |
| 337 | } |
| 338 | // Inspect only sdkVersionTargeting.Value. |
| 339 | // Even though one of the SdkVersionTargeting.Alternatives values may be |
| 340 | // better matching, we will select all of them |
| 341 | return m.Value[0].Min == nil || |
| 342 | m.Value[0].Min.Value <= config.sdkVersion || |
| 343 | (config.allowPrereleased && m.Value[0].Min.Value == preReleaseVersion) |
| 344 | } |
| 345 | |
| 346 | type textureCompressionFormatTargetingMatcher struct { |
| 347 | *android_bundle_proto.TextureCompressionFormatTargeting |
| 348 | } |
| 349 | |
| 350 | func (m textureCompressionFormatTargetingMatcher) matches(_ TargetConfig) bool { |
| 351 | if m.TextureCompressionFormatTargeting == nil { |
| 352 | return true |
| 353 | } |
| 354 | log.Fatal("texture based entry selection is not implemented") |
| 355 | return false |
| 356 | } |
| 357 | |
| 358 | type userCountriesTargetingMatcher struct { |
| 359 | *android_bundle_proto.UserCountriesTargeting |
| 360 | } |
| 361 | |
| 362 | func (m userCountriesTargetingMatcher) matches(_ TargetConfig) bool { |
| 363 | if m.UserCountriesTargeting == nil { |
| 364 | return true |
| 365 | } |
| 366 | log.Fatal("country based entry selection is not implemented") |
| 367 | return false |
| 368 | } |
| 369 | |
| 370 | type variantTargetingMatcher struct { |
| 371 | *android_bundle_proto.VariantTargeting |
| 372 | } |
| 373 | |
Sam Delmerico | b48d57b | 2022-11-22 17:47:59 -0500 | [diff] [blame] | 374 | func (m variantTargetingMatcher) matches(config TargetConfig, allAbisMustMatch bool) bool { |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 375 | if m.VariantTargeting == nil { |
| 376 | return true |
| 377 | } |
| 378 | return sdkVersionTargetingMatcher{m.SdkVersionTargeting}.matches(config) && |
| 379 | abiTargetingMatcher{m.AbiTargeting}.matches(config) && |
Sam Delmerico | b48d57b | 2022-11-22 17:47:59 -0500 | [diff] [blame] | 380 | multiAbiTargetingMatcher{m.MultiAbiTargeting}.matches(config, allAbisMustMatch) && |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 381 | screenDensityTargetingMatcher{m.ScreenDensityTargeting}.matches(config) && |
| 382 | textureCompressionFormatTargetingMatcher{m.TextureCompressionFormatTargeting}.matches(config) |
| 383 | } |
| 384 | |
| 385 | type SelectionResult struct { |
| 386 | moduleName string |
| 387 | entries []string |
| 388 | } |
| 389 | |
| 390 | // Return all entries matching target configuration |
| 391 | func selectApks(toc Toc, targetConfig TargetConfig) SelectionResult { |
Sam Delmerico | b48d57b | 2022-11-22 17:47:59 -0500 | [diff] [blame] | 392 | checkMatching := func(allAbisMustMatch bool) SelectionResult { |
| 393 | var result SelectionResult |
| 394 | for _, variant := range (*toc).GetVariant() { |
| 395 | if !(variantTargetingMatcher{variant.GetTargeting()}.matches(targetConfig, allAbisMustMatch)) { |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 396 | continue |
| 397 | } |
Sam Delmerico | b48d57b | 2022-11-22 17:47:59 -0500 | [diff] [blame] | 398 | for _, as := range variant.GetApkSet() { |
| 399 | if !(moduleMetadataMatcher{as.ModuleMetadata}.matches(targetConfig)) { |
| 400 | continue |
| 401 | } |
| 402 | for _, apkdesc := range as.GetApkDescription() { |
| 403 | if (apkDescriptionMatcher{apkdesc}).matches(targetConfig, allAbisMustMatch) { |
| 404 | result.entries = append(result.entries, apkdesc.GetPath()) |
| 405 | // TODO(asmundak): As it turns out, moduleName which we get from |
| 406 | // the ModuleMetadata matches the module names of the generated |
| 407 | // entry paths just by coincidence, only for the split APKs. We |
| 408 | // need to discuss this with bundletool folks. |
| 409 | result.moduleName = as.GetModuleMetadata().GetName() |
| 410 | } |
| 411 | } |
| 412 | // we allow only a single module, so bail out here if we found one |
| 413 | if result.moduleName != "" { |
| 414 | return result |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 415 | } |
| 416 | } |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 417 | } |
Sam Delmerico | b48d57b | 2022-11-22 17:47:59 -0500 | [diff] [blame] | 418 | return result |
| 419 | } |
| 420 | result := checkMatching(true) |
| 421 | if result.moduleName == "" { |
| 422 | // if there are no matches where all of the ABIs are available in the |
| 423 | // TargetConfig, then search again with a looser requirement of at |
| 424 | // least one matching ABI |
| 425 | // NOTE(b/260130686): this logic diverges from the logic in bundletool |
| 426 | // https://github.com/google/bundletool/blob/ae0fc0162fd80d92ef8f4ef4527c066f0106942f/src/main/java/com/android/tools/build/bundletool/device/MultiAbiMatcher.java#L43 |
| 427 | result = checkMatching(false) |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 428 | } |
| 429 | return result |
| 430 | } |
| 431 | |
| 432 | type Zip2ZipWriter interface { |
| 433 | CopyFrom(file *zip.File, name string) error |
| 434 | } |
| 435 | |
| 436 | // Writes out selected entries, renaming them as needed |
| 437 | func (apkSet *ApkSet) writeApks(selected SelectionResult, config TargetConfig, |
Colin Cross | ffbcd1d | 2021-11-12 12:19:42 -0800 | [diff] [blame] | 438 | outFile io.Writer, zipWriter Zip2ZipWriter, partition string) ([]string, error) { |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 439 | // Renaming rules: |
| 440 | // splits/MODULE-master.apk to STEM.apk |
| 441 | // else |
| 442 | // splits/MODULE-*.apk to STEM>-$1.apk |
| 443 | // TODO(asmundak): |
| 444 | // add more rules, for .apex files |
| 445 | renameRules := []struct { |
| 446 | rex *regexp.Regexp |
| 447 | repl string |
| 448 | }{ |
| 449 | { |
| 450 | regexp.MustCompile(`^.*/` + selected.moduleName + `-master\.apk$`), |
| 451 | config.stem + `.apk`, |
| 452 | }, |
| 453 | { |
| 454 | regexp.MustCompile(`^.*/` + selected.moduleName + `(-.*\.apk)$`), |
| 455 | config.stem + `$1`, |
| 456 | }, |
Sasha Smundak | 827c55f | 2020-05-20 13:10:59 -0700 | [diff] [blame] | 457 | { |
| 458 | regexp.MustCompile(`^universal\.apk$`), |
| 459 | config.stem + ".apk", |
| 460 | }, |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 461 | } |
| 462 | renamer := func(path string) (string, bool) { |
| 463 | for _, rr := range renameRules { |
| 464 | if rr.rex.MatchString(path) { |
| 465 | return rr.rex.ReplaceAllString(path, rr.repl), true |
| 466 | } |
| 467 | } |
| 468 | return "", false |
| 469 | } |
| 470 | |
| 471 | entryOrigin := make(map[string]string) // output entry to input entry |
Jaewoong Jung | 11c1e0f | 2020-06-29 19:18:44 -0700 | [diff] [blame] | 472 | var apkcerts []string |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 473 | for _, apk := range selected.entries { |
| 474 | apkFile, ok := apkSet.entries[apk] |
| 475 | if !ok { |
Jaewoong Jung | 11c1e0f | 2020-06-29 19:18:44 -0700 | [diff] [blame] | 476 | return nil, fmt.Errorf("TOC refers to an entry %s which does not exist", apk) |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 477 | } |
| 478 | inName := apkFile.Name |
| 479 | outName, ok := renamer(inName) |
| 480 | if !ok { |
| 481 | log.Fatalf("selected an entry with unexpected name %s", inName) |
| 482 | } |
| 483 | if origin, ok := entryOrigin[inName]; ok { |
| 484 | log.Fatalf("selected entries %s and %s will have the same output name %s", |
| 485 | origin, inName, outName) |
| 486 | } |
| 487 | entryOrigin[outName] = inName |
Colin Cross | ffbcd1d | 2021-11-12 12:19:42 -0800 | [diff] [blame] | 488 | if outName == config.stem+".apk" { |
| 489 | if err := writeZipEntryToFile(outFile, apkFile); err != nil { |
| 490 | return nil, err |
| 491 | } |
| 492 | } else { |
| 493 | if err := zipWriter.CopyFrom(apkFile, outName); err != nil { |
| 494 | return nil, err |
| 495 | } |
Jaewoong Jung | 11c1e0f | 2020-06-29 19:18:44 -0700 | [diff] [blame] | 496 | } |
| 497 | if partition != "" { |
| 498 | apkcerts = append(apkcerts, fmt.Sprintf( |
| 499 | `name="%s" certificate="PRESIGNED" private_key="" partition="%s"`, outName, partition)) |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 500 | } |
| 501 | } |
Jaewoong Jung | 11c1e0f | 2020-06-29 19:18:44 -0700 | [diff] [blame] | 502 | sort.Strings(apkcerts) |
| 503 | return apkcerts, nil |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 506 | func (apkSet *ApkSet) extractAndCopySingle(selected SelectionResult, outFile *os.File) error { |
| 507 | if len(selected.entries) != 1 { |
| 508 | return fmt.Errorf("Too many matching entries for extract-single:\n%v", selected.entries) |
| 509 | } |
| 510 | apk, ok := apkSet.entries[selected.entries[0]] |
| 511 | if !ok { |
| 512 | return fmt.Errorf("Couldn't find apk path %s", selected.entries[0]) |
| 513 | } |
Colin Cross | ffbcd1d | 2021-11-12 12:19:42 -0800 | [diff] [blame] | 514 | return writeZipEntryToFile(outFile, apk) |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 517 | // Arguments parsing |
| 518 | var ( |
Colin Cross | ffbcd1d | 2021-11-12 12:19:42 -0800 | [diff] [blame] | 519 | outputFile = flag.String("o", "", "output file for primary entry") |
| 520 | zipFile = flag.String("zip", "", "output file containing additional extracted entries") |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 521 | targetConfig = TargetConfig{ |
| 522 | screenDpi: map[android_bundle_proto.ScreenDensity_DensityAlias]bool{}, |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 523 | abis: map[android_bundle_proto.Abi_AbiAlias]int{}, |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 524 | } |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 525 | extractSingle = flag.Bool("extract-single", false, |
| 526 | "extract a single target and output it uncompressed. only available for standalone apks and apexes.") |
Jaewoong Jung | 11c1e0f | 2020-06-29 19:18:44 -0700 | [diff] [blame] | 527 | apkcertsOutput = flag.String("apkcerts", "", |
| 528 | "optional apkcerts.txt output file containing signing info of all outputted apks") |
| 529 | partition = flag.String("partition", "", "partition string. required when -apkcerts is used.") |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 530 | ) |
| 531 | |
| 532 | // Parse abi values |
| 533 | type abiFlagValue struct { |
| 534 | targetConfig *TargetConfig |
| 535 | } |
| 536 | |
| 537 | func (a abiFlagValue) String() string { |
| 538 | return "all" |
| 539 | } |
| 540 | |
| 541 | func (a abiFlagValue) Set(abiList string) error { |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 542 | for i, abi := range strings.Split(abiList, ",") { |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 543 | v, ok := android_bundle_proto.Abi_AbiAlias_value[abi] |
| 544 | if !ok { |
| 545 | return fmt.Errorf("bad ABI value: %q", abi) |
| 546 | } |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 547 | targetConfig.abis[android_bundle_proto.Abi_AbiAlias(v)] = i |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 548 | } |
| 549 | return nil |
| 550 | } |
| 551 | |
| 552 | // Parse screen density values |
| 553 | type screenDensityFlagValue struct { |
| 554 | targetConfig *TargetConfig |
| 555 | } |
| 556 | |
| 557 | func (s screenDensityFlagValue) String() string { |
| 558 | return "none" |
| 559 | } |
| 560 | |
| 561 | func (s screenDensityFlagValue) Set(densityList string) error { |
| 562 | if densityList == "none" { |
| 563 | return nil |
| 564 | } |
| 565 | if densityList == "all" { |
| 566 | targetConfig.screenDpi[android_bundle_proto.ScreenDensity_DENSITY_UNSPECIFIED] = true |
| 567 | return nil |
| 568 | } |
| 569 | for _, density := range strings.Split(densityList, ",") { |
| 570 | v, found := android_bundle_proto.ScreenDensity_DensityAlias_value[density] |
| 571 | if !found { |
| 572 | return fmt.Errorf("bad screen density value: %q", density) |
| 573 | } |
| 574 | targetConfig.screenDpi[android_bundle_proto.ScreenDensity_DensityAlias(v)] = true |
| 575 | } |
| 576 | return nil |
| 577 | } |
| 578 | |
| 579 | func processArgs() { |
| 580 | flag.Usage = func() { |
Colin Cross | ffbcd1d | 2021-11-12 12:19:42 -0800 | [diff] [blame] | 581 | fmt.Fprintln(os.Stderr, `usage: extract_apks -o <output-file> [-zip <output-zip-file>] `+ |
Pranav Gupta | 51645ff | 2023-03-20 16:19:53 -0700 | [diff] [blame] | 582 | `-sdk-version value -abis value [-skip-sdk-check]`+ |
Jaewoong Jung | 11c1e0f | 2020-06-29 19:18:44 -0700 | [diff] [blame] | 583 | `-screen-densities value {-stem value | -extract-single} [-allow-prereleased] `+ |
| 584 | `[-apkcerts <apkcerts output file> -partition <partition>] <APK set>`) |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 585 | flag.PrintDefaults() |
| 586 | os.Exit(2) |
| 587 | } |
| 588 | version := flag.Uint("sdk-version", 0, "SDK version") |
| 589 | flag.Var(abiFlagValue{&targetConfig}, "abis", |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 590 | "comma-separated ABIs list of ARMEABI ARMEABI_V7A ARM64_V8A X86 X86_64 MIPS MIPS64") |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 591 | flag.Var(screenDensityFlagValue{&targetConfig}, "screen-densities", |
| 592 | "'all' or comma-separated list of screen density names (NODPI LDPI MDPI TVDPI HDPI XHDPI XXHDPI XXXHDPI)") |
| 593 | flag.BoolVar(&targetConfig.allowPrereleased, "allow-prereleased", false, |
| 594 | "allow prereleased") |
Pranav Gupta | 51645ff | 2023-03-20 16:19:53 -0700 | [diff] [blame] | 595 | flag.BoolVar(&targetConfig.skipSdkCheck, "skip-sdk-check", false, "Skip the SDK version check") |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 596 | flag.StringVar(&targetConfig.stem, "stem", "", "output entries base name in the output zip file") |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 597 | flag.Parse() |
Jaewoong Jung | 11c1e0f | 2020-06-29 19:18:44 -0700 | [diff] [blame] | 598 | if (*outputFile == "") || len(flag.Args()) != 1 || *version == 0 || |
Colin Cross | ffbcd1d | 2021-11-12 12:19:42 -0800 | [diff] [blame] | 599 | ((targetConfig.stem == "" || *zipFile == "") && !*extractSingle) || |
| 600 | (*apkcertsOutput != "" && *partition == "") { |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 601 | flag.Usage() |
| 602 | } |
| 603 | targetConfig.sdkVersion = int32(*version) |
| 604 | |
| 605 | } |
| 606 | |
| 607 | func main() { |
| 608 | processArgs() |
| 609 | var toc Toc |
| 610 | apkSet, err := newApkSet(flag.Arg(0)) |
| 611 | if err == nil { |
| 612 | defer apkSet.close() |
| 613 | toc, err = apkSet.getToc() |
| 614 | } |
| 615 | if err != nil { |
| 616 | log.Fatal(err) |
| 617 | } |
| 618 | sel := selectApks(toc, targetConfig) |
| 619 | if len(sel.entries) == 0 { |
| 620 | log.Fatalf("there are no entries for the target configuration: %#v", targetConfig) |
| 621 | } |
| 622 | |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 623 | outFile, err := os.Create(*outputFile) |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 624 | if err != nil { |
| 625 | log.Fatal(err) |
| 626 | } |
| 627 | defer outFile.Close() |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 628 | |
| 629 | if *extractSingle { |
| 630 | err = apkSet.extractAndCopySingle(sel, outFile) |
| 631 | } else { |
Colin Cross | ffbcd1d | 2021-11-12 12:19:42 -0800 | [diff] [blame] | 632 | zipOutputFile, err := os.Create(*zipFile) |
| 633 | if err != nil { |
| 634 | log.Fatal(err) |
| 635 | } |
| 636 | defer zipOutputFile.Close() |
| 637 | |
| 638 | zipWriter := zip.NewWriter(zipOutputFile) |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 639 | defer func() { |
Colin Cross | ffbcd1d | 2021-11-12 12:19:42 -0800 | [diff] [blame] | 640 | if err := zipWriter.Close(); err != nil { |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 641 | log.Fatal(err) |
| 642 | } |
| 643 | }() |
Colin Cross | ffbcd1d | 2021-11-12 12:19:42 -0800 | [diff] [blame] | 644 | |
| 645 | apkcerts, err := apkSet.writeApks(sel, targetConfig, outFile, zipWriter, *partition) |
Jaewoong Jung | 11c1e0f | 2020-06-29 19:18:44 -0700 | [diff] [blame] | 646 | if err == nil && *apkcertsOutput != "" { |
| 647 | apkcertsFile, err := os.Create(*apkcertsOutput) |
| 648 | if err != nil { |
| 649 | log.Fatal(err) |
| 650 | } |
| 651 | defer apkcertsFile.Close() |
| 652 | for _, a := range apkcerts { |
| 653 | _, err = apkcertsFile.WriteString(a + "\n") |
| 654 | if err != nil { |
| 655 | log.Fatal(err) |
| 656 | } |
| 657 | } |
| 658 | } |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 659 | } |
| 660 | if err != nil { |
Sasha Smundak | 7a894a6 | 2020-05-06 21:23:08 -0700 | [diff] [blame] | 661 | log.Fatal(err) |
| 662 | } |
| 663 | } |
Colin Cross | ffbcd1d | 2021-11-12 12:19:42 -0800 | [diff] [blame] | 664 | |
| 665 | func writeZipEntryToFile(outFile io.Writer, zipEntry *zip.File) error { |
| 666 | reader, err := zipEntry.Open() |
| 667 | if err != nil { |
| 668 | return err |
| 669 | } |
| 670 | defer reader.Close() |
| 671 | _, err = io.Copy(outFile, reader) |
| 672 | return err |
| 673 | } |