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