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