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 | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 20 | "path/filepath" |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 21 | "strings" |
| 22 | |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 23 | "github.com/google/blueprint" |
Colin Cross | 76b5f0c | 2017-08-29 16:02:06 -0700 | [diff] [blame] | 24 | "github.com/google/blueprint/proptools" |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 25 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 26 | "android/soong/android" |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 27 | "android/soong/cc" |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 28 | "android/soong/tradefed" |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 31 | func init() { |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 32 | android.RegisterModuleType("android_app", AndroidAppFactory) |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 33 | android.RegisterModuleType("android_test", AndroidTestFactory) |
Colin Cross | 252fc6f | 2018-10-04 15:22:03 -0700 | [diff] [blame] | 34 | android.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory) |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 35 | android.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory) |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 36 | android.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory) |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 37 | android.RegisterModuleType("android_app_import", AndroidAppImportFactory) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 40 | // AndroidManifest.xml merging |
| 41 | // package splits |
| 42 | |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 43 | type appProperties struct { |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 44 | // 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] | 45 | Additional_certificates []string |
| 46 | |
| 47 | // If set, create package-export.apk, which other packages can |
| 48 | // use to get PRODUCT-agnostic resource data like IDs and type definitions. |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 49 | Export_package_resources *bool |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 50 | |
Colin Cross | 1605606 | 2017-12-13 22:46:28 -0800 | [diff] [blame] | 51 | // Specifies that this app should be installed to the priv-app directory, |
| 52 | // where the system will grant it additional privileges not available to |
| 53 | // normal apps. |
| 54 | Privileged *bool |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 55 | |
| 56 | // list of resource labels to generate individual resource packages |
| 57 | Package_splits []string |
Jason Monk | d4122be | 2018-08-10 09:33:36 -0400 | [diff] [blame] | 58 | |
| 59 | // Names of modules to be overridden. Listed modules can only be other binaries |
| 60 | // (in Make or Soong). |
| 61 | // This does not completely prevent installation of the overridden binaries, but if both |
| 62 | // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed |
| 63 | // from PRODUCT_PACKAGES. |
| 64 | Overrides []string |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 65 | |
| 66 | // list of native libraries that will be provided in or alongside the resulting jar |
| 67 | Jni_libs []string `android:"arch_variant"` |
| 68 | |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 69 | // Store native libraries uncompressed in the APK and set the android:extractNativeLibs="false" manifest |
| 70 | // flag so that they are used from inside the APK at runtime. Defaults to true for android_test modules unless |
| 71 | // sdk_version or min_sdk_version is set to a version that doesn't support it (<23), defaults to false for other |
| 72 | // module types where the native libraries are generally preinstalled outside the APK. |
| 73 | Use_embedded_native_libs *bool |
Colin Cross | 46abdad | 2019-02-07 13:07:08 -0800 | [diff] [blame] | 74 | |
| 75 | // Store dex files uncompressed in the APK and set the android:useEmbeddedDex="true" manifest attribute so that |
| 76 | // they are used from inside the APK at runtime. |
| 77 | Use_embedded_dex *bool |
Colin Cross | 47fa9d3 | 2019-03-26 10:51:39 -0700 | [diff] [blame] | 78 | |
| 79 | // Forces native libraries to always be packaged into the APK, |
| 80 | // Use_embedded_native_libs still selects whether they are stored uncompressed and aligned or compressed. |
| 81 | // True for android_test* modules. |
| 82 | AlwaysPackageNativeLibs bool `blueprint:"mutated"` |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 85 | // android_app properties that can be overridden by override_android_app |
| 86 | type overridableAppProperties struct { |
| 87 | // The name of a certificate in the default certificate directory, blank to use the default product certificate, |
| 88 | // or an android_app_certificate module name in the form ":module". |
| 89 | Certificate *string |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 90 | |
| 91 | // the package name of this app. The package name in the manifest file is used if one was not given. |
| 92 | Package_name *string |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 95 | type AndroidApp struct { |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 96 | Library |
| 97 | aapt |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 98 | android.OverridableModuleBase |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 99 | |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 100 | certificate Certificate |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 101 | |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 102 | appProperties appProperties |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 103 | |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 104 | overridableAppProperties overridableAppProperties |
| 105 | |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 106 | installJniLibs []jniLib |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 107 | |
| 108 | bundleFile android.Path |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 109 | |
| 110 | // the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES. |
| 111 | installApkName string |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 112 | |
| 113 | additionalAaptFlags []string |
Colin Cross | e1731a5 | 2017-12-14 11:22:55 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 116 | func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths { |
| 117 | return nil |
| 118 | } |
| 119 | |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 120 | func (a *AndroidApp) ExportedStaticPackages() android.Paths { |
| 121 | return nil |
| 122 | } |
| 123 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 124 | var _ AndroidLibraryDependency = (*AndroidApp)(nil) |
| 125 | |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 126 | type Certificate struct { |
| 127 | Pem, Key android.Path |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 130 | func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 131 | a.Module.deps(ctx) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 132 | |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 133 | 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] | 134 | a.aapt.deps(ctx, sdkContext(a)) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 135 | } |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 136 | |
| 137 | for _, jniTarget := range ctx.MultiTargets() { |
| 138 | variation := []blueprint.Variation{ |
| 139 | {Mutator: "arch", Variation: jniTarget.String()}, |
| 140 | {Mutator: "link", Variation: "shared"}, |
| 141 | } |
| 142 | tag := &jniDependencyTag{ |
| 143 | target: jniTarget, |
| 144 | } |
| 145 | ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs...) |
| 146 | } |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 147 | |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 148 | cert := android.SrcIsModule(a.getCertString(ctx)) |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 149 | if cert != "" { |
| 150 | ctx.AddDependency(ctx.Module(), certificateTag, cert) |
| 151 | } |
| 152 | |
| 153 | for _, cert := range a.appProperties.Additional_certificates { |
| 154 | cert = android.SrcIsModule(cert) |
| 155 | if cert != "" { |
| 156 | ctx.AddDependency(ctx.Module(), certificateTag, cert) |
| 157 | } else { |
| 158 | ctx.PropertyErrorf("additional_certificates", |
| 159 | `must be names of android_app_certificate modules in the form ":module"`) |
| 160 | } |
| 161 | } |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 164 | func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 165 | a.aapt.uncompressedJNI = a.shouldUncompressJNI(ctx) |
Colin Cross | 46abdad | 2019-02-07 13:07:08 -0800 | [diff] [blame] | 166 | a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex) |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 167 | a.generateAndroidBuildActions(ctx) |
| 168 | } |
| 169 | |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 170 | // shouldUncompressJNI returns true if the native libraries should be stored in the APK uncompressed and the |
| 171 | // extractNativeLibs application flag should be set to false in the manifest. |
| 172 | func (a *AndroidApp) shouldUncompressJNI(ctx android.ModuleContext) bool { |
| 173 | minSdkVersion, err := sdkVersionToNumber(ctx, a.minSdkVersion()) |
| 174 | if err != nil { |
| 175 | ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.minSdkVersion(), err) |
| 176 | } |
| 177 | |
| 178 | return minSdkVersion >= 23 && Bool(a.appProperties.Use_embedded_native_libs) |
| 179 | } |
| 180 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 181 | // Returns whether this module should have the dex file stored uncompressed in the APK. |
| 182 | func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool { |
Colin Cross | 46abdad | 2019-02-07 13:07:08 -0800 | [diff] [blame] | 183 | if Bool(a.appProperties.Use_embedded_dex) { |
| 184 | return true |
| 185 | } |
| 186 | |
Colin Cross | 5a0dcd5 | 2018-10-05 14:20:06 -0700 | [diff] [blame] | 187 | if ctx.Config().UnbundledBuild() { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 188 | return false |
Colin Cross | 5a0dcd5 | 2018-10-05 14:20:06 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Jaewoong Jung | acf18d7 | 2019-05-02 14:55:29 -0700 | [diff] [blame^] | 191 | // Uncompress dex in APKs of privileged apps |
| 192 | if ctx.Config().UncompressPrivAppDex() && Bool(a.appProperties.Privileged) { |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 193 | return true |
| 194 | } |
| 195 | |
Jaewoong Jung | acf18d7 | 2019-05-02 14:55:29 -0700 | [diff] [blame^] | 196 | return shouldUncompressDex(ctx, &a.dexpreopter) |
Colin Cross | 5a0dcd5 | 2018-10-05 14:20:06 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 199 | func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) { |
David Brazdil | d25060a | 2019-02-18 18:24:16 +0000 | [diff] [blame] | 200 | a.aapt.usesNonSdkApis = Bool(a.Module.deviceProperties.Platform_apis) |
| 201 | |
Jaewoong Jung | de4c02f | 2019-01-22 11:19:56 -0800 | [diff] [blame] | 202 | aaptLinkFlags := []string{} |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 203 | |
Jaewoong Jung | de4c02f | 2019-01-22 11:19:56 -0800 | [diff] [blame] | 204 | // 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] | 205 | hasProduct := false |
| 206 | for _, f := range a.aaptProperties.Aaptflags { |
| 207 | if strings.HasPrefix(f, "--product") { |
| 208 | hasProduct = true |
Jaewoong Jung | de4c02f | 2019-01-22 11:19:56 -0800 | [diff] [blame] | 209 | break |
Colin Cross | e78dcd3 | 2018-04-19 15:25:19 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
Colin Cross | e78dcd3 | 2018-04-19 15:25:19 -0700 | [diff] [blame] | 212 | if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 { |
Jaewoong Jung | de4c02f | 2019-01-22 11:19:56 -0800 | [diff] [blame] | 213 | aaptLinkFlags = append(aaptLinkFlags, "--product", ctx.Config().ProductAAPTCharacteristics()) |
Colin Cross | e78dcd3 | 2018-04-19 15:25:19 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Dan Willemsen | 72be590 | 2018-10-24 20:24:57 -0700 | [diff] [blame] | 216 | if !Bool(a.aaptProperties.Aapt_include_all_resources) { |
| 217 | // Product AAPT config |
| 218 | for _, aaptConfig := range ctx.Config().ProductAAPTConfig() { |
Jaewoong Jung | de4c02f | 2019-01-22 11:19:56 -0800 | [diff] [blame] | 219 | aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig) |
Dan Willemsen | 72be590 | 2018-10-24 20:24:57 -0700 | [diff] [blame] | 220 | } |
Colin Cross | e78dcd3 | 2018-04-19 15:25:19 -0700 | [diff] [blame] | 221 | |
Dan Willemsen | 72be590 | 2018-10-24 20:24:57 -0700 | [diff] [blame] | 222 | // Product AAPT preferred config |
| 223 | if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 { |
Jaewoong Jung | de4c02f | 2019-01-22 11:19:56 -0800 | [diff] [blame] | 224 | aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig()) |
Dan Willemsen | 72be590 | 2018-10-24 20:24:57 -0700 | [diff] [blame] | 225 | } |
Colin Cross | e78dcd3 | 2018-04-19 15:25:19 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 228 | manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName()) |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 229 | if overridden || a.overridableAppProperties.Package_name != nil { |
| 230 | // The product override variable has a priority over the package_name property. |
| 231 | if !overridden { |
| 232 | manifestPackageName = *a.overridableAppProperties.Package_name |
| 233 | } |
Jaewoong Jung | de4c02f | 2019-01-22 11:19:56 -0800 | [diff] [blame] | 234 | aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName) |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 235 | } |
| 236 | |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 237 | aaptLinkFlags = append(aaptLinkFlags, a.additionalAaptFlags...) |
| 238 | |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 239 | a.aapt.splitNames = a.appProperties.Package_splits |
| 240 | |
Jaewoong Jung | de4c02f | 2019-01-22 11:19:56 -0800 | [diff] [blame] | 241 | a.aapt.buildActions(ctx, sdkContext(a), aaptLinkFlags...) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 242 | |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 243 | // apps manifests are handled by aapt, don't let Module see them |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 244 | a.properties.Manifest = nil |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 245 | } |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 246 | |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 247 | func (a *AndroidApp) proguardBuildActions(ctx android.ModuleContext) { |
Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 248 | var staticLibProguardFlagFiles android.Paths |
| 249 | ctx.VisitDirectDeps(func(m android.Module) { |
| 250 | if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag { |
| 251 | staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...) |
| 252 | } |
| 253 | }) |
| 254 | |
| 255 | staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles) |
| 256 | |
| 257 | a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...) |
| 258 | a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile) |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 259 | } |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 260 | |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 261 | func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 262 | |
| 263 | var installDir string |
| 264 | if ctx.ModuleName() == "framework-res" { |
| 265 | // framework-res.apk is installed as system/framework/framework-res.apk |
| 266 | installDir = "framework" |
| 267 | } else if Bool(a.appProperties.Privileged) { |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 268 | installDir = filepath.Join("priv-app", a.installApkName) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 269 | } else { |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 270 | installDir = filepath.Join("app", a.installApkName) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 271 | } |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 272 | a.dexpreopter.installPath = android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk") |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 273 | a.dexpreopter.isInstallable = Bool(a.properties.Installable) |
| 274 | a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx) |
| 275 | a.deviceProperties.UncompressDex = a.dexpreopter.uncompressedDex |
Colin Cross | 5a0dcd5 | 2018-10-05 14:20:06 -0700 | [diff] [blame] | 276 | |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 277 | if ctx.ModuleName() != "framework-res" { |
| 278 | a.Module.compile(ctx, a.aaptSrcJar) |
| 279 | } |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 280 | |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 281 | return a.maybeStrippedDexJarFile |
| 282 | } |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 283 | |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 284 | func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, ctx android.ModuleContext) android.WritablePath { |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 285 | var jniJarFile android.WritablePath |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 286 | if len(jniLibs) > 0 { |
Colin Cross | 47fa9d3 | 2019-03-26 10:51:39 -0700 | [diff] [blame] | 287 | embedJni := ctx.Config().UnbundledBuild() || Bool(a.appProperties.Use_embedded_native_libs) || |
| 288 | a.appProperties.AlwaysPackageNativeLibs |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 289 | if embedJni { |
| 290 | jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip") |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 291 | TransformJniLibsToJar(ctx, jniJarFile, jniLibs, a.shouldUncompressJNI(ctx)) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 292 | } else { |
| 293 | a.installJniLibs = jniLibs |
| 294 | } |
| 295 | } |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 296 | return jniJarFile |
| 297 | } |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 298 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 299 | // Reads and prepends a main cert from the default cert dir if it hasn't been set already, i.e. it |
| 300 | // isn't a cert module reference. Also checks and enforces system cert restriction if applicable. |
| 301 | func processMainCert(m android.ModuleBase, certPropValue string, certificates []Certificate, ctx android.ModuleContext) []Certificate { |
| 302 | if android.SrcIsModule(certPropValue) == "" { |
| 303 | var mainCert Certificate |
| 304 | if certPropValue != "" { |
| 305 | defaultDir := ctx.Config().DefaultAppCertificateDir(ctx) |
| 306 | mainCert = Certificate{ |
| 307 | defaultDir.Join(ctx, certPropValue+".x509.pem"), |
| 308 | defaultDir.Join(ctx, certPropValue+".pk8"), |
| 309 | } |
| 310 | } else { |
| 311 | pem, key := ctx.Config().DefaultAppCertificate(ctx) |
| 312 | mainCert = Certificate{pem, key} |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 313 | } |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 314 | certificates = append([]Certificate{mainCert}, certificates...) |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 317 | if !m.Platform() { |
| 318 | certPath := certificates[0].Pem.String() |
Jeongik Cha | c946414 | 2019-01-07 12:07:27 +0900 | [diff] [blame] | 319 | systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String() |
| 320 | if strings.HasPrefix(certPath, systemCertPath) { |
| 321 | enforceSystemCert := ctx.Config().EnforceSystemCertificate() |
| 322 | whitelist := ctx.Config().EnforceSystemCertificateWhitelist() |
| 323 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 324 | if enforceSystemCert && !inList(m.Name(), whitelist) { |
Jeongik Cha | c946414 | 2019-01-07 12:07:27 +0900 | [diff] [blame] | 325 | ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.") |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 330 | return certificates |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) { |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 334 | // Check if the install APK name needs to be overridden. |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 335 | a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name()) |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 336 | |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 337 | // Process all building blocks, from AAPT to certificates. |
| 338 | a.aaptBuildActions(ctx) |
| 339 | |
| 340 | a.proguardBuildActions(ctx) |
| 341 | |
| 342 | dexJarFile := a.dexBuildActions(ctx) |
| 343 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 344 | jniLibs, certificateDeps := collectAppDeps(ctx) |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 345 | jniJarFile := a.jniBuildActions(jniLibs, ctx) |
| 346 | |
| 347 | if ctx.Failed() { |
| 348 | return |
| 349 | } |
| 350 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 351 | certificates := processMainCert(a.ModuleBase, a.getCertString(ctx), certificateDeps, ctx) |
| 352 | a.certificate = certificates[0] |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 353 | |
| 354 | // Build a final signed app package. |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 355 | // TODO(jungjw): Consider changing this to installApkName. |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 356 | packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".apk") |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 357 | CreateAndSignAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 358 | a.outputFile = packageFile |
| 359 | |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 360 | for _, split := range a.aapt.splits { |
| 361 | // Sign the split APKs |
| 362 | packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"_"+split.suffix+".apk") |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 363 | CreateAndSignAppPackage(ctx, packageFile, split.path, nil, nil, certificates) |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 364 | a.extraOutputFiles = append(a.extraOutputFiles, packageFile) |
| 365 | } |
| 366 | |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 367 | // Build an app bundle. |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 368 | bundleFile := android.PathForModuleOut(ctx, "base.zip") |
| 369 | BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile) |
| 370 | a.bundleFile = bundleFile |
| 371 | |
Jaewoong Jung | 590b1ae | 2019-01-22 16:40:58 -0800 | [diff] [blame] | 372 | // Install the app package. |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 373 | var installDir android.OutputPath |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 374 | if ctx.ModuleName() == "framework-res" { |
| 375 | // framework-res.apk is installed as system/framework/framework-res.apk |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 376 | installDir = android.PathForModuleInstall(ctx, "framework") |
Colin Cross | 1605606 | 2017-12-13 22:46:28 -0800 | [diff] [blame] | 377 | } else if Bool(a.appProperties.Privileged) { |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 378 | installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName) |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 379 | } else { |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 380 | installDir = android.PathForModuleInstall(ctx, "app", a.installApkName) |
| 381 | } |
| 382 | |
| 383 | ctx.InstallFile(installDir, a.installApkName+".apk", a.outputFile) |
| 384 | for _, split := range a.aapt.splits { |
| 385 | ctx.InstallFile(installDir, a.installApkName+"_"+split.suffix+".apk", split.path) |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 386 | } |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 389 | func collectAppDeps(ctx android.ModuleContext) ([]jniLib, []Certificate) { |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 390 | var jniLibs []jniLib |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 391 | var certificates []Certificate |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 392 | |
| 393 | ctx.VisitDirectDeps(func(module android.Module) { |
| 394 | otherName := ctx.OtherModuleName(module) |
| 395 | tag := ctx.OtherModuleDependencyTag(module) |
| 396 | |
| 397 | if jniTag, ok := tag.(*jniDependencyTag); ok { |
| 398 | if dep, ok := module.(*cc.Module); ok { |
| 399 | lib := dep.OutputFile() |
| 400 | if lib.Valid() { |
| 401 | jniLibs = append(jniLibs, jniLib{ |
| 402 | name: ctx.OtherModuleName(module), |
| 403 | path: lib.Path(), |
| 404 | target: jniTag.target, |
| 405 | }) |
| 406 | } else { |
| 407 | ctx.ModuleErrorf("dependency %q missing output file", otherName) |
| 408 | } |
| 409 | } else { |
| 410 | ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 411 | } |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 412 | } else if tag == certificateTag { |
| 413 | if dep, ok := module.(*AndroidAppCertificate); ok { |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 414 | certificates = append(certificates, dep.Certificate) |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 415 | } else { |
| 416 | ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName) |
| 417 | } |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 418 | } |
| 419 | }) |
| 420 | |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 421 | return jniLibs, certificates |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 424 | func (a *AndroidApp) getCertString(ctx android.BaseContext) string { |
| 425 | certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName()) |
| 426 | if overridden { |
Jaewoong Jung | acb6db3 | 2019-02-28 16:22:30 +0000 | [diff] [blame] | 427 | return ":" + certificate |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 428 | } |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 429 | return String(a.overridableAppProperties.Certificate) |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 430 | } |
| 431 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 432 | // 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] | 433 | func AndroidAppFactory() android.Module { |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 434 | module := &AndroidApp{} |
| 435 | |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 436 | module.Module.deviceProperties.Optimize.EnabledByDefault = true |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 437 | module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true) |
| 438 | |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 439 | module.Module.properties.Instrument = true |
Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 440 | module.Module.properties.Installable = proptools.BoolPtr(true) |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 441 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 442 | module.AddProperties( |
Colin Cross | 540eff8 | 2017-06-22 17:01:52 -0700 | [diff] [blame] | 443 | &module.Module.properties, |
| 444 | &module.Module.deviceProperties, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 445 | &module.Module.dexpreoptProperties, |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 446 | &module.Module.protoProperties, |
| 447 | &module.aaptProperties, |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 448 | &module.appProperties, |
| 449 | &module.overridableAppProperties) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 450 | |
Colin Cross | a9d8bee | 2018-10-02 13:59:46 -0700 | [diff] [blame] | 451 | module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool { |
| 452 | return class == android.Device && ctx.Config().DevicePrefer32BitApps() |
| 453 | }) |
| 454 | |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 455 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 456 | android.InitDefaultableModule(module) |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 457 | android.InitOverridableModule(module, &module.appProperties.Overrides) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 458 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 459 | return module |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 460 | } |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 461 | |
| 462 | type appTestProperties struct { |
| 463 | Instrumentation_for *string |
| 464 | } |
| 465 | |
| 466 | type AndroidTest struct { |
| 467 | AndroidApp |
| 468 | |
| 469 | appTestProperties appTestProperties |
| 470 | |
| 471 | testProperties testProperties |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 472 | |
| 473 | testConfig android.Path |
Colin Cross | d96ca35 | 2018-08-10 16:06:24 -0700 | [diff] [blame] | 474 | data android.Paths |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 478 | // Check if the instrumentation target package is overridden before generating build actions. |
| 479 | if a.appTestProperties.Instrumentation_for != nil { |
| 480 | manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for) |
| 481 | if overridden { |
| 482 | a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName) |
| 483 | } |
| 484 | } |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 485 | a.generateAndroidBuildActions(ctx) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 486 | |
yangbill | 4f41bc2 | 2019-02-13 21:45:47 +0800 | [diff] [blame] | 487 | 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] | 488 | a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) { |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 492 | a.AndroidApp.DepsMutator(ctx) |
Colin Cross | 4b964c0 | 2018-10-15 16:18:06 -0700 | [diff] [blame] | 493 | if a.appTestProperties.Instrumentation_for != nil { |
| 494 | // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac, |
| 495 | // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so |
| 496 | // use instrumentationForTag instead of libTag. |
| 497 | ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for)) |
| 498 | } |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 501 | // android_test compiles test sources and Android resources into an Android application package `.apk` file and |
| 502 | // 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] | 503 | func AndroidTestFactory() android.Module { |
| 504 | module := &AndroidTest{} |
| 505 | |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 506 | module.Module.deviceProperties.Optimize.EnabledByDefault = true |
Colin Cross | 5067db9 | 2018-09-17 16:46:35 -0700 | [diff] [blame] | 507 | |
| 508 | module.Module.properties.Instrument = true |
Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 509 | module.Module.properties.Installable = proptools.BoolPtr(true) |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 510 | module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true) |
Colin Cross | 47fa9d3 | 2019-03-26 10:51:39 -0700 | [diff] [blame] | 511 | module.appProperties.AlwaysPackageNativeLibs = true |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 512 | module.Module.dexpreopter.isTest = true |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 513 | |
| 514 | module.AddProperties( |
| 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 | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 518 | &module.Module.protoProperties, |
| 519 | &module.aaptProperties, |
| 520 | &module.appProperties, |
Dan Willemsen | f5531d2 | 2018-07-16 17:21:19 -0700 | [diff] [blame] | 521 | &module.appTestProperties, |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 522 | &module.overridableAppProperties, |
Dan Willemsen | f5531d2 | 2018-07-16 17:21:19 -0700 | [diff] [blame] | 523 | &module.testProperties) |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 524 | |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 525 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 526 | android.InitDefaultableModule(module) |
Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 527 | return module |
| 528 | } |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 529 | |
Colin Cross | 252fc6f | 2018-10-04 15:22:03 -0700 | [diff] [blame] | 530 | type appTestHelperAppProperties struct { |
| 531 | // list of compatibility suites (for example "cts", "vts") that the module should be |
| 532 | // installed into. |
| 533 | Test_suites []string `android:"arch_variant"` |
| 534 | } |
| 535 | |
| 536 | type AndroidTestHelperApp struct { |
| 537 | AndroidApp |
| 538 | |
| 539 | appTestHelperAppProperties appTestHelperAppProperties |
| 540 | } |
| 541 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 542 | // android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that |
| 543 | // will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a |
| 544 | // test. |
Colin Cross | 252fc6f | 2018-10-04 15:22:03 -0700 | [diff] [blame] | 545 | func AndroidTestHelperAppFactory() android.Module { |
| 546 | module := &AndroidTestHelperApp{} |
| 547 | |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 548 | module.Module.deviceProperties.Optimize.EnabledByDefault = true |
Colin Cross | 252fc6f | 2018-10-04 15:22:03 -0700 | [diff] [blame] | 549 | |
| 550 | module.Module.properties.Installable = proptools.BoolPtr(true) |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 551 | module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true) |
Colin Cross | 47fa9d3 | 2019-03-26 10:51:39 -0700 | [diff] [blame] | 552 | module.appProperties.AlwaysPackageNativeLibs = true |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 553 | module.Module.dexpreopter.isTest = true |
Colin Cross | 252fc6f | 2018-10-04 15:22:03 -0700 | [diff] [blame] | 554 | |
| 555 | module.AddProperties( |
| 556 | &module.Module.properties, |
| 557 | &module.Module.deviceProperties, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 558 | &module.Module.dexpreoptProperties, |
Colin Cross | 252fc6f | 2018-10-04 15:22:03 -0700 | [diff] [blame] | 559 | &module.Module.protoProperties, |
| 560 | &module.aaptProperties, |
| 561 | &module.appProperties, |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 562 | &module.appTestHelperAppProperties, |
| 563 | &module.overridableAppProperties) |
Colin Cross | 252fc6f | 2018-10-04 15:22:03 -0700 | [diff] [blame] | 564 | |
| 565 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 566 | android.InitDefaultableModule(module) |
| 567 | return module |
| 568 | } |
| 569 | |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 570 | type AndroidAppCertificate struct { |
| 571 | android.ModuleBase |
| 572 | properties AndroidAppCertificateProperties |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 573 | Certificate Certificate |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | type AndroidAppCertificateProperties struct { |
| 577 | // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name. |
| 578 | Certificate *string |
| 579 | } |
| 580 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 581 | // android_app_certificate modules can be referenced by the certificates property of android_app modules to select |
| 582 | // the signing key. |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 583 | func AndroidAppCertificateFactory() android.Module { |
| 584 | module := &AndroidAppCertificate{} |
| 585 | module.AddProperties(&module.properties) |
| 586 | android.InitAndroidModule(module) |
| 587 | return module |
| 588 | } |
| 589 | |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 590 | func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 591 | cert := String(c.properties.Certificate) |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 592 | c.Certificate = Certificate{ |
Colin Cross | bd01e2a | 2018-10-04 15:21:03 -0700 | [diff] [blame] | 593 | android.PathForModuleSrc(ctx, cert+".x509.pem"), |
| 594 | android.PathForModuleSrc(ctx, cert+".pk8"), |
| 595 | } |
| 596 | } |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 597 | |
| 598 | type OverrideAndroidApp struct { |
| 599 | android.ModuleBase |
| 600 | android.OverrideModuleBase |
| 601 | } |
| 602 | |
| 603 | func (i *OverrideAndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 604 | // All the overrides happen in the base module. |
| 605 | // TODO(jungjw): Check the base module type. |
| 606 | } |
| 607 | |
| 608 | // override_android_app is used to create an android_app module based on another android_app by overriding |
| 609 | // some of its properties. |
| 610 | func OverrideAndroidAppModuleFactory() android.Module { |
| 611 | m := &OverrideAndroidApp{} |
| 612 | m.AddProperties(&overridableAppProperties{}) |
| 613 | |
| 614 | android.InitAndroidModule(m) |
| 615 | android.InitOverrideModule(m) |
| 616 | return m |
| 617 | } |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 618 | |
| 619 | type AndroidAppImport struct { |
| 620 | android.ModuleBase |
| 621 | android.DefaultableModuleBase |
| 622 | prebuilt android.Prebuilt |
| 623 | |
| 624 | properties AndroidAppImportProperties |
| 625 | |
| 626 | outputFile android.Path |
| 627 | certificate *Certificate |
| 628 | |
| 629 | dexpreopter |
| 630 | } |
| 631 | |
| 632 | type AndroidAppImportProperties struct { |
| 633 | // A prebuilt apk to import |
| 634 | Apk string |
| 635 | |
| 636 | // The name of a certificate in the default certificate directory, blank to use the default |
| 637 | // product certificate, or an android_app_certificate module name in the form ":module". |
| 638 | Certificate *string |
| 639 | |
| 640 | // Set this flag to true if the prebuilt apk is already signed. The certificate property must not |
| 641 | // be set for presigned modules. |
| 642 | Presigned *bool |
| 643 | |
| 644 | // Specifies that this app should be installed to the priv-app directory, |
| 645 | // where the system will grant it additional privileges not available to |
| 646 | // normal apps. |
| 647 | Privileged *bool |
| 648 | |
| 649 | // Names of modules to be overridden. Listed modules can only be other binaries |
| 650 | // (in Make or Soong). |
| 651 | // This does not completely prevent installation of the overridden binaries, but if both |
| 652 | // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed |
| 653 | // from PRODUCT_PACKAGES. |
| 654 | Overrides []string |
| 655 | } |
| 656 | |
| 657 | func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 658 | cert := android.SrcIsModule(String(a.properties.Certificate)) |
| 659 | if cert != "" { |
| 660 | ctx.AddDependency(ctx.Module(), certificateTag, cert) |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | func (a *AndroidAppImport) uncompressEmbeddedJniLibs( |
| 665 | ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) { |
| 666 | rule := android.NewRuleBuilder() |
| 667 | rule.Command(). |
| 668 | Textf(`if (zipinfo %s 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath). |
| 669 | Tool(ctx.Config().HostToolPath(ctx, "zip2zip")). |
| 670 | FlagWithInput("-i ", inputPath). |
| 671 | FlagWithOutput("-o ", outputPath). |
| 672 | FlagWithArg("-0 ", "'lib/**/*.so'"). |
| 673 | Textf(`; else cp -f %s %s; fi`, inputPath, outputPath) |
| 674 | rule.Build(pctx, ctx, "uncompress-embedded-jni-libs", "Uncompress embedded JIN libs") |
| 675 | } |
| 676 | |
Jaewoong Jung | acf18d7 | 2019-05-02 14:55:29 -0700 | [diff] [blame^] | 677 | // Returns whether this module should have the dex file stored uncompressed in the APK. |
| 678 | func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool { |
| 679 | if ctx.Config().UnbundledBuild() { |
| 680 | return false |
| 681 | } |
| 682 | |
| 683 | // Uncompress dex in APKs of privileged apps |
| 684 | if ctx.Config().UncompressPrivAppDex() && Bool(a.properties.Privileged) { |
| 685 | return true |
| 686 | } |
| 687 | |
| 688 | return shouldUncompressDex(ctx, &a.dexpreopter) |
| 689 | } |
| 690 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 691 | func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 692 | if String(a.properties.Certificate) == "" && !Bool(a.properties.Presigned) { |
| 693 | ctx.PropertyErrorf("certificate", "No certificate specified for prebuilt") |
| 694 | } |
| 695 | if String(a.properties.Certificate) != "" && Bool(a.properties.Presigned) { |
| 696 | ctx.PropertyErrorf("certificate", "Certificate can't be specified for presigned modules") |
| 697 | } |
| 698 | |
| 699 | _, certificates := collectAppDeps(ctx) |
| 700 | |
| 701 | // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK |
| 702 | // TODO: LOCAL_DPI_VARIANTS |
| 703 | // TODO: LOCAL_PACKAGE_SPLITS |
| 704 | |
| 705 | srcApk := a.prebuilt.SingleSourcePath(ctx) |
| 706 | |
| 707 | // TODO: Install or embed JNI libraries |
| 708 | |
| 709 | // Uncompress JNI libraries in the apk |
| 710 | jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk") |
| 711 | a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath) |
| 712 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 713 | installDir := android.PathForModuleInstall(ctx, "app", a.BaseModuleName()) |
| 714 | a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk") |
| 715 | a.dexpreopter.isInstallable = true |
| 716 | a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned) |
Jaewoong Jung | acf18d7 | 2019-05-02 14:55:29 -0700 | [diff] [blame^] | 717 | a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx) |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 718 | dexOutput := a.dexpreopter.dexpreopt(ctx, jnisUncompressed) |
| 719 | |
| 720 | // Sign or align the package |
| 721 | // TODO: Handle EXTERNAL |
| 722 | if !Bool(a.properties.Presigned) { |
| 723 | certificates = processMainCert(a.ModuleBase, *a.properties.Certificate, certificates, ctx) |
| 724 | if len(certificates) != 1 { |
| 725 | ctx.ModuleErrorf("Unexpected number of certificates were extracted: %q", certificates) |
| 726 | } |
| 727 | a.certificate = &certificates[0] |
| 728 | signed := android.PathForModuleOut(ctx, "signed", ctx.ModuleName()+".apk") |
| 729 | SignAppPackage(ctx, signed, dexOutput, certificates) |
| 730 | a.outputFile = signed |
| 731 | } else { |
| 732 | alignedApk := android.PathForModuleOut(ctx, "zip-aligned", ctx.ModuleName()+".apk") |
| 733 | TransformZipAlign(ctx, alignedApk, dexOutput) |
| 734 | a.outputFile = alignedApk |
| 735 | } |
| 736 | |
| 737 | // TODO: Optionally compress the output apk. |
| 738 | |
| 739 | ctx.InstallFile(installDir, a.BaseModuleName()+".apk", a.outputFile) |
| 740 | |
| 741 | // TODO: androidmk converter jni libs |
| 742 | } |
| 743 | |
| 744 | func (a *AndroidAppImport) Prebuilt() *android.Prebuilt { |
| 745 | return &a.prebuilt |
| 746 | } |
| 747 | |
| 748 | func (a *AndroidAppImport) Name() string { |
| 749 | return a.prebuilt.Name(a.ModuleBase.Name()) |
| 750 | } |
| 751 | |
| 752 | // android_app_import imports a prebuilt apk with additional processing specified in the module. |
| 753 | func AndroidAppImportFactory() android.Module { |
| 754 | module := &AndroidAppImport{} |
| 755 | module.AddProperties(&module.properties) |
| 756 | module.AddProperties(&module.dexpreoptProperties) |
| 757 | |
| 758 | InitJavaModule(module, android.DeviceSupported) |
| 759 | android.InitSingleSourcePrebuiltModule(module, &module.properties.Apk) |
| 760 | |
| 761 | return module |
| 762 | } |