| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 1 | // Copyright 2020 Google Inc. All rights reserved. | 
 | 2 | // | 
 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 4 | // you may not use this file except in compliance with the License. | 
 | 5 | // You may obtain a copy of the License at | 
 | 6 | // | 
 | 7 | //     http://www.apache.org/licenses/LICENSE-2.0 | 
 | 8 | // | 
 | 9 | // Unless required by applicable law or agreed to in writing, software | 
 | 10 | // distributed under the License is distributed on an "AS IS" BASIS, | 
 | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 12 | // See the License for the specific language governing permissions and | 
 | 13 | // limitations under the License. | 
 | 14 |  | 
 | 15 | package java | 
 | 16 |  | 
 | 17 | // This file contains the module implementations for android_app_import and android_test_import. | 
 | 18 |  | 
 | 19 | import ( | 
| Colin Cross | 5368d0b | 2023-07-07 11:32:32 -0700 | [diff] [blame] | 20 | 	"fmt" | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 21 | 	"reflect" | 
| Cole Faust | 9c5c09f | 2023-09-06 16:11:44 -0700 | [diff] [blame] | 22 | 	"strings" | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 23 |  | 
| Cole Faust | d580613 | 2023-04-13 15:43:53 -0700 | [diff] [blame] | 24 | 	"github.com/google/blueprint" | 
 | 25 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 26 | 	"github.com/google/blueprint/proptools" | 
 | 27 |  | 
 | 28 | 	"android/soong/android" | 
| Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 29 | 	"android/soong/provenance" | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 30 | ) | 
 | 31 |  | 
 | 32 | func init() { | 
 | 33 | 	RegisterAppImportBuildComponents(android.InitRegistrationContext) | 
 | 34 |  | 
 | 35 | 	initAndroidAppImportVariantGroupTypes() | 
 | 36 | } | 
 | 37 |  | 
| Cole Faust | 4ec178c | 2023-01-13 12:03:38 -0800 | [diff] [blame] | 38 | var ( | 
 | 39 | 	uncompressEmbeddedJniLibsRule = pctx.AndroidStaticRule("uncompress-embedded-jni-libs", blueprint.RuleParams{ | 
 | 40 | 		Command: `if (zipinfo $in 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then ` + | 
 | 41 | 			`${config.Zip2ZipCmd} -i $in -o $out -0 'lib/**/*.so'` + | 
 | 42 | 			`; else cp -f $in $out; fi`, | 
 | 43 | 		CommandDeps: []string{"${config.Zip2ZipCmd}"}, | 
 | 44 | 		Description: "Uncompress embedded JNI libs", | 
 | 45 | 	}) | 
 | 46 |  | 
 | 47 | 	uncompressDexRule = pctx.AndroidStaticRule("uncompress-dex", blueprint.RuleParams{ | 
 | 48 | 		Command: `if (zipinfo $in '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then ` + | 
 | 49 | 			`${config.Zip2ZipCmd} -i $in -o $out -0 'classes*.dex'` + | 
 | 50 | 			`; else cp -f $in $out; fi`, | 
 | 51 | 		CommandDeps: []string{"${config.Zip2ZipCmd}"}, | 
 | 52 | 		Description: "Uncompress dex files", | 
 | 53 | 	}) | 
| Cole Faust | 2f1da16 | 2023-04-17 15:06:56 -0700 | [diff] [blame] | 54 |  | 
| Cole Faust | 9c5c09f | 2023-09-06 16:11:44 -0700 | [diff] [blame] | 55 | 	checkPresignedApkRule = pctx.AndroidStaticRule("check-presigned-apk", blueprint.RuleParams{ | 
 | 56 | 		Command:     "build/soong/scripts/check_prebuilt_presigned_apk.py --aapt2 ${config.Aapt2Cmd} --zipalign ${config.ZipAlign} $extraArgs $in $out", | 
 | 57 | 		CommandDeps: []string{"build/soong/scripts/check_prebuilt_presigned_apk.py", "${config.Aapt2Cmd}", "${config.ZipAlign}"}, | 
 | 58 | 		Description: "Check presigned apk", | 
 | 59 | 	}, "extraArgs") | 
| Cole Faust | 4ec178c | 2023-01-13 12:03:38 -0800 | [diff] [blame] | 60 | ) | 
 | 61 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 62 | func RegisterAppImportBuildComponents(ctx android.RegistrationContext) { | 
 | 63 | 	ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory) | 
 | 64 | 	ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory) | 
 | 65 | } | 
 | 66 |  | 
 | 67 | type AndroidAppImport struct { | 
 | 68 | 	android.ModuleBase | 
 | 69 | 	android.DefaultableModuleBase | 
 | 70 | 	android.ApexModuleBase | 
 | 71 | 	prebuilt android.Prebuilt | 
 | 72 |  | 
 | 73 | 	properties   AndroidAppImportProperties | 
 | 74 | 	dpiVariants  interface{} | 
 | 75 | 	archVariants interface{} | 
 | 76 |  | 
 | 77 | 	outputFile  android.Path | 
 | 78 | 	certificate Certificate | 
 | 79 |  | 
 | 80 | 	dexpreopter | 
 | 81 |  | 
 | 82 | 	usesLibrary usesLibrary | 
 | 83 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 84 | 	installPath android.InstallPath | 
 | 85 |  | 
 | 86 | 	hideApexVariantFromMake bool | 
| Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 87 |  | 
 | 88 | 	provenanceMetaDataFile android.OutputPath | 
| LaMont Jones | afe7baf | 2024-01-09 22:47:39 +0000 | [diff] [blame] | 89 |  | 
 | 90 | 	// Single aconfig "cache file" merged from this module and all dependencies. | 
 | 91 | 	mergedAconfigFiles map[string]android.Paths | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 92 | } | 
 | 93 |  | 
 | 94 | type AndroidAppImportProperties struct { | 
 | 95 | 	// A prebuilt apk to import | 
| Jooyung Han | f05ca9c | 2021-06-28 21:48:51 +0900 | [diff] [blame] | 96 | 	Apk *string `android:"path"` | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 97 |  | 
 | 98 | 	// The name of a certificate in the default certificate directory or an android_app_certificate | 
 | 99 | 	// module name in the form ":module". Should be empty if presigned or default_dev_cert is set. | 
 | 100 | 	Certificate *string | 
 | 101 |  | 
| Jaewoong Jung | 25ae8de | 2021-03-08 17:37:46 -0800 | [diff] [blame] | 102 | 	// Names of extra android_app_certificate modules to sign the apk with in the form ":module". | 
 | 103 | 	Additional_certificates []string | 
 | 104 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 105 | 	// Set this flag to true if the prebuilt apk is already signed. The certificate property must not | 
 | 106 | 	// be set for presigned modules. | 
 | 107 | 	Presigned *bool | 
 | 108 |  | 
| Jaewoong Jung | 1c1b6e6 | 2021-03-09 15:02:31 -0800 | [diff] [blame] | 109 | 	// Name of the signing certificate lineage file or filegroup module. | 
 | 110 | 	Lineage *string `android:"path"` | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 111 |  | 
| Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 112 | 	// For overriding the --rotation-min-sdk-version property of apksig | 
 | 113 | 	RotationMinSdkVersion *string | 
 | 114 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 115 | 	// Sign with the default system dev certificate. Must be used judiciously. Most imported apps | 
 | 116 | 	// need to either specify a specific certificate or be presigned. | 
 | 117 | 	Default_dev_cert *bool | 
 | 118 |  | 
 | 119 | 	// Specifies that this app should be installed to the priv-app directory, | 
 | 120 | 	// where the system will grant it additional privileges not available to | 
 | 121 | 	// normal apps. | 
 | 122 | 	Privileged *bool | 
 | 123 |  | 
 | 124 | 	// Names of modules to be overridden. Listed modules can only be other binaries | 
 | 125 | 	// (in Make or Soong). | 
 | 126 | 	// This does not completely prevent installation of the overridden binaries, but if both | 
 | 127 | 	// binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed | 
 | 128 | 	// from PRODUCT_PACKAGES. | 
 | 129 | 	Overrides []string | 
 | 130 |  | 
 | 131 | 	// Optional name for the installed app. If unspecified, it is derived from the module name. | 
 | 132 | 	Filename *string | 
| Bill Peckham | a036da9 | 2021-01-08 16:09:09 -0800 | [diff] [blame] | 133 |  | 
 | 134 | 	// If set, create package-export.apk, which other packages can | 
 | 135 | 	// use to get PRODUCT-agnostic resource data like IDs and type definitions. | 
 | 136 | 	Export_package_resources *bool | 
| Spandan Das | d1fac64 | 2021-05-18 17:01:41 +0000 | [diff] [blame] | 137 |  | 
 | 138 | 	// Optional. Install to a subdirectory of the default install path for the module | 
 | 139 | 	Relative_install_path *string | 
| Cole Faust | 2f1da16 | 2023-04-17 15:06:56 -0700 | [diff] [blame] | 140 |  | 
 | 141 | 	// Whether the prebuilt apk can be installed without additional processing. Default is false. | 
 | 142 | 	Preprocessed *bool | 
 | 143 |  | 
 | 144 | 	// Whether or not to skip checking the preprocessed apk for proper alignment and uncompressed | 
 | 145 | 	// JNI libs and dex files. Default is false | 
 | 146 | 	Skip_preprocessed_apk_checks *bool | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 147 | } | 
 | 148 |  | 
 | 149 | func (a *AndroidAppImport) IsInstallable() bool { | 
 | 150 | 	return true | 
 | 151 | } | 
 | 152 |  | 
 | 153 | // Updates properties with variant-specific values. | 
 | 154 | func (a *AndroidAppImport) processVariants(ctx android.LoadHookContext) { | 
 | 155 | 	config := ctx.Config() | 
 | 156 |  | 
 | 157 | 	dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName("Dpi_variants") | 
 | 158 | 	// Try DPI variant matches in the reverse-priority order so that the highest priority match | 
 | 159 | 	// overwrites everything else. | 
 | 160 | 	// TODO(jungjw): Can we optimize this by making it priority order? | 
 | 161 | 	for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- { | 
 | 162 | 		MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPrebuiltDPI()[i]) | 
 | 163 | 	} | 
 | 164 | 	if config.ProductAAPTPreferredConfig() != "" { | 
 | 165 | 		MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig()) | 
 | 166 | 	} | 
 | 167 |  | 
 | 168 | 	archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName("Arch") | 
 | 169 | 	archType := ctx.Config().AndroidFirstDeviceTarget.Arch.ArchType | 
 | 170 | 	MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name) | 
 | 171 |  | 
 | 172 | 	if String(a.properties.Apk) == "" { | 
 | 173 | 		// Disable this module since the apk property is still empty after processing all matching | 
 | 174 | 		// variants. This likely means there is no matching variant, and the default variant doesn't | 
 | 175 | 		// have an apk property value either. | 
 | 176 | 		a.Disable() | 
 | 177 | 	} | 
 | 178 | } | 
 | 179 |  | 
 | 180 | func MergePropertiesFromVariant(ctx android.EarlyModuleContext, | 
 | 181 | 	dst interface{}, variantGroup reflect.Value, variant string) { | 
 | 182 | 	src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant)) | 
 | 183 | 	if !src.IsValid() { | 
 | 184 | 		return | 
 | 185 | 	} | 
 | 186 |  | 
 | 187 | 	err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend) | 
 | 188 | 	if err != nil { | 
 | 189 | 		if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { | 
 | 190 | 			ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) | 
 | 191 | 		} else { | 
 | 192 | 			panic(err) | 
 | 193 | 		} | 
 | 194 | 	} | 
 | 195 | } | 
 | 196 |  | 
 | 197 | func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) { | 
 | 198 | 	cert := android.SrcIsModule(String(a.properties.Certificate)) | 
 | 199 | 	if cert != "" { | 
 | 200 | 		ctx.AddDependency(ctx.Module(), certificateTag, cert) | 
 | 201 | 	} | 
 | 202 |  | 
| Jaewoong Jung | 25ae8de | 2021-03-08 17:37:46 -0800 | [diff] [blame] | 203 | 	for _, cert := range a.properties.Additional_certificates { | 
 | 204 | 		cert = android.SrcIsModule(cert) | 
 | 205 | 		if cert != "" { | 
 | 206 | 			ctx.AddDependency(ctx.Module(), certificateTag, cert) | 
 | 207 | 		} else { | 
 | 208 | 			ctx.PropertyErrorf("additional_certificates", | 
 | 209 | 				`must be names of android_app_certificate modules in the form ":module"`) | 
 | 210 | 		} | 
 | 211 | 	} | 
 | 212 |  | 
| Cole Faust | d580613 | 2023-04-13 15:43:53 -0700 | [diff] [blame] | 213 | 	a.usesLibrary.deps(ctx, true) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 214 | } | 
 | 215 |  | 
 | 216 | func (a *AndroidAppImport) uncompressEmbeddedJniLibs( | 
 | 217 | 	ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) { | 
 | 218 | 	// Test apps don't need their JNI libraries stored uncompressed. As a matter of fact, messing | 
 | 219 | 	// with them may invalidate pre-existing signature data. | 
| Cole Faust | 2f1da16 | 2023-04-17 15:06:56 -0700 | [diff] [blame] | 220 | 	if ctx.InstallInTestcases() && (Bool(a.properties.Presigned) || Bool(a.properties.Preprocessed)) { | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 221 | 		ctx.Build(pctx, android.BuildParams{ | 
 | 222 | 			Rule:   android.Cp, | 
 | 223 | 			Output: outputPath, | 
 | 224 | 			Input:  inputPath, | 
 | 225 | 		}) | 
 | 226 | 		return | 
 | 227 | 	} | 
| Cole Faust | 4ec178c | 2023-01-13 12:03:38 -0800 | [diff] [blame] | 228 |  | 
 | 229 | 	ctx.Build(pctx, android.BuildParams{ | 
 | 230 | 		Rule:   uncompressEmbeddedJniLibsRule, | 
 | 231 | 		Input:  inputPath, | 
 | 232 | 		Output: outputPath, | 
 | 233 | 	}) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 234 | } | 
 | 235 |  | 
 | 236 | // Returns whether this module should have the dex file stored uncompressed in the APK. | 
 | 237 | func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool { | 
| Cole Faust | 2f1da16 | 2023-04-17 15:06:56 -0700 | [diff] [blame] | 238 | 	if ctx.Config().UnbundledBuild() || proptools.Bool(a.properties.Preprocessed) { | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 239 | 		return false | 
 | 240 | 	} | 
 | 241 |  | 
| Ulya Trafimovich | 0061c0d | 2021-09-01 15:40:38 +0100 | [diff] [blame] | 242 | 	// Uncompress dex in APKs of priv-apps if and only if DONT_UNCOMPRESS_PRIV_APPS_DEXS is false. | 
 | 243 | 	if a.Privileged() { | 
 | 244 | 		return ctx.Config().UncompressPrivAppDex() | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 245 | 	} | 
 | 246 |  | 
 | 247 | 	return shouldUncompressDex(ctx, &a.dexpreopter) | 
 | 248 | } | 
 | 249 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 250 | func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
 | 251 | 	a.generateAndroidBuildActions(ctx) | 
 | 252 | } | 
 | 253 |  | 
 | 254 | func (a *AndroidAppImport) InstallApkName() string { | 
 | 255 | 	return a.BaseModuleName() | 
 | 256 | } | 
 | 257 |  | 
 | 258 | func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext) { | 
| Cole Faust | d580613 | 2023-04-13 15:43:53 -0700 | [diff] [blame] | 259 | 	if a.Name() == "prebuilt_framework-res" { | 
 | 260 | 		ctx.ModuleErrorf("prebuilt_framework-res found. This used to have special handling in soong, but was removed due to prebuilt_framework-res no longer existing. This check is to ensure it doesn't come back without readding the special handling.") | 
 | 261 | 	} | 
 | 262 |  | 
| Colin Cross | ff694a8 | 2023-12-13 15:54:49 -0800 | [diff] [blame] | 263 | 	apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 264 | 	if !apexInfo.IsForPlatform() { | 
 | 265 | 		a.hideApexVariantFromMake = true | 
 | 266 | 	} | 
 | 267 |  | 
| Cole Faust | 6158528 | 2023-07-14 16:23:39 -0700 | [diff] [blame] | 268 | 	if Bool(a.properties.Preprocessed) { | 
 | 269 | 		if a.properties.Presigned != nil && !*a.properties.Presigned { | 
 | 270 | 			ctx.ModuleErrorf("Setting preprocessed: true implies presigned: true, so you cannot set presigned to false") | 
 | 271 | 		} | 
 | 272 | 		t := true | 
 | 273 | 		a.properties.Presigned = &t | 
 | 274 | 	} | 
 | 275 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 276 | 	numCertPropsSet := 0 | 
 | 277 | 	if String(a.properties.Certificate) != "" { | 
 | 278 | 		numCertPropsSet++ | 
 | 279 | 	} | 
 | 280 | 	if Bool(a.properties.Presigned) { | 
 | 281 | 		numCertPropsSet++ | 
 | 282 | 	} | 
 | 283 | 	if Bool(a.properties.Default_dev_cert) { | 
 | 284 | 		numCertPropsSet++ | 
 | 285 | 	} | 
 | 286 | 	if numCertPropsSet != 1 { | 
| Cole Faust | 6158528 | 2023-07-14 16:23:39 -0700 | [diff] [blame] | 287 | 		ctx.ModuleErrorf("One and only one of certficate, presigned (implied by preprocessed), and default_dev_cert properties must be set") | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 288 | 	} | 
 | 289 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 290 | 	// TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK | 
 | 291 | 	// TODO: LOCAL_PACKAGE_SPLITS | 
 | 292 |  | 
 | 293 | 	srcApk := a.prebuilt.SingleSourcePath(ctx) | 
 | 294 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 295 | 	// TODO: Install or embed JNI libraries | 
 | 296 |  | 
 | 297 | 	// Uncompress JNI libraries in the apk | 
 | 298 | 	jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk") | 
 | 299 | 	a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath) | 
 | 300 |  | 
| Spandan Das | d1fac64 | 2021-05-18 17:01:41 +0000 | [diff] [blame] | 301 | 	var pathFragments []string | 
 | 302 | 	relInstallPath := String(a.properties.Relative_install_path) | 
| Bill Peckham | a036da9 | 2021-01-08 16:09:09 -0800 | [diff] [blame] | 303 |  | 
| Cole Faust | d580613 | 2023-04-13 15:43:53 -0700 | [diff] [blame] | 304 | 	if Bool(a.properties.Privileged) { | 
| Spandan Das | d1fac64 | 2021-05-18 17:01:41 +0000 | [diff] [blame] | 305 | 		pathFragments = []string{"priv-app", relInstallPath, a.BaseModuleName()} | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 306 | 	} else if ctx.InstallInTestcases() { | 
| Spandan Das | d1fac64 | 2021-05-18 17:01:41 +0000 | [diff] [blame] | 307 | 		pathFragments = []string{relInstallPath, a.BaseModuleName(), ctx.DeviceConfig().DeviceArch()} | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 308 | 	} else { | 
| Spandan Das | d1fac64 | 2021-05-18 17:01:41 +0000 | [diff] [blame] | 309 | 		pathFragments = []string{"app", relInstallPath, a.BaseModuleName()} | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 310 | 	} | 
 | 311 |  | 
| Spandan Das | d1fac64 | 2021-05-18 17:01:41 +0000 | [diff] [blame] | 312 | 	installDir := android.PathForModuleInstall(ctx, pathFragments...) | 
| Ulya Trafimovich | 76b0852 | 2021-01-14 17:52:43 +0000 | [diff] [blame] | 313 | 	a.dexpreopter.isApp = true | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 314 | 	a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk") | 
 | 315 | 	a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned) | 
 | 316 | 	a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx) | 
 | 317 |  | 
 | 318 | 	a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries() | 
 | 319 | 	a.dexpreopter.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx) | 
 | 320 |  | 
| Ulya Trafimovich | fe927a2 | 2021-02-26 14:36:48 +0000 | [diff] [blame] | 321 | 	if a.usesLibrary.enforceUsesLibraries() { | 
| Cole Faust | 2f1da16 | 2023-04-17 15:06:56 -0700 | [diff] [blame] | 322 | 		a.usesLibrary.verifyUsesLibrariesAPK(ctx, srcApk) | 
| Ulya Trafimovich | fe927a2 | 2021-02-26 14:36:48 +0000 | [diff] [blame] | 323 | 	} | 
 | 324 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 325 | 	a.dexpreopter.dexpreopt(ctx, jnisUncompressed) | 
 | 326 | 	if a.dexpreopter.uncompressedDex { | 
 | 327 | 		dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk") | 
| Cole Faust | 4ec178c | 2023-01-13 12:03:38 -0800 | [diff] [blame] | 328 | 		ctx.Build(pctx, android.BuildParams{ | 
 | 329 | 			Rule:   uncompressDexRule, | 
 | 330 | 			Input:  jnisUncompressed, | 
 | 331 | 			Output: dexUncompressed, | 
 | 332 | 		}) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 333 | 		jnisUncompressed = dexUncompressed | 
 | 334 | 	} | 
 | 335 |  | 
 | 336 | 	apkFilename := proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".apk") | 
 | 337 |  | 
 | 338 | 	// TODO: Handle EXTERNAL | 
 | 339 |  | 
 | 340 | 	// Sign or align the package if package has not been preprocessed | 
| Bill Peckham | a036da9 | 2021-01-08 16:09:09 -0800 | [diff] [blame] | 341 |  | 
| Cole Faust | 2f1da16 | 2023-04-17 15:06:56 -0700 | [diff] [blame] | 342 | 	if proptools.Bool(a.properties.Preprocessed) { | 
| Cole Faust | 9c5c09f | 2023-09-06 16:11:44 -0700 | [diff] [blame] | 343 | 		validationStamp := a.validatePresignedApk(ctx, srcApk) | 
 | 344 | 		output := android.PathForModuleOut(ctx, apkFilename) | 
 | 345 | 		ctx.Build(pctx, android.BuildParams{ | 
 | 346 | 			Rule:       android.Cp, | 
 | 347 | 			Input:      srcApk, | 
 | 348 | 			Output:     output, | 
 | 349 | 			Validation: validationStamp, | 
 | 350 | 		}) | 
| Cole Faust | 2f1da16 | 2023-04-17 15:06:56 -0700 | [diff] [blame] | 351 | 		a.outputFile = output | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 352 | 		a.certificate = PresignedCertificate | 
 | 353 | 	} else if !Bool(a.properties.Presigned) { | 
 | 354 | 		// If the certificate property is empty at this point, default_dev_cert must be set to true. | 
 | 355 | 		// Which makes processMainCert's behavior for the empty cert string WAI. | 
| Cole Faust | 6158528 | 2023-07-14 16:23:39 -0700 | [diff] [blame] | 356 | 		_, _, certificates := collectAppDeps(ctx, a, false, false) | 
| Colin Cross | bc2c8a7 | 2022-09-14 12:45:42 -0700 | [diff] [blame] | 357 | 		a.certificate, certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 358 | 		signed := android.PathForModuleOut(ctx, "signed", apkFilename) | 
 | 359 | 		var lineageFile android.Path | 
 | 360 | 		if lineage := String(a.properties.Lineage); lineage != "" { | 
 | 361 | 			lineageFile = android.PathForModuleSrc(ctx, lineage) | 
 | 362 | 		} | 
| Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 363 |  | 
 | 364 | 		rotationMinSdkVersion := String(a.properties.RotationMinSdkVersion) | 
 | 365 |  | 
 | 366 | 		SignAppPackage(ctx, signed, jnisUncompressed, certificates, nil, lineageFile, rotationMinSdkVersion) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 367 | 		a.outputFile = signed | 
 | 368 | 	} else { | 
| Cole Faust | 9c5c09f | 2023-09-06 16:11:44 -0700 | [diff] [blame] | 369 | 		validationStamp := a.validatePresignedApk(ctx, srcApk) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 370 | 		alignedApk := android.PathForModuleOut(ctx, "zip-aligned", apkFilename) | 
| Cole Faust | 9c5c09f | 2023-09-06 16:11:44 -0700 | [diff] [blame] | 371 | 		TransformZipAlign(ctx, alignedApk, jnisUncompressed, []android.Path{validationStamp}) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 372 | 		a.outputFile = alignedApk | 
 | 373 | 		a.certificate = PresignedCertificate | 
 | 374 | 	} | 
 | 375 |  | 
 | 376 | 	// TODO: Optionally compress the output apk. | 
 | 377 |  | 
 | 378 | 	if apexInfo.IsForPlatform() { | 
 | 379 | 		a.installPath = ctx.InstallFile(installDir, apkFilename, a.outputFile) | 
| Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 380 | 		artifactPath := android.PathForModuleSrc(ctx, *a.properties.Apk) | 
 | 381 | 		a.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, artifactPath, a.installPath) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 382 | 	} | 
| LaMont Jones | afe7baf | 2024-01-09 22:47:39 +0000 | [diff] [blame] | 383 | 	android.CollectDependencyAconfigFiles(ctx, &a.mergedAconfigFiles) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 384 |  | 
 | 385 | 	// TODO: androidmk converter jni libs | 
 | 386 | } | 
 | 387 |  | 
| Cole Faust | 9c5c09f | 2023-09-06 16:11:44 -0700 | [diff] [blame] | 388 | func (a *AndroidAppImport) validatePresignedApk(ctx android.ModuleContext, srcApk android.Path) android.Path { | 
 | 389 | 	stamp := android.PathForModuleOut(ctx, "validated-prebuilt", "check.stamp") | 
 | 390 | 	var extraArgs []string | 
| Cole Faust | 93b89b4 | 2023-07-20 17:31:16 -0700 | [diff] [blame] | 391 | 	if a.Privileged() { | 
| Cole Faust | 9c5c09f | 2023-09-06 16:11:44 -0700 | [diff] [blame] | 392 | 		extraArgs = append(extraArgs, "--privileged") | 
 | 393 | 	} | 
 | 394 | 	if proptools.Bool(a.properties.Skip_preprocessed_apk_checks) { | 
 | 395 | 		extraArgs = append(extraArgs, "--skip-preprocessed-apk-checks") | 
 | 396 | 	} | 
 | 397 | 	if proptools.Bool(a.properties.Preprocessed) { | 
 | 398 | 		extraArgs = append(extraArgs, "--preprocessed") | 
| Cole Faust | 93b89b4 | 2023-07-20 17:31:16 -0700 | [diff] [blame] | 399 | 	} | 
 | 400 |  | 
| Cole Faust | 2f1da16 | 2023-04-17 15:06:56 -0700 | [diff] [blame] | 401 | 	ctx.Build(pctx, android.BuildParams{ | 
| Cole Faust | 9c5c09f | 2023-09-06 16:11:44 -0700 | [diff] [blame] | 402 | 		Rule:   checkPresignedApkRule, | 
| Cole Faust | 6158528 | 2023-07-14 16:23:39 -0700 | [diff] [blame] | 403 | 		Input:  srcApk, | 
| Cole Faust | 9c5c09f | 2023-09-06 16:11:44 -0700 | [diff] [blame] | 404 | 		Output: stamp, | 
 | 405 | 		Args: map[string]string{ | 
 | 406 | 			"extraArgs": strings.Join(extraArgs, " "), | 
 | 407 | 		}, | 
| Cole Faust | 6158528 | 2023-07-14 16:23:39 -0700 | [diff] [blame] | 408 | 	}) | 
| Cole Faust | 9c5c09f | 2023-09-06 16:11:44 -0700 | [diff] [blame] | 409 | 	return stamp | 
| Cole Faust | 6158528 | 2023-07-14 16:23:39 -0700 | [diff] [blame] | 410 | } | 
 | 411 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 412 | func (a *AndroidAppImport) Prebuilt() *android.Prebuilt { | 
 | 413 | 	return &a.prebuilt | 
 | 414 | } | 
 | 415 |  | 
 | 416 | func (a *AndroidAppImport) Name() string { | 
 | 417 | 	return a.prebuilt.Name(a.ModuleBase.Name()) | 
 | 418 | } | 
 | 419 |  | 
 | 420 | func (a *AndroidAppImport) OutputFile() android.Path { | 
 | 421 | 	return a.outputFile | 
 | 422 | } | 
 | 423 |  | 
| Colin Cross | 5368d0b | 2023-07-07 11:32:32 -0700 | [diff] [blame] | 424 | func (a *AndroidAppImport) OutputFiles(tag string) (android.Paths, error) { | 
 | 425 | 	switch tag { | 
 | 426 | 	case "": | 
 | 427 | 		return []android.Path{a.outputFile}, nil | 
 | 428 | 	default: | 
 | 429 | 		return nil, fmt.Errorf("unsupported module reference tag %q", tag) | 
 | 430 | 	} | 
 | 431 | } | 
 | 432 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 433 | func (a *AndroidAppImport) JacocoReportClassesFile() android.Path { | 
 | 434 | 	return nil | 
 | 435 | } | 
 | 436 |  | 
 | 437 | func (a *AndroidAppImport) Certificate() Certificate { | 
 | 438 | 	return a.certificate | 
 | 439 | } | 
 | 440 |  | 
| Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 441 | func (a *AndroidAppImport) ProvenanceMetaDataFile() android.OutputPath { | 
 | 442 | 	return a.provenanceMetaDataFile | 
 | 443 | } | 
 | 444 |  | 
| Andrei Onea | 580636b | 2022-08-17 16:53:46 +0000 | [diff] [blame] | 445 | func (a *AndroidAppImport) PrivAppAllowlist() android.OptionalPath { | 
 | 446 | 	return android.OptionalPath{} | 
 | 447 | } | 
 | 448 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 449 | var dpiVariantGroupType reflect.Type | 
 | 450 | var archVariantGroupType reflect.Type | 
 | 451 | var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"} | 
 | 452 |  | 
 | 453 | func initAndroidAppImportVariantGroupTypes() { | 
 | 454 | 	dpiVariantGroupType = createVariantGroupType(supportedDpis, "Dpi_variants") | 
 | 455 |  | 
 | 456 | 	archNames := make([]string, len(android.ArchTypeList())) | 
 | 457 | 	for i, archType := range android.ArchTypeList() { | 
 | 458 | 		archNames[i] = archType.Name | 
 | 459 | 	} | 
 | 460 | 	archVariantGroupType = createVariantGroupType(archNames, "Arch") | 
 | 461 | } | 
 | 462 |  | 
 | 463 | // Populates all variant struct properties at creation time. | 
 | 464 | func (a *AndroidAppImport) populateAllVariantStructs() { | 
 | 465 | 	a.dpiVariants = reflect.New(dpiVariantGroupType).Interface() | 
 | 466 | 	a.AddProperties(a.dpiVariants) | 
 | 467 |  | 
 | 468 | 	a.archVariants = reflect.New(archVariantGroupType).Interface() | 
 | 469 | 	a.AddProperties(a.archVariants) | 
 | 470 | } | 
 | 471 |  | 
 | 472 | func (a *AndroidAppImport) Privileged() bool { | 
 | 473 | 	return Bool(a.properties.Privileged) | 
 | 474 | } | 
 | 475 |  | 
 | 476 | func (a *AndroidAppImport) DepIsInSameApex(_ android.BaseModuleContext, _ android.Module) bool { | 
 | 477 | 	// android_app_import might have extra dependencies via uses_libs property. | 
 | 478 | 	// Don't track the dependency as we don't automatically add those libraries | 
 | 479 | 	// to the classpath. It should be explicitly added to java_libs property of APEX | 
 | 480 | 	return false | 
 | 481 | } | 
 | 482 |  | 
| Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 483 | func (a *AndroidAppImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { | 
 | 484 | 	return android.SdkSpecPrivate | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 485 | } | 
 | 486 |  | 
| Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 487 | func (a *AndroidAppImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { | 
 | 488 | 	return android.SdkSpecPrivate.ApiLevel | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 489 | } | 
 | 490 |  | 
| Colin Cross | 8355c15 | 2021-08-10 19:24:07 -0700 | [diff] [blame] | 491 | func (a *AndroidAppImport) LintDepSets() LintDepSets { | 
 | 492 | 	return LintDepSets{} | 
 | 493 | } | 
 | 494 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 495 | var _ android.ApexModule = (*AndroidAppImport)(nil) | 
 | 496 |  | 
 | 497 | // Implements android.ApexModule | 
 | 498 | func (j *AndroidAppImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext, | 
 | 499 | 	sdkVersion android.ApiLevel) error { | 
 | 500 | 	// Do not check for prebuilts against the min_sdk_version of enclosing APEX | 
 | 501 | 	return nil | 
 | 502 | } | 
 | 503 |  | 
 | 504 | func createVariantGroupType(variants []string, variantGroupName string) reflect.Type { | 
 | 505 | 	props := reflect.TypeOf((*AndroidAppImportProperties)(nil)) | 
 | 506 |  | 
 | 507 | 	variantFields := make([]reflect.StructField, len(variants)) | 
 | 508 | 	for i, variant := range variants { | 
 | 509 | 		variantFields[i] = reflect.StructField{ | 
 | 510 | 			Name: proptools.FieldNameForProperty(variant), | 
 | 511 | 			Type: props, | 
 | 512 | 		} | 
 | 513 | 	} | 
 | 514 |  | 
 | 515 | 	variantGroupStruct := reflect.StructOf(variantFields) | 
 | 516 | 	return reflect.StructOf([]reflect.StructField{ | 
 | 517 | 		{ | 
 | 518 | 			Name: variantGroupName, | 
 | 519 | 			Type: variantGroupStruct, | 
 | 520 | 		}, | 
 | 521 | 	}) | 
 | 522 | } | 
 | 523 |  | 
 | 524 | // android_app_import imports a prebuilt apk with additional processing specified in the module. | 
 | 525 | // DPI-specific apk source files can be specified using dpi_variants. Example: | 
 | 526 | // | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 527 | //	android_app_import { | 
 | 528 | //	    name: "example_import", | 
 | 529 | //	    apk: "prebuilts/example.apk", | 
 | 530 | //	    dpi_variants: { | 
 | 531 | //	        mdpi: { | 
 | 532 | //	            apk: "prebuilts/example_mdpi.apk", | 
 | 533 | //	        }, | 
 | 534 | //	        xhdpi: { | 
 | 535 | //	            apk: "prebuilts/example_xhdpi.apk", | 
 | 536 | //	        }, | 
 | 537 | //	    }, | 
 | 538 | //	    presigned: true, | 
 | 539 | //	} | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 540 | func AndroidAppImportFactory() android.Module { | 
 | 541 | 	module := &AndroidAppImport{} | 
 | 542 | 	module.AddProperties(&module.properties) | 
 | 543 | 	module.AddProperties(&module.dexpreoptProperties) | 
 | 544 | 	module.AddProperties(&module.usesLibrary.usesLibraryProperties) | 
 | 545 | 	module.populateAllVariantStructs() | 
 | 546 | 	android.AddLoadHook(module, func(ctx android.LoadHookContext) { | 
 | 547 | 		module.processVariants(ctx) | 
 | 548 | 	}) | 
 | 549 |  | 
 | 550 | 	android.InitApexModule(module) | 
 | 551 | 	android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) | 
 | 552 | 	android.InitDefaultableModule(module) | 
 | 553 | 	android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk") | 
 | 554 |  | 
| Ulya Trafimovich | 22890c4 | 2021-01-05 12:04:17 +0000 | [diff] [blame] | 555 | 	module.usesLibrary.enforce = true | 
 | 556 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 557 | 	return module | 
 | 558 | } | 
 | 559 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 560 | type AndroidTestImport struct { | 
 | 561 | 	AndroidAppImport | 
 | 562 |  | 
| Jiyong Park | 2f83b31 | 2022-10-20 20:18:35 +0900 | [diff] [blame] | 563 | 	testProperties struct { | 
 | 564 | 		// list of compatibility suites (for example "cts", "vts") that the module should be | 
 | 565 | 		// installed into. | 
 | 566 | 		Test_suites []string `android:"arch_variant"` | 
 | 567 |  | 
 | 568 | 		// list of files or filegroup modules that provide data that should be installed alongside | 
 | 569 | 		// the test | 
 | 570 | 		Data []string `android:"path"` | 
 | 571 |  | 
 | 572 | 		// Install the test into a folder named for the module in all test suites. | 
 | 573 | 		Per_testcase_directory *bool | 
 | 574 | 	} | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 575 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 576 | 	data android.Paths | 
 | 577 | } | 
 | 578 |  | 
 | 579 | func (a *AndroidTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 580 | 	a.generateAndroidBuildActions(ctx) | 
 | 581 |  | 
 | 582 | 	a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data) | 
 | 583 | } | 
 | 584 |  | 
 | 585 | func (a *AndroidTestImport) InstallInTestcases() bool { | 
 | 586 | 	return true | 
 | 587 | } | 
 | 588 |  | 
 | 589 | // android_test_import imports a prebuilt test apk with additional processing specified in the | 
 | 590 | // module. DPI or arch variant configurations can be made as with android_app_import. | 
 | 591 | func AndroidTestImportFactory() android.Module { | 
 | 592 | 	module := &AndroidTestImport{} | 
 | 593 | 	module.AddProperties(&module.properties) | 
 | 594 | 	module.AddProperties(&module.dexpreoptProperties) | 
 | 595 | 	module.AddProperties(&module.testProperties) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 596 | 	module.populateAllVariantStructs() | 
 | 597 | 	android.AddLoadHook(module, func(ctx android.LoadHookContext) { | 
 | 598 | 		module.processVariants(ctx) | 
 | 599 | 	}) | 
 | 600 |  | 
 | 601 | 	module.dexpreopter.isTest = true | 
 | 602 |  | 
 | 603 | 	android.InitApexModule(module) | 
 | 604 | 	android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) | 
 | 605 | 	android.InitDefaultableModule(module) | 
 | 606 | 	android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk") | 
 | 607 |  | 
 | 608 | 	return module | 
 | 609 | } |