blob: defb4cbf7e64dacece1192648a1a7e5b4413ae0e [file] [log] [blame]
Jaewoong Jungf9b44652020-12-21 12:29:12 -08001// 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
15package java
16
17// This file contains the module implementation for android_app_set.
18
19import (
20 "strconv"
21 "strings"
22
23 "github.com/google/blueprint/proptools"
24
25 "android/soong/android"
26)
27
28func init() {
29 RegisterAppSetBuildComponents(android.InitRegistrationContext)
30}
31
32func RegisterAppSetBuildComponents(ctx android.RegistrationContext) {
33 ctx.RegisterModuleType("android_app_set", AndroidAppSetFactory)
34}
35
36type AndroidAppSetProperties struct {
37 // APK Set path
38 Set *string
39
40 // Specifies that this app should be installed to the priv-app directory,
41 // where the system will grant it additional privileges not available to
42 // normal apps.
43 Privileged *bool
44
45 // APKs in this set use prerelease SDK version
46 Prerelease *bool
47
48 // Names of modules to be overridden. Listed modules can only be other apps
49 // (in Make or Soong).
50 Overrides []string
51}
52
53type AndroidAppSet struct {
54 android.ModuleBase
55 android.DefaultableModuleBase
56 prebuilt android.Prebuilt
57
Colin Crossffbcd1d2021-11-12 12:19:42 -080058 properties AndroidAppSetProperties
59 packedOutput android.WritablePath
60 primaryOutput android.WritablePath
61 apkcertsFile android.ModuleOutPath
Jaewoong Jungf9b44652020-12-21 12:29:12 -080062}
63
64func (as *AndroidAppSet) Name() string {
65 return as.prebuilt.Name(as.ModuleBase.Name())
66}
67
68func (as *AndroidAppSet) IsInstallable() bool {
69 return true
70}
71
72func (as *AndroidAppSet) Prebuilt() *android.Prebuilt {
73 return &as.prebuilt
74}
75
76func (as *AndroidAppSet) Privileged() bool {
77 return Bool(as.properties.Privileged)
78}
79
80func (as *AndroidAppSet) OutputFile() android.Path {
Colin Crossffbcd1d2021-11-12 12:19:42 -080081 return as.primaryOutput
Jaewoong Jungf9b44652020-12-21 12:29:12 -080082}
83
Colin Crossffbcd1d2021-11-12 12:19:42 -080084func (as *AndroidAppSet) PackedAdditionalOutputs() android.Path {
85 return as.packedOutput
Jaewoong Jungf9b44652020-12-21 12:29:12 -080086}
87
88func (as *AndroidAppSet) APKCertsFile() android.Path {
89 return as.apkcertsFile
90}
91
92var TargetCpuAbi = map[string]string{
93 "arm": "ARMEABI_V7A",
94 "arm64": "ARM64_V8A",
95 "x86": "X86",
96 "x86_64": "X86_64",
97}
98
Anton Hansson144be1f2022-11-25 14:06:46 +000099func SupportedAbis(ctx android.ModuleContext, excludeNativeBridgeAbis bool) []string {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800100 abiName := func(targetIdx int, deviceArch string) string {
101 if abi, found := TargetCpuAbi[deviceArch]; found {
102 return abi
103 }
104 ctx.ModuleErrorf("Target %d has invalid Arch: %s", targetIdx, deviceArch)
105 return "BAD_ABI"
106 }
107
108 var result []string
109 for i, target := range ctx.Config().Targets[android.Android] {
Anton Hansson144be1f2022-11-25 14:06:46 +0000110 if target.NativeBridge == android.NativeBridgeEnabled && excludeNativeBridgeAbis {
111 continue
112 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800113 result = append(result, abiName(i, target.Arch.ArchType.String()))
114 }
115 return result
116}
117
118func (as *AndroidAppSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
119 as.packedOutput = android.PathForModuleOut(ctx, ctx.ModuleName()+".zip")
Colin Crossffbcd1d2021-11-12 12:19:42 -0800120 as.primaryOutput = android.PathForModuleOut(ctx, as.BaseModuleName()+".apk")
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800121 as.apkcertsFile = android.PathForModuleOut(ctx, "apkcerts.txt")
122 // We are assuming here that the install file in the APK
123 // set has `.apk` suffix. If it doesn't the build will fail.
124 // APK sets containing APEX files are handled elsewhere.
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800125 screenDensities := "all"
126 if dpis := ctx.Config().ProductAAPTPrebuiltDPI(); len(dpis) > 0 {
127 screenDensities = strings.ToUpper(strings.Join(dpis, ","))
128 }
129 // TODO(asmundak): handle locales.
130 // TODO(asmundak): do we support device features
131 ctx.Build(pctx,
132 android.BuildParams{
Colin Crossffbcd1d2021-11-12 12:19:42 -0800133 Rule: extractMatchingApks,
134 Description: "Extract APKs from APK set",
135 Output: as.primaryOutput,
136 ImplicitOutputs: android.WritablePaths{as.packedOutput, as.apkcertsFile},
137 Inputs: android.Paths{as.prebuilt.SingleSourcePath(ctx)},
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800138 Args: map[string]string{
Anton Hansson144be1f2022-11-25 14:06:46 +0000139 "abis": strings.Join(SupportedAbis(ctx, false), ","),
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800140 "allow-prereleased": strconv.FormatBool(proptools.Bool(as.properties.Prerelease)),
141 "screen-densities": screenDensities,
142 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
143 "stem": as.BaseModuleName(),
144 "apkcerts": as.apkcertsFile.String(),
145 "partition": as.PartitionTag(ctx.DeviceConfig()),
Colin Crossffbcd1d2021-11-12 12:19:42 -0800146 "zip": as.packedOutput.String(),
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800147 },
148 })
Colin Cross50ed1f92021-11-12 17:41:02 -0800149
150 var installDir android.InstallPath
151 if as.Privileged() {
152 installDir = android.PathForModuleInstall(ctx, "priv-app", as.BaseModuleName())
153 } else {
154 installDir = android.PathForModuleInstall(ctx, "app", as.BaseModuleName())
155 }
156 ctx.InstallFileWithExtraFilesZip(installDir, as.BaseModuleName()+".apk", as.primaryOutput, as.packedOutput)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800157}
158
Colin Cross50ed1f92021-11-12 17:41:02 -0800159func (as *AndroidAppSet) InstallBypassMake() bool { return true }
160
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800161// android_app_set extracts a set of APKs based on the target device
162// configuration and installs this set as "split APKs".
163// The extracted set always contains an APK whose name is
164// _module_name_.apk and every split APK matching target device.
165// The extraction of the density-specific splits depends on
166// PRODUCT_AAPT_PREBUILT_DPI variable. If present (its value should
167// be a list density names: LDPI, MDPI, HDPI, etc.), only listed
168// splits will be extracted. Otherwise all density-specific splits
169// will be extracted.
170func AndroidAppSetFactory() android.Module {
171 module := &AndroidAppSet{}
172 module.AddProperties(&module.properties)
173 InitJavaModule(module, android.DeviceSupported)
174 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Set")
175 return module
176}