Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 1 | // Copyright 2015 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 | package java |
| 16 | |
| 17 | // This file contains the module types for compiling Android apps. |
| 18 | |
| 19 | import ( |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 20 | "path/filepath" |
| 21 | "reflect" |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 22 | "sort" |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 23 | "strings" |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 24 | |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 25 | "github.com/google/blueprint" |
| 26 | "github.com/google/blueprint/proptools" |
| 27 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 28 | "android/soong/android" |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 29 | "android/soong/cc" |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 30 | "android/soong/tradefed" |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 31 | ) |
| 32 | |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 33 | var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"} |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 34 | |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 35 | func init() { |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 36 | android.RegisterModuleType("android_app", AndroidAppFactory) |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 37 | android.RegisterModuleType("android_test", AndroidTestFactory) |
Colin Cross | 252fc6f | 2018-10-04 15:22:03 -0700 | [diff] [blame] | 38 | android.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory) |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 39 | android.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory) |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 40 | android.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory) |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 41 | android.RegisterModuleType("android_app_import", AndroidAppImportFactory) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 44 | // AndroidManifest.xml merging |
| 45 | // package splits |
| 46 | |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 47 | type appProperties struct { |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 48 | // Names of extra android_app_certificate modules to sign the apk with in the form ":module". |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 49 | Additional_certificates []string |
| 50 | |
| 51 | // If set, create package-export.apk, which other packages can |
| 52 | // use to get PRODUCT-agnostic resource data like IDs and type definitions. |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 53 | Export_package_resources *bool |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 54 | |
Colin Cross | 1605606 | 2017-12-13 22:46:28 -0800 | [diff] [blame] | 55 | // Specifies that this app should be installed to the priv-app directory, |
| 56 | // where the system will grant it additional privileges not available to |
| 57 | // normal apps. |
| 58 | Privileged *bool |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 59 | |
| 60 | // list of resource labels to generate individual resource packages |
| 61 | Package_splits []string |
Jason Monk | d4122be | 2018-08-10 09:33:36 -0400 | [diff] [blame] | 62 | |
| 63 | // Names of modules to be overridden. Listed modules can only be other binaries |
| 64 | // (in Make or Soong). |
| 65 | // This does not completely prevent installation of the overridden binaries, but if both |
| 66 | // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed |
| 67 | // from PRODUCT_PACKAGES. |
| 68 | Overrides []string |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 69 | |
| 70 | // list of native libraries that will be provided in or alongside the resulting jar |
| 71 | Jni_libs []string `android:"arch_variant"` |
| 72 | |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 73 | // STL library to use for JNI libraries. |
| 74 | Stl *string `android:"arch_variant"` |
| 75 | |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 76 | // Store native libraries uncompressed in the APK and set the android:extractNativeLibs="false" manifest |
| 77 | // flag so that they are used from inside the APK at runtime. Defaults to true for android_test modules unless |
| 78 | // sdk_version or min_sdk_version is set to a version that doesn't support it (<23), defaults to false for other |
| 79 | // module types where the native libraries are generally preinstalled outside the APK. |
| 80 | Use_embedded_native_libs *bool |
Colin Cross | 46abdad | 2019-02-07 13:07:08 -0800 | [diff] [blame] | 81 | |
| 82 | // Store dex files uncompressed in the APK and set the android:useEmbeddedDex="true" manifest attribute so that |
| 83 | // they are used from inside the APK at runtime. |
| 84 | Use_embedded_dex *bool |
Colin Cross | 47fa9d3 | 2019-03-26 10:51:39 -0700 | [diff] [blame] | 85 | |
| 86 | // Forces native libraries to always be packaged into the APK, |
| 87 | // Use_embedded_native_libs still selects whether they are stored uncompressed and aligned or compressed. |
| 88 | // True for android_test* modules. |
| 89 | AlwaysPackageNativeLibs bool `blueprint:"mutated"` |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 90 | |
| 91 | // If set, find and merge all NOTICE files that this module and its dependencies have and store |
| 92 | // it in the APK as an asset. |
| 93 | Embed_notices *bool |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 96 | // android_app properties that can be overridden by override_android_app |
| 97 | type overridableAppProperties struct { |
| 98 | // The name of a certificate in the default certificate directory, blank to use the default product certificate, |
| 99 | // or an android_app_certificate module name in the form ":module". |
| 100 | Certificate *string |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 101 | |
| 102 | // the package name of this app. The package name in the manifest file is used if one was not given. |
| 103 | Package_name *string |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 106 | type AndroidApp struct { |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 107 | Library |
| 108 | aapt |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 109 | android.OverridableModuleBase |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 110 | |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 111 | usesLibrary usesLibrary |
| 112 | |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 113 | certificate Certificate |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 114 | |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 115 | appProperties appProperties |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 116 | |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 117 | overridableAppProperties overridableAppProperties |
| 118 | |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 119 | installJniLibs []jniLib |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 120 | |
| 121 | bundleFile android.Path |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 122 | |
| 123 | // the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES. |
| 124 | installApkName string |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 125 | |
| 126 | additionalAaptFlags []string |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 127 | |
| 128 | noticeOutputs android.NoticeOutputs |
Colin Cross | e1731a5 | 2017-12-14 11:22:55 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 131 | func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths { |
| 132 | return nil |
| 133 | } |
| 134 | |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 135 | func (a *AndroidApp) ExportedStaticPackages() android.Paths { |
| 136 | return nil |
| 137 | } |
| 138 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 139 | var _ AndroidLibraryDependency = (*AndroidApp)(nil) |
| 140 | |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 141 | type Certificate struct { |
| 142 | Pem, Key android.Path |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 145 | func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 146 | a.Module.deps(ctx) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 147 | |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 148 | if String(a.appProperties.Stl) == "c++_shared" && a.sdkVersion() == "" { |
| 149 | ctx.PropertyErrorf("stl", "sdk_version must be set in order to use c++_shared") |
| 150 | } |
| 151 | |
Paul Duffin | 250e619 | 2019-06-07 10:44:37 +0100 | [diff] [blame] | 152 | sdkDep := decodeSdkDep(ctx, sdkContext(a)) |
| 153 | if sdkDep.hasFrameworkLibs() { |
| 154 | a.aapt.deps(ctx, sdkDep) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 155 | } |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 156 | |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 157 | embedJni := a.shouldEmbedJnis(ctx) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 158 | for _, jniTarget := range ctx.MultiTargets() { |
| 159 | variation := []blueprint.Variation{ |
| 160 | {Mutator: "arch", Variation: jniTarget.String()}, |
| 161 | {Mutator: "link", Variation: "shared"}, |
| 162 | } |
| 163 | tag := &jniDependencyTag{ |
| 164 | target: jniTarget, |
| 165 | } |
| 166 | ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs...) |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 167 | if String(a.appProperties.Stl) == "c++_shared" { |
| 168 | if embedJni { |
Jaewoong Jung | 710756a | 2019-06-04 11:53:47 -0700 | [diff] [blame] | 169 | ctx.AddFarVariationDependencies(variation, tag, "ndk_libc++_shared") |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 172 | } |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 173 | |
Paul Duffin | 250e619 | 2019-06-07 10:44:37 +0100 | [diff] [blame] | 174 | a.usesLibrary.deps(ctx, sdkDep.hasFrameworkLibs()) |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 175 | } |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 176 | |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 177 | func (a *AndroidApp) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) { |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 178 | cert := android.SrcIsModule(a.getCertString(ctx)) |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 179 | if cert != "" { |
| 180 | ctx.AddDependency(ctx.Module(), certificateTag, cert) |
| 181 | } |
| 182 | |
| 183 | for _, cert := range a.appProperties.Additional_certificates { |
| 184 | cert = android.SrcIsModule(cert) |
| 185 | if cert != "" { |
| 186 | ctx.AddDependency(ctx.Module(), certificateTag, cert) |
| 187 | } else { |
| 188 | ctx.PropertyErrorf("additional_certificates", |
| 189 | `must be names of android_app_certificate modules in the form ":module"`) |
| 190 | } |
| 191 | } |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame^] | 194 | func (a *AndroidTestHelperApp) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 195 | a.generateAndroidBuildActions(ctx) |
| 196 | } |
| 197 | |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 198 | func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame^] | 199 | a.checkPlatformAPI(ctx) |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 200 | a.generateAndroidBuildActions(ctx) |
| 201 | } |
| 202 | |
Sasha Smundak | 6ad7725 | 2019-05-01 13:16:22 -0700 | [diff] [blame] | 203 | // Returns true if the native libraries should be stored in the APK uncompressed and the |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 204 | // extractNativeLibs application flag should be set to false in the manifest. |
Sasha Smundak | 6ad7725 | 2019-05-01 13:16:22 -0700 | [diff] [blame] | 205 | func (a *AndroidApp) useEmbeddedNativeLibs(ctx android.ModuleContext) bool { |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 206 | minSdkVersion, err := sdkVersionToNumber(ctx, a.minSdkVersion()) |
| 207 | if err != nil { |
| 208 | ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.minSdkVersion(), err) |
| 209 | } |
| 210 | |
| 211 | return minSdkVersion >= 23 && Bool(a.appProperties.Use_embedded_native_libs) |
| 212 | } |
| 213 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 214 | // Returns whether this module should have the dex file stored uncompressed in the APK. |
| 215 | func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool { |
Colin Cross | 46abdad | 2019-02-07 13:07:08 -0800 | [diff] [blame] | 216 | if Bool(a.appProperties.Use_embedded_dex) { |
| 217 | return true |
| 218 | } |
| 219 | |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 220 | // Uncompress dex in APKs of privileged apps (even for unbundled builds, they may |
| 221 | // be preinstalled as prebuilts). |
Jaewoong Jung | acf18d7 | 2019-05-02 14:55:29 -0700 | [diff] [blame] | 222 | if ctx.Config().UncompressPrivAppDex() && Bool(a.appProperties.Privileged) { |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 223 | return true |
| 224 | } |
| 225 | |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 226 | if ctx.Config().UnbundledBuild() { |
| 227 | return false |
| 228 | } |
| 229 | |
Jaewoong Jung | acf18d7 | 2019-05-02 14:55:29 -0700 | [diff] [blame] | 230 | return shouldUncompressDex(ctx, &a.dexpreopter) |
Colin Cross | 5a0dcd5 | 2018-10-05 14:20:06 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 233 | func (a *AndroidApp) shouldEmbedJnis(ctx android.BaseModuleContext) bool { |
| 234 | return ctx.Config().UnbundledBuild() || Bool(a.appProperties.Use_embedded_native_libs) || |
| 235 | a.appProperties.AlwaysPackageNativeLibs |
| 236 | } |
| 237 | |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 238 | func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) { |
David Brazdil | d25060a | 2019-02-18 18:24:16 +0000 | [diff] [blame] | 239 | a.aapt.usesNonSdkApis = Bool(a.Module.deviceProperties.Platform_apis) |
| 240 | |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 241 | // Ask manifest_fixer to add or update the application element indicating this app has no code. |
| 242 | a.aapt.hasNoCode = !a.hasCode(ctx) |
| 243 | |
Jaewoong Jung | de4c02f | 2019-01-22 11:19:56 -0800 | [diff] [blame] | 244 | aaptLinkFlags := []string{} |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 245 | |
Jaewoong Jung | de4c02f | 2019-01-22 11:19:56 -0800 | [diff] [blame] | 246 | // Add TARGET_AAPT_CHARACTERISTICS values to AAPT link flags if they exist and --product flags were not provided. |
Colin Cross | e78dcd3 | 2018-04-19 15:25:19 -0700 | [diff] [blame] | 247 | hasProduct := false |
| 248 | for _, f := range a.aaptProperties.Aaptflags { |
| 249 | if strings.HasPrefix(f, "--product") { |
| 250 | hasProduct = true |
Jaewoong Jung | de4c02f | 2019-01-22 11:19:56 -0800 | [diff] [blame] | 251 | break |
Colin Cross | e78dcd3 | 2018-04-19 15:25:19 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
Colin Cross | e78dcd3 | 2018-04-19 15:25:19 -0700 | [diff] [blame] | 254 | if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 { |
Jaewoong Jung | de4c02f | 2019-01-22 11:19:56 -0800 | [diff] [blame] | 255 | aaptLinkFlags = append(aaptLinkFlags, "--product", ctx.Config().ProductAAPTCharacteristics()) |
Colin Cross | e78dcd3 | 2018-04-19 15:25:19 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Dan Willemsen | 72be590 | 2018-10-24 20:24:57 -0700 | [diff] [blame] | 258 | if !Bool(a.aaptProperties.Aapt_include_all_resources) { |
| 259 | // Product AAPT config |
| 260 | for _, aaptConfig := range ctx.Config().ProductAAPTConfig() { |
Jaewoong Jung | de4c02f | 2019-01-22 11:19:56 -0800 | [diff] [blame] | 261 | aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig) |
Dan Willemsen | 72be590 | 2018-10-24 20:24:57 -0700 | [diff] [blame] | 262 | } |
Colin Cross | e78dcd3 | 2018-04-19 15:25:19 -0700 | [diff] [blame] | 263 | |
Dan Willemsen | 72be590 | 2018-10-24 20:24:57 -0700 | [diff] [blame] | 264 | // Product AAPT preferred config |
| 265 | if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 { |
Jaewoong Jung | de4c02f | 2019-01-22 11:19:56 -0800 | [diff] [blame] | 266 | aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig()) |
Dan Willemsen | 72be590 | 2018-10-24 20:24:57 -0700 | [diff] [blame] | 267 | } |
Colin Cross | e78dcd3 | 2018-04-19 15:25:19 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 270 | manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName()) |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 271 | if overridden || a.overridableAppProperties.Package_name != nil { |
| 272 | // The product override variable has a priority over the package_name property. |
| 273 | if !overridden { |
| 274 | manifestPackageName = *a.overridableAppProperties.Package_name |
| 275 | } |
Jaewoong Jung | de4c02f | 2019-01-22 11:19:56 -0800 | [diff] [blame] | 276 | aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName) |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 277 | } |
| 278 | |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 279 | aaptLinkFlags = append(aaptLinkFlags, a.additionalAaptFlags...) |
| 280 | |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 281 | a.aapt.splitNames = a.appProperties.Package_splits |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 282 | a.aapt.sdkLibraries = a.exportedSdkLibs |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 283 | |
Jaewoong Jung | de4c02f | 2019-01-22 11:19:56 -0800 | [diff] [blame] | 284 | a.aapt.buildActions(ctx, sdkContext(a), aaptLinkFlags...) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 285 | |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 286 | // apps manifests are handled by aapt, don't let Module see them |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 287 | a.properties.Manifest = nil |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 288 | } |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 289 | |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 290 | func (a *AndroidApp) proguardBuildActions(ctx android.ModuleContext) { |
Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 291 | var staticLibProguardFlagFiles android.Paths |
| 292 | ctx.VisitDirectDeps(func(m android.Module) { |
| 293 | if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag { |
| 294 | staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...) |
| 295 | } |
| 296 | }) |
| 297 | |
| 298 | staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles) |
| 299 | |
| 300 | a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...) |
| 301 | a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile) |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 302 | } |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 303 | |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 304 | func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 305 | |
| 306 | var installDir string |
| 307 | if ctx.ModuleName() == "framework-res" { |
| 308 | // framework-res.apk is installed as system/framework/framework-res.apk |
| 309 | installDir = "framework" |
| 310 | } else if Bool(a.appProperties.Privileged) { |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 311 | installDir = filepath.Join("priv-app", a.installApkName) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 312 | } else { |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 313 | installDir = filepath.Join("app", a.installApkName) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 314 | } |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 315 | |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 316 | a.dexpreopter.installPath = android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk") |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 317 | a.dexpreopter.isInstallable = Bool(a.properties.Installable) |
| 318 | a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx) |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 319 | |
| 320 | a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries() |
| 321 | a.dexpreopter.usesLibs = a.usesLibrary.usesLibraryProperties.Uses_libs |
| 322 | a.dexpreopter.optionalUsesLibs = a.usesLibrary.presentOptionalUsesLibs(ctx) |
| 323 | a.dexpreopter.libraryPaths = a.usesLibrary.usesLibraryPaths(ctx) |
| 324 | a.dexpreopter.manifestFile = a.mergedManifestFile |
| 325 | |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 326 | a.deviceProperties.UncompressDex = a.dexpreopter.uncompressedDex |
Colin Cross | 5a0dcd5 | 2018-10-05 14:20:06 -0700 | [diff] [blame] | 327 | |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 328 | if ctx.ModuleName() != "framework-res" { |
| 329 | a.Module.compile(ctx, a.aaptSrcJar) |
| 330 | } |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 331 | |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 332 | return a.maybeStrippedDexJarFile |
| 333 | } |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 334 | |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 335 | func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, ctx android.ModuleContext) android.WritablePath { |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 336 | var jniJarFile android.WritablePath |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 337 | if len(jniLibs) > 0 { |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 338 | if a.shouldEmbedJnis(ctx) { |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 339 | jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip") |
Sasha Smundak | 6ad7725 | 2019-05-01 13:16:22 -0700 | [diff] [blame] | 340 | TransformJniLibsToJar(ctx, jniJarFile, jniLibs, a.useEmbeddedNativeLibs(ctx)) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 341 | } else { |
| 342 | a.installJniLibs = jniLibs |
| 343 | } |
| 344 | } |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 345 | return jniJarFile |
| 346 | } |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 347 | |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 348 | func (a *AndroidApp) noticeBuildActions(ctx android.ModuleContext, installDir android.OutputPath) { |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 349 | // Collect NOTICE files from all dependencies. |
| 350 | seenModules := make(map[android.Module]bool) |
| 351 | noticePathSet := make(map[android.Path]bool) |
| 352 | |
| 353 | ctx.WalkDeps(func(child android.Module, parent android.Module) bool { |
| 354 | // Have we already seen this? |
| 355 | if _, ok := seenModules[child]; ok { |
| 356 | return false |
| 357 | } |
| 358 | seenModules[child] = true |
| 359 | |
| 360 | // Skip host modules. |
| 361 | if child.Target().Os.Class == android.Host || child.Target().Os.Class == android.HostCross { |
| 362 | return false |
| 363 | } |
| 364 | |
| 365 | path := child.(android.Module).NoticeFile() |
| 366 | if path.Valid() { |
| 367 | noticePathSet[path.Path()] = true |
| 368 | } |
| 369 | return true |
| 370 | }) |
| 371 | |
| 372 | // If the app has one, add it too. |
| 373 | if a.NoticeFile().Valid() { |
| 374 | noticePathSet[a.NoticeFile().Path()] = true |
| 375 | } |
| 376 | |
| 377 | if len(noticePathSet) == 0 { |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 378 | return |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 379 | } |
| 380 | var noticePaths []android.Path |
| 381 | for path := range noticePathSet { |
| 382 | noticePaths = append(noticePaths, path) |
| 383 | } |
| 384 | sort.Slice(noticePaths, func(i, j int) bool { |
| 385 | return noticePaths[i].String() < noticePaths[j].String() |
| 386 | }) |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 387 | |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 388 | a.noticeOutputs = android.BuildNoticeOutput(ctx, installDir, a.installApkName+".apk", noticePaths) |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 391 | // Reads and prepends a main cert from the default cert dir if it hasn't been set already, i.e. it |
| 392 | // isn't a cert module reference. Also checks and enforces system cert restriction if applicable. |
| 393 | func processMainCert(m android.ModuleBase, certPropValue string, certificates []Certificate, ctx android.ModuleContext) []Certificate { |
| 394 | if android.SrcIsModule(certPropValue) == "" { |
| 395 | var mainCert Certificate |
| 396 | if certPropValue != "" { |
| 397 | defaultDir := ctx.Config().DefaultAppCertificateDir(ctx) |
| 398 | mainCert = Certificate{ |
| 399 | defaultDir.Join(ctx, certPropValue+".x509.pem"), |
| 400 | defaultDir.Join(ctx, certPropValue+".pk8"), |
| 401 | } |
| 402 | } else { |
| 403 | pem, key := ctx.Config().DefaultAppCertificate(ctx) |
| 404 | mainCert = Certificate{pem, key} |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 405 | } |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 406 | certificates = append([]Certificate{mainCert}, certificates...) |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 409 | if !m.Platform() { |
| 410 | certPath := certificates[0].Pem.String() |
Jeongik Cha | c946414 | 2019-01-07 12:07:27 +0900 | [diff] [blame] | 411 | systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String() |
| 412 | if strings.HasPrefix(certPath, systemCertPath) { |
| 413 | enforceSystemCert := ctx.Config().EnforceSystemCertificate() |
| 414 | whitelist := ctx.Config().EnforceSystemCertificateWhitelist() |
| 415 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 416 | if enforceSystemCert && !inList(m.Name(), whitelist) { |
Jeongik Cha | c946414 | 2019-01-07 12:07:27 +0900 | [diff] [blame] | 417 | ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.") |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 422 | return certificates |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 426 | var apkDeps android.Paths |
| 427 | |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame^] | 428 | a.aapt.useEmbeddedNativeLibs = a.useEmbeddedNativeLibs(ctx) |
| 429 | a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex) |
| 430 | |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 431 | // Check if the install APK name needs to be overridden. |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 432 | a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name()) |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 433 | |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 434 | var installDir android.OutputPath |
| 435 | if ctx.ModuleName() == "framework-res" { |
| 436 | // framework-res.apk is installed as system/framework/framework-res.apk |
| 437 | installDir = android.PathForModuleInstall(ctx, "framework") |
| 438 | } else if Bool(a.appProperties.Privileged) { |
| 439 | installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName) |
| 440 | } else { |
| 441 | installDir = android.PathForModuleInstall(ctx, "app", a.installApkName) |
| 442 | } |
| 443 | |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 444 | a.noticeBuildActions(ctx, installDir) |
| 445 | if Bool(a.appProperties.Embed_notices) || ctx.Config().IsEnvTrue("ALWAYS_EMBED_NOTICES") { |
| 446 | a.aapt.noticeFile = a.noticeOutputs.HtmlGzOutput |
| 447 | } |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 448 | |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 449 | // Process all building blocks, from AAPT to certificates. |
| 450 | a.aaptBuildActions(ctx) |
| 451 | |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 452 | if a.usesLibrary.enforceUsesLibraries() { |
| 453 | manifestCheckFile := a.usesLibrary.verifyUsesLibrariesManifest(ctx, a.mergedManifestFile) |
| 454 | apkDeps = append(apkDeps, manifestCheckFile) |
| 455 | } |
| 456 | |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 457 | a.proguardBuildActions(ctx) |
| 458 | |
| 459 | dexJarFile := a.dexBuildActions(ctx) |
| 460 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 461 | jniLibs, certificateDeps := collectAppDeps(ctx) |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 462 | jniJarFile := a.jniBuildActions(jniLibs, ctx) |
| 463 | |
| 464 | if ctx.Failed() { |
| 465 | return |
| 466 | } |
| 467 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 468 | certificates := processMainCert(a.ModuleBase, a.getCertString(ctx), certificateDeps, ctx) |
| 469 | a.certificate = certificates[0] |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 470 | |
| 471 | // Build a final signed app package. |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 472 | // TODO(jungjw): Consider changing this to installApkName. |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 473 | packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".apk") |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 474 | CreateAndSignAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates, apkDeps) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 475 | a.outputFile = packageFile |
| 476 | |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 477 | for _, split := range a.aapt.splits { |
| 478 | // Sign the split APKs |
| 479 | packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"_"+split.suffix+".apk") |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 480 | CreateAndSignAppPackage(ctx, packageFile, split.path, nil, nil, certificates, apkDeps) |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 481 | a.extraOutputFiles = append(a.extraOutputFiles, packageFile) |
| 482 | } |
| 483 | |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 484 | // Build an app bundle. |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 485 | bundleFile := android.PathForModuleOut(ctx, "base.zip") |
| 486 | BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile) |
| 487 | a.bundleFile = bundleFile |
| 488 | |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 489 | // Install the app package. |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 490 | ctx.InstallFile(installDir, a.installApkName+".apk", a.outputFile) |
| 491 | for _, split := range a.aapt.splits { |
| 492 | ctx.InstallFile(installDir, a.installApkName+"_"+split.suffix+".apk", split.path) |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 493 | } |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 496 | func collectAppDeps(ctx android.ModuleContext) ([]jniLib, []Certificate) { |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 497 | var jniLibs []jniLib |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 498 | var certificates []Certificate |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 499 | |
| 500 | ctx.VisitDirectDeps(func(module android.Module) { |
| 501 | otherName := ctx.OtherModuleName(module) |
| 502 | tag := ctx.OtherModuleDependencyTag(module) |
| 503 | |
| 504 | if jniTag, ok := tag.(*jniDependencyTag); ok { |
| 505 | if dep, ok := module.(*cc.Module); ok { |
| 506 | lib := dep.OutputFile() |
| 507 | if lib.Valid() { |
| 508 | jniLibs = append(jniLibs, jniLib{ |
| 509 | name: ctx.OtherModuleName(module), |
| 510 | path: lib.Path(), |
| 511 | target: jniTag.target, |
| 512 | }) |
| 513 | } else { |
| 514 | ctx.ModuleErrorf("dependency %q missing output file", otherName) |
| 515 | } |
| 516 | } else { |
| 517 | ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 518 | } |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 519 | } else if tag == certificateTag { |
| 520 | if dep, ok := module.(*AndroidAppCertificate); ok { |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 521 | certificates = append(certificates, dep.Certificate) |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 522 | } else { |
| 523 | ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName) |
| 524 | } |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 525 | } |
| 526 | }) |
| 527 | |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 528 | return jniLibs, certificates |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 531 | func (a *AndroidApp) getCertString(ctx android.BaseModuleContext) string { |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 532 | certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName()) |
| 533 | if overridden { |
Jaewoong Jung | acb6db3 | 2019-02-28 16:22:30 +0000 | [diff] [blame] | 534 | return ":" + certificate |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 535 | } |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 536 | return String(a.overridableAppProperties.Certificate) |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 537 | } |
| 538 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 539 | // android_app compiles sources and Android resources into an Android application package `.apk` file. |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 540 | func AndroidAppFactory() android.Module { |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 541 | module := &AndroidApp{} |
| 542 | |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 543 | module.Module.deviceProperties.Optimize.EnabledByDefault = true |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 544 | module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true) |
| 545 | |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 546 | module.Module.properties.Instrument = true |
Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 547 | module.Module.properties.Installable = proptools.BoolPtr(true) |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 548 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 549 | module.AddProperties( |
Colin Cross | 540eff8 | 2017-06-22 17:01:52 -0700 | [diff] [blame] | 550 | &module.Module.properties, |
| 551 | &module.Module.deviceProperties, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 552 | &module.Module.dexpreoptProperties, |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 553 | &module.Module.protoProperties, |
| 554 | &module.aaptProperties, |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 555 | &module.appProperties, |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 556 | &module.overridableAppProperties, |
| 557 | &module.usesLibrary.usesLibraryProperties) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 558 | |
Colin Cross | a9d8bee | 2018-10-02 13:59:46 -0700 | [diff] [blame] | 559 | module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool { |
| 560 | return class == android.Device && ctx.Config().DevicePrefer32BitApps() |
| 561 | }) |
| 562 | |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 563 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 564 | android.InitDefaultableModule(module) |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 565 | android.InitOverridableModule(module, &module.appProperties.Overrides) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 566 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 567 | return module |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 568 | } |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 569 | |
| 570 | type appTestProperties struct { |
| 571 | Instrumentation_for *string |
| 572 | } |
| 573 | |
| 574 | type AndroidTest struct { |
| 575 | AndroidApp |
| 576 | |
| 577 | appTestProperties appTestProperties |
| 578 | |
| 579 | testProperties testProperties |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 580 | |
| 581 | testConfig android.Path |
Colin Cross | d96ca35 | 2018-08-10 16:06:24 -0700 | [diff] [blame] | 582 | data android.Paths |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 586 | // Check if the instrumentation target package is overridden before generating build actions. |
| 587 | if a.appTestProperties.Instrumentation_for != nil { |
| 588 | manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for) |
| 589 | if overridden { |
| 590 | a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName) |
| 591 | } |
| 592 | } |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 593 | a.generateAndroidBuildActions(ctx) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 594 | |
yangbill | 4f41bc2 | 2019-02-13 21:45:47 +0800 | [diff] [blame] | 595 | a.testConfig = tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config, a.testProperties.Test_config_template, a.manifestPath, a.testProperties.Test_suites) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 596 | a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) { |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 600 | a.AndroidApp.DepsMutator(ctx) |
Colin Cross | 4b964c0 | 2018-10-15 16:18:06 -0700 | [diff] [blame] | 601 | if a.appTestProperties.Instrumentation_for != nil { |
| 602 | // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac, |
| 603 | // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so |
| 604 | // use instrumentationForTag instead of libTag. |
| 605 | ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for)) |
| 606 | } |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 609 | // android_test compiles test sources and Android resources into an Android application package `.apk` file and |
| 610 | // creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file. |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 611 | func AndroidTestFactory() android.Module { |
| 612 | module := &AndroidTest{} |
| 613 | |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 614 | module.Module.deviceProperties.Optimize.EnabledByDefault = true |
Colin Cross | 5067db9 | 2018-09-17 16:46:35 -0700 | [diff] [blame] | 615 | |
| 616 | module.Module.properties.Instrument = true |
Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 617 | module.Module.properties.Installable = proptools.BoolPtr(true) |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 618 | module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true) |
Colin Cross | 47fa9d3 | 2019-03-26 10:51:39 -0700 | [diff] [blame] | 619 | module.appProperties.AlwaysPackageNativeLibs = true |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 620 | module.Module.dexpreopter.isTest = true |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 621 | |
| 622 | module.AddProperties( |
| 623 | &module.Module.properties, |
| 624 | &module.Module.deviceProperties, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 625 | &module.Module.dexpreoptProperties, |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 626 | &module.Module.protoProperties, |
| 627 | &module.aaptProperties, |
| 628 | &module.appProperties, |
Dan Willemsen | f5531d2 | 2018-07-16 17:21:19 -0700 | [diff] [blame] | 629 | &module.appTestProperties, |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 630 | &module.overridableAppProperties, |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 631 | &module.usesLibrary.usesLibraryProperties, |
Dan Willemsen | f5531d2 | 2018-07-16 17:21:19 -0700 | [diff] [blame] | 632 | &module.testProperties) |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 633 | |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 634 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 635 | android.InitDefaultableModule(module) |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 636 | return module |
| 637 | } |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 638 | |
Colin Cross | 252fc6f | 2018-10-04 15:22:03 -0700 | [diff] [blame] | 639 | type appTestHelperAppProperties struct { |
| 640 | // list of compatibility suites (for example "cts", "vts") that the module should be |
| 641 | // installed into. |
| 642 | Test_suites []string `android:"arch_variant"` |
| 643 | } |
| 644 | |
| 645 | type AndroidTestHelperApp struct { |
| 646 | AndroidApp |
| 647 | |
| 648 | appTestHelperAppProperties appTestHelperAppProperties |
| 649 | } |
| 650 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 651 | // android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that |
| 652 | // will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a |
| 653 | // test. |
Colin Cross | 252fc6f | 2018-10-04 15:22:03 -0700 | [diff] [blame] | 654 | func AndroidTestHelperAppFactory() android.Module { |
| 655 | module := &AndroidTestHelperApp{} |
| 656 | |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 657 | module.Module.deviceProperties.Optimize.EnabledByDefault = true |
Colin Cross | 252fc6f | 2018-10-04 15:22:03 -0700 | [diff] [blame] | 658 | |
| 659 | module.Module.properties.Installable = proptools.BoolPtr(true) |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 660 | module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true) |
Colin Cross | 47fa9d3 | 2019-03-26 10:51:39 -0700 | [diff] [blame] | 661 | module.appProperties.AlwaysPackageNativeLibs = true |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 662 | module.Module.dexpreopter.isTest = true |
Colin Cross | 252fc6f | 2018-10-04 15:22:03 -0700 | [diff] [blame] | 663 | |
| 664 | module.AddProperties( |
| 665 | &module.Module.properties, |
| 666 | &module.Module.deviceProperties, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 667 | &module.Module.dexpreoptProperties, |
Colin Cross | 252fc6f | 2018-10-04 15:22:03 -0700 | [diff] [blame] | 668 | &module.Module.protoProperties, |
| 669 | &module.aaptProperties, |
| 670 | &module.appProperties, |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 671 | &module.appTestHelperAppProperties, |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 672 | &module.overridableAppProperties, |
| 673 | &module.usesLibrary.usesLibraryProperties) |
Colin Cross | 252fc6f | 2018-10-04 15:22:03 -0700 | [diff] [blame] | 674 | |
| 675 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 676 | android.InitDefaultableModule(module) |
| 677 | return module |
| 678 | } |
| 679 | |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 680 | type AndroidAppCertificate struct { |
| 681 | android.ModuleBase |
| 682 | properties AndroidAppCertificateProperties |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 683 | Certificate Certificate |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | type AndroidAppCertificateProperties struct { |
| 687 | // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name. |
| 688 | Certificate *string |
| 689 | } |
| 690 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 691 | // android_app_certificate modules can be referenced by the certificates property of android_app modules to select |
| 692 | // the signing key. |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 693 | func AndroidAppCertificateFactory() android.Module { |
| 694 | module := &AndroidAppCertificate{} |
| 695 | module.AddProperties(&module.properties) |
| 696 | android.InitAndroidModule(module) |
| 697 | return module |
| 698 | } |
| 699 | |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 700 | func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 701 | cert := String(c.properties.Certificate) |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 702 | c.Certificate = Certificate{ |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 703 | android.PathForModuleSrc(ctx, cert+".x509.pem"), |
| 704 | android.PathForModuleSrc(ctx, cert+".pk8"), |
| 705 | } |
| 706 | } |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 707 | |
| 708 | type OverrideAndroidApp struct { |
| 709 | android.ModuleBase |
| 710 | android.OverrideModuleBase |
| 711 | } |
| 712 | |
| 713 | func (i *OverrideAndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 714 | // All the overrides happen in the base module. |
| 715 | // TODO(jungjw): Check the base module type. |
| 716 | } |
| 717 | |
| 718 | // override_android_app is used to create an android_app module based on another android_app by overriding |
| 719 | // some of its properties. |
| 720 | func OverrideAndroidAppModuleFactory() android.Module { |
| 721 | m := &OverrideAndroidApp{} |
| 722 | m.AddProperties(&overridableAppProperties{}) |
| 723 | |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 724 | android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon) |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 725 | android.InitOverrideModule(m) |
| 726 | return m |
| 727 | } |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 728 | |
| 729 | type AndroidAppImport struct { |
| 730 | android.ModuleBase |
| 731 | android.DefaultableModuleBase |
| 732 | prebuilt android.Prebuilt |
| 733 | |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 734 | properties AndroidAppImportProperties |
| 735 | dpiVariants interface{} |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 736 | |
| 737 | outputFile android.Path |
| 738 | certificate *Certificate |
| 739 | |
| 740 | dexpreopter |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 741 | |
| 742 | usesLibrary usesLibrary |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | type AndroidAppImportProperties struct { |
| 746 | // A prebuilt apk to import |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 747 | Apk *string |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 748 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 749 | // The name of a certificate in the default certificate directory, blank to use the default |
| 750 | // product certificate, or an android_app_certificate module name in the form ":module". |
| 751 | Certificate *string |
| 752 | |
| 753 | // Set this flag to true if the prebuilt apk is already signed. The certificate property must not |
| 754 | // be set for presigned modules. |
| 755 | Presigned *bool |
| 756 | |
| 757 | // Specifies that this app should be installed to the priv-app directory, |
| 758 | // where the system will grant it additional privileges not available to |
| 759 | // normal apps. |
| 760 | Privileged *bool |
| 761 | |
| 762 | // Names of modules to be overridden. Listed modules can only be other binaries |
| 763 | // (in Make or Soong). |
| 764 | // This does not completely prevent installation of the overridden binaries, but if both |
| 765 | // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed |
| 766 | // from PRODUCT_PACKAGES. |
| 767 | Overrides []string |
| 768 | } |
| 769 | |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 770 | // Chooses a source APK path to use based on the module and product specs. |
| 771 | func (a *AndroidAppImport) updateSrcApkPath(ctx android.LoadHookContext) { |
| 772 | config := ctx.Config() |
| 773 | |
| 774 | dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName("Dpi_variants") |
| 775 | // Try DPI variant matches in the reverse-priority order so that the highest priority match |
| 776 | // overwrites everything else. |
| 777 | // TODO(jungjw): Can we optimize this by making it priority order? |
| 778 | for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- { |
| 779 | dpi := config.ProductAAPTPrebuiltDPI()[i] |
| 780 | if inList(dpi, supportedDpis) { |
| 781 | MergePropertiesFromVariant(ctx, &a.properties, dpiProps, dpi, "dpi_variants") |
| 782 | } |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 783 | } |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 784 | if config.ProductAAPTPreferredConfig() != "" { |
| 785 | dpi := config.ProductAAPTPreferredConfig() |
| 786 | if inList(dpi, supportedDpis) { |
| 787 | MergePropertiesFromVariant(ctx, &a.properties, dpiProps, dpi, "dpi_variants") |
| 788 | } |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 789 | } |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 792 | func MergePropertiesFromVariant(ctx android.BaseModuleContext, |
| 793 | dst interface{}, variantGroup reflect.Value, variant, variantGroupPath string) { |
| 794 | src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant)) |
| 795 | if !src.IsValid() { |
| 796 | ctx.ModuleErrorf("field %q does not exist", variantGroupPath+"."+variant) |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 799 | err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend) |
| 800 | if err != nil { |
| 801 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
| 802 | ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
| 803 | } else { |
| 804 | panic(err) |
| 805 | } |
| 806 | } |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 809 | func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 810 | cert := android.SrcIsModule(String(a.properties.Certificate)) |
| 811 | if cert != "" { |
| 812 | ctx.AddDependency(ctx.Module(), certificateTag, cert) |
| 813 | } |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 814 | |
Paul Duffin | 250e619 | 2019-06-07 10:44:37 +0100 | [diff] [blame] | 815 | a.usesLibrary.deps(ctx, true) |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | func (a *AndroidAppImport) uncompressEmbeddedJniLibs( |
| 819 | ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) { |
| 820 | rule := android.NewRuleBuilder() |
| 821 | rule.Command(). |
| 822 | Textf(`if (zipinfo %s 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath). |
Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 823 | BuiltTool(ctx, "zip2zip"). |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 824 | FlagWithInput("-i ", inputPath). |
| 825 | FlagWithOutput("-o ", outputPath). |
| 826 | FlagWithArg("-0 ", "'lib/**/*.so'"). |
| 827 | Textf(`; else cp -f %s %s; fi`, inputPath, outputPath) |
| 828 | rule.Build(pctx, ctx, "uncompress-embedded-jni-libs", "Uncompress embedded JIN libs") |
| 829 | } |
| 830 | |
Jaewoong Jung | acf18d7 | 2019-05-02 14:55:29 -0700 | [diff] [blame] | 831 | // Returns whether this module should have the dex file stored uncompressed in the APK. |
| 832 | func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool { |
| 833 | if ctx.Config().UnbundledBuild() { |
| 834 | return false |
| 835 | } |
| 836 | |
| 837 | // Uncompress dex in APKs of privileged apps |
| 838 | if ctx.Config().UncompressPrivAppDex() && Bool(a.properties.Privileged) { |
| 839 | return true |
| 840 | } |
| 841 | |
| 842 | return shouldUncompressDex(ctx, &a.dexpreopter) |
| 843 | } |
| 844 | |
Jaewoong Jung | ea1bdb0 | 2019-05-09 14:36:34 -0700 | [diff] [blame] | 845 | func (a *AndroidAppImport) uncompressDex( |
| 846 | ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) { |
| 847 | rule := android.NewRuleBuilder() |
| 848 | rule.Command(). |
| 849 | Textf(`if (zipinfo %s '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath). |
Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 850 | BuiltTool(ctx, "zip2zip"). |
Jaewoong Jung | ea1bdb0 | 2019-05-09 14:36:34 -0700 | [diff] [blame] | 851 | FlagWithInput("-i ", inputPath). |
| 852 | FlagWithOutput("-o ", outputPath). |
| 853 | FlagWithArg("-0 ", "'classes*.dex'"). |
| 854 | Textf(`; else cp -f %s %s; fi`, inputPath, outputPath) |
| 855 | rule.Build(pctx, ctx, "uncompress-dex", "Uncompress dex files") |
| 856 | } |
| 857 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 858 | func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 859 | if String(a.properties.Certificate) == "" && !Bool(a.properties.Presigned) { |
| 860 | ctx.PropertyErrorf("certificate", "No certificate specified for prebuilt") |
| 861 | } |
| 862 | if String(a.properties.Certificate) != "" && Bool(a.properties.Presigned) { |
| 863 | ctx.PropertyErrorf("certificate", "Certificate can't be specified for presigned modules") |
| 864 | } |
| 865 | |
| 866 | _, certificates := collectAppDeps(ctx) |
| 867 | |
| 868 | // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 869 | // TODO: LOCAL_PACKAGE_SPLITS |
| 870 | |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 871 | srcApk := a.prebuilt.SingleSourcePath(ctx) |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 872 | |
| 873 | if a.usesLibrary.enforceUsesLibraries() { |
| 874 | srcApk = a.usesLibrary.verifyUsesLibrariesAPK(ctx, srcApk) |
| 875 | } |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 876 | |
| 877 | // TODO: Install or embed JNI libraries |
| 878 | |
| 879 | // Uncompress JNI libraries in the apk |
| 880 | jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk") |
| 881 | a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath) |
| 882 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 883 | installDir := android.PathForModuleInstall(ctx, "app", a.BaseModuleName()) |
| 884 | a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk") |
| 885 | a.dexpreopter.isInstallable = true |
| 886 | a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned) |
Jaewoong Jung | acf18d7 | 2019-05-02 14:55:29 -0700 | [diff] [blame] | 887 | a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx) |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 888 | |
| 889 | a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries() |
| 890 | a.dexpreopter.usesLibs = a.usesLibrary.usesLibraryProperties.Uses_libs |
| 891 | a.dexpreopter.optionalUsesLibs = a.usesLibrary.presentOptionalUsesLibs(ctx) |
| 892 | a.dexpreopter.libraryPaths = a.usesLibrary.usesLibraryPaths(ctx) |
| 893 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 894 | dexOutput := a.dexpreopter.dexpreopt(ctx, jnisUncompressed) |
Jaewoong Jung | ea1bdb0 | 2019-05-09 14:36:34 -0700 | [diff] [blame] | 895 | if a.dexpreopter.uncompressedDex { |
| 896 | dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk") |
| 897 | a.uncompressDex(ctx, dexOutput, dexUncompressed.OutputPath) |
| 898 | dexOutput = dexUncompressed |
| 899 | } |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 900 | |
| 901 | // Sign or align the package |
| 902 | // TODO: Handle EXTERNAL |
| 903 | if !Bool(a.properties.Presigned) { |
| 904 | certificates = processMainCert(a.ModuleBase, *a.properties.Certificate, certificates, ctx) |
| 905 | if len(certificates) != 1 { |
| 906 | ctx.ModuleErrorf("Unexpected number of certificates were extracted: %q", certificates) |
| 907 | } |
| 908 | a.certificate = &certificates[0] |
| 909 | signed := android.PathForModuleOut(ctx, "signed", ctx.ModuleName()+".apk") |
| 910 | SignAppPackage(ctx, signed, dexOutput, certificates) |
| 911 | a.outputFile = signed |
| 912 | } else { |
| 913 | alignedApk := android.PathForModuleOut(ctx, "zip-aligned", ctx.ModuleName()+".apk") |
| 914 | TransformZipAlign(ctx, alignedApk, dexOutput) |
| 915 | a.outputFile = alignedApk |
| 916 | } |
| 917 | |
| 918 | // TODO: Optionally compress the output apk. |
| 919 | |
| 920 | ctx.InstallFile(installDir, a.BaseModuleName()+".apk", a.outputFile) |
| 921 | |
| 922 | // TODO: androidmk converter jni libs |
| 923 | } |
| 924 | |
| 925 | func (a *AndroidAppImport) Prebuilt() *android.Prebuilt { |
| 926 | return &a.prebuilt |
| 927 | } |
| 928 | |
| 929 | func (a *AndroidAppImport) Name() string { |
| 930 | return a.prebuilt.Name(a.ModuleBase.Name()) |
| 931 | } |
| 932 | |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 933 | // Populates dpi_variants property and its fields at creation time. |
| 934 | func (a *AndroidAppImport) addDpiVariants() { |
| 935 | // TODO(jungjw): Do we want to do some filtering here? |
| 936 | props := reflect.ValueOf(&a.properties).Type() |
| 937 | |
| 938 | dpiFields := make([]reflect.StructField, len(supportedDpis)) |
| 939 | for i, dpi := range supportedDpis { |
| 940 | dpiFields[i] = reflect.StructField{ |
| 941 | Name: proptools.FieldNameForProperty(dpi), |
| 942 | Type: props, |
| 943 | } |
| 944 | } |
| 945 | dpiStruct := reflect.StructOf(dpiFields) |
| 946 | a.dpiVariants = reflect.New(reflect.StructOf([]reflect.StructField{ |
| 947 | { |
| 948 | Name: "Dpi_variants", |
| 949 | Type: dpiStruct, |
| 950 | }, |
| 951 | })).Interface() |
| 952 | a.AddProperties(a.dpiVariants) |
| 953 | } |
| 954 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 955 | // android_app_import imports a prebuilt apk with additional processing specified in the module. |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 956 | // DPI-specific apk source files can be specified using dpi_variants. Example: |
| 957 | // |
| 958 | // android_app_import { |
| 959 | // name: "example_import", |
| 960 | // apk: "prebuilts/example.apk", |
| 961 | // dpi_variants: { |
| 962 | // mdpi: { |
| 963 | // apk: "prebuilts/example_mdpi.apk", |
| 964 | // }, |
| 965 | // xhdpi: { |
| 966 | // apk: "prebuilts/example_xhdpi.apk", |
| 967 | // }, |
| 968 | // }, |
| 969 | // certificate: "PRESIGNED", |
| 970 | // } |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 971 | func AndroidAppImportFactory() android.Module { |
| 972 | module := &AndroidAppImport{} |
| 973 | module.AddProperties(&module.properties) |
| 974 | module.AddProperties(&module.dexpreoptProperties) |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 975 | module.AddProperties(&module.usesLibrary.usesLibraryProperties) |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 976 | module.addDpiVariants() |
| 977 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { |
| 978 | module.updateSrcApkPath(ctx) |
| 979 | }) |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 980 | |
| 981 | InitJavaModule(module, android.DeviceSupported) |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 982 | android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk") |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 983 | |
| 984 | return module |
| 985 | } |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 986 | |
| 987 | type UsesLibraryProperties struct { |
| 988 | // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file. |
| 989 | Uses_libs []string |
| 990 | |
| 991 | // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file with |
| 992 | // required=false. |
| 993 | Optional_uses_libs []string |
| 994 | |
| 995 | // If true, the list of uses_libs and optional_uses_libs modules must match the AndroidManifest.xml file. Defaults |
| 996 | // to true if either uses_libs or optional_uses_libs is set. Will unconditionally default to true in the future. |
| 997 | Enforce_uses_libs *bool |
| 998 | } |
| 999 | |
| 1000 | // usesLibrary provides properties and helper functions for AndroidApp and AndroidAppImport to verify that the |
| 1001 | // <uses-library> tags that end up in the manifest of an APK match the ones known to the build system through the |
| 1002 | // uses_libs and optional_uses_libs properties. The build system's values are used by dexpreopt to preopt apps |
| 1003 | // with knowledge of their shared libraries. |
| 1004 | type usesLibrary struct { |
| 1005 | usesLibraryProperties UsesLibraryProperties |
| 1006 | } |
| 1007 | |
Paul Duffin | 250e619 | 2019-06-07 10:44:37 +0100 | [diff] [blame] | 1008 | func (u *usesLibrary) deps(ctx android.BottomUpMutatorContext, hasFrameworkLibs bool) { |
Colin Cross | 3245b2c | 2019-06-07 13:18:09 -0700 | [diff] [blame] | 1009 | if !ctx.Config().UnbundledBuild() { |
| 1010 | ctx.AddVariationDependencies(nil, usesLibTag, u.usesLibraryProperties.Uses_libs...) |
| 1011 | ctx.AddVariationDependencies(nil, usesLibTag, u.presentOptionalUsesLibs(ctx)...) |
Paul Duffin | 250e619 | 2019-06-07 10:44:37 +0100 | [diff] [blame] | 1012 | // Only add these extra dependencies if the module depends on framework libs. This avoids |
| 1013 | // creating a cyclic dependency: |
| 1014 | // e.g. framework-res -> org.apache.http.legacy -> ... -> framework-res. |
| 1015 | if hasFrameworkLibs { |
Colin Cross | 3245b2c | 2019-06-07 13:18:09 -0700 | [diff] [blame] | 1016 | // dexpreopt/dexpreopt.go needs the paths to the dex jars of these libraries in case construct_context.sh needs |
| 1017 | // to pass them to dex2oat. Add them as a dependency so we can determine the path to the dex jar of each |
| 1018 | // library to dexpreopt. |
| 1019 | ctx.AddVariationDependencies(nil, usesLibTag, |
| 1020 | "org.apache.http.legacy", |
| 1021 | "android.hidl.base-V1.0-java", |
| 1022 | "android.hidl.manager-V1.0-java") |
| 1023 | } |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | // presentOptionalUsesLibs returns optional_uses_libs after filtering out MissingUsesLibraries, which don't exist in the |
| 1028 | // build. |
| 1029 | func (u *usesLibrary) presentOptionalUsesLibs(ctx android.BaseModuleContext) []string { |
| 1030 | optionalUsesLibs, _ := android.FilterList(u.usesLibraryProperties.Optional_uses_libs, ctx.Config().MissingUsesLibraries()) |
| 1031 | return optionalUsesLibs |
| 1032 | } |
| 1033 | |
| 1034 | // usesLibraryPaths returns a map of module names of shared library dependencies to the paths to their dex jars. |
| 1035 | func (u *usesLibrary) usesLibraryPaths(ctx android.ModuleContext) map[string]android.Path { |
| 1036 | usesLibPaths := make(map[string]android.Path) |
| 1037 | |
| 1038 | if !ctx.Config().UnbundledBuild() { |
| 1039 | ctx.VisitDirectDepsWithTag(usesLibTag, func(m android.Module) { |
| 1040 | if lib, ok := m.(Dependency); ok { |
| 1041 | if dexJar := lib.DexJar(); dexJar != nil { |
| 1042 | usesLibPaths[ctx.OtherModuleName(m)] = dexJar |
| 1043 | } else { |
| 1044 | ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must produce a dex jar, does it have installable: true?", |
| 1045 | ctx.OtherModuleName(m)) |
| 1046 | } |
| 1047 | } else if ctx.Config().AllowMissingDependencies() { |
| 1048 | ctx.AddMissingDependencies([]string{ctx.OtherModuleName(m)}) |
| 1049 | } else { |
| 1050 | ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must be a java library", |
| 1051 | ctx.OtherModuleName(m)) |
| 1052 | } |
| 1053 | }) |
| 1054 | } |
| 1055 | |
| 1056 | return usesLibPaths |
| 1057 | } |
| 1058 | |
| 1059 | // enforceUsesLibraries returns true of <uses-library> tags should be checked against uses_libs and optional_uses_libs |
| 1060 | // properties. Defaults to true if either of uses_libs or optional_uses_libs is specified. Will default to true |
| 1061 | // unconditionally in the future. |
| 1062 | func (u *usesLibrary) enforceUsesLibraries() bool { |
| 1063 | defaultEnforceUsesLibs := len(u.usesLibraryProperties.Uses_libs) > 0 || |
| 1064 | len(u.usesLibraryProperties.Optional_uses_libs) > 0 |
| 1065 | return BoolDefault(u.usesLibraryProperties.Enforce_uses_libs, defaultEnforceUsesLibs) |
| 1066 | } |
| 1067 | |
| 1068 | // verifyUsesLibrariesManifest checks the <uses-library> tags in an AndroidManifest.xml against the ones specified |
| 1069 | // in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the manifest. |
| 1070 | func (u *usesLibrary) verifyUsesLibrariesManifest(ctx android.ModuleContext, manifest android.Path) android.Path { |
| 1071 | outputFile := android.PathForModuleOut(ctx, "manifest_check", "AndroidManifest.xml") |
| 1072 | |
| 1073 | rule := android.NewRuleBuilder() |
Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 1074 | cmd := rule.Command().BuiltTool(ctx, "manifest_check"). |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 1075 | Flag("--enforce-uses-libraries"). |
| 1076 | Input(manifest). |
| 1077 | FlagWithOutput("-o ", outputFile) |
| 1078 | |
| 1079 | for _, lib := range u.usesLibraryProperties.Uses_libs { |
| 1080 | cmd.FlagWithArg("--uses-library ", lib) |
| 1081 | } |
| 1082 | |
| 1083 | for _, lib := range u.usesLibraryProperties.Optional_uses_libs { |
| 1084 | cmd.FlagWithArg("--optional-uses-library ", lib) |
| 1085 | } |
| 1086 | |
| 1087 | rule.Build(pctx, ctx, "verify_uses_libraries", "verify <uses-library>") |
| 1088 | |
| 1089 | return outputFile |
| 1090 | } |
| 1091 | |
| 1092 | // verifyUsesLibrariesAPK checks the <uses-library> tags in the manifest of an APK against the ones specified |
| 1093 | // in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the APK. |
| 1094 | func (u *usesLibrary) verifyUsesLibrariesAPK(ctx android.ModuleContext, apk android.Path) android.Path { |
| 1095 | outputFile := android.PathForModuleOut(ctx, "verify_uses_libraries", apk.Base()) |
| 1096 | |
| 1097 | rule := android.NewRuleBuilder() |
| 1098 | aapt := ctx.Config().HostToolPath(ctx, "aapt") |
| 1099 | rule.Command(). |
| 1100 | Textf("aapt_binary=%s", aapt.String()).Implicit(aapt). |
| 1101 | Textf(`uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Uses_libs, " ")). |
| 1102 | Textf(`optional_uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Optional_uses_libs, " ")). |
| 1103 | Tool(android.PathForSource(ctx, "build/make/core/verify_uses_libraries.sh")).Input(apk) |
| 1104 | rule.Command().Text("cp -f").Input(apk).Output(outputFile) |
| 1105 | |
| 1106 | rule.Build(pctx, ctx, "verify_uses_libraries", "verify <uses-library>") |
| 1107 | |
| 1108 | return outputFile |
| 1109 | } |