From 5446e886578fa72367addeca884e608160fc902e Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Wed, 22 May 2019 10:46:27 -0700 Subject: [PATCH] Add tags for SDK libraries to AndroidManifest.xml Port I4e7a9bdc5bf845af85168abf55f8063900bacc72 and Ieb7c1429077aaf6899e214a217d9455d95dbfea6 from Make to Soong to support adding tags to AndroidManifest.xml for transitive java_sdk_library dependencies and android.test.base and android.test.mock. Fixes: 133306356 Test: m checkbuild Change-Id: I23763bb689c474d25d04a418df1c46e7f0d807af --- java/aar.go | 29 +++++++++++++++++++++++------ java/android_manifest.go | 20 +++++++++++++++++++- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/java/aar.go b/java/aar.go index 7332e76b2..65a7c2a5e 100644 --- a/java/aar.go +++ b/java/aar.go @@ -84,6 +84,7 @@ type aapt struct { useEmbeddedNativeLibs bool useEmbeddedDex bool usesNonSdkApis bool + sdkLibraries []string splitNames []string splits []split @@ -194,13 +195,15 @@ func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkContext sdkContext) { } func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext sdkContext, extraLinkFlags ...string) { - transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, libDeps, libFlags := aaptLibs(ctx, sdkContext) + + transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, libDeps, libFlags, sdkLibraries := + aaptLibs(ctx, sdkContext) // App manifest file manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml") manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile) - manifestPath := manifestFixer(ctx, manifestSrcPath, sdkContext, + manifestPath := manifestFixer(ctx, manifestSrcPath, sdkContext, sdkLibraries, a.isLibrary, a.useEmbeddedNativeLibs, a.usesNonSdkApis, a.useEmbeddedDex) a.transitiveManifestPaths = append(android.Paths{manifestPath}, transitiveStaticLibManifests...) @@ -303,7 +306,7 @@ func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext sdkContext, ex // aaptLibs collects libraries from dependencies and sdk_version and converts them into paths func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext) (transitiveStaticLibs, transitiveStaticLibManifests android.Paths, - staticRRODirs []rroDir, deps android.Paths, flags []string) { + staticRRODirs []rroDir, deps android.Paths, flags []string, sdkLibraries []string) { var sharedLibs android.Paths @@ -322,7 +325,16 @@ func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext) (transitiveStati switch ctx.OtherModuleDependencyTag(module) { case instrumentationForTag: // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2. - case libTag, frameworkResTag: + case libTag: + if exportPackage != nil { + sharedLibs = append(sharedLibs, exportPackage) + } + + if _, ok := module.(SdkLibraryDependency); ok { + sdkLibraries = append(sdkLibraries, ctx.OtherModuleName(module)) + } + + case frameworkResTag: if exportPackage != nil { sharedLibs = append(sharedLibs, exportPackage) } @@ -331,6 +343,7 @@ func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext) (transitiveStati transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...) transitiveStaticLibs = append(transitiveStaticLibs, exportPackage) transitiveStaticLibManifests = append(transitiveStaticLibManifests, aarDep.ExportedManifests()...) + sdkLibraries = append(sdkLibraries, aarDep.ExportedSdkLibs()...) outer: for _, d := range aarDep.ExportedRRODirs() { @@ -358,8 +371,9 @@ func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext) (transitiveStati transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs) transitiveStaticLibManifests = android.FirstUniquePaths(transitiveStaticLibManifests) + sdkLibraries = android.FirstUniqueStrings(sdkLibraries) - return transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, deps, flags + return transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, deps, flags, sdkLibraries } type AndroidLibrary struct { @@ -393,6 +407,7 @@ func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) { func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) { a.aapt.isLibrary = true + a.aapt.sdkLibraries = a.exportedSdkLibs a.aapt.buildActions(ctx, sdkContext(a)) ctx.CheckbuildFile(a.proguardOptionsFile) @@ -603,10 +618,12 @@ func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { linkFlags = append(linkFlags, "--manifest "+a.manifest.String()) linkDeps = append(linkDeps, a.manifest) - transitiveStaticLibs, staticLibManifests, staticRRODirs, libDeps, libFlags := aaptLibs(ctx, sdkContext(a)) + transitiveStaticLibs, staticLibManifests, staticRRODirs, libDeps, libFlags, sdkLibraries := + aaptLibs(ctx, sdkContext(a)) _ = staticLibManifests _ = staticRRODirs + _ = sdkLibraries linkDeps = append(linkDeps, libDeps...) linkFlags = append(linkFlags, libFlags...) diff --git a/java/android_manifest.go b/java/android_manifest.go index 5d3cfa5eb..b5921be77 100644 --- a/java/android_manifest.go +++ b/java/android_manifest.go @@ -41,8 +41,18 @@ var manifestMergerRule = pctx.AndroidStaticRule("manifestMerger", }, "args", "libs") +// These two libs are added as optional dependencies ( with +// android:required set to false). This is because they haven't existed in pre-P +// devices, but classes in them were in bootclasspath jars, etc. So making them +// hard dependencies (android:required=true) would prevent apps from being +// installed to such legacy devices. +var optionalUsesLibs = []string{ + "android.test.base", + "android.test.mock", +} + // Uses manifest_fixer.py to inject minSdkVersion, etc. into an AndroidManifest.xml -func manifestFixer(ctx android.ModuleContext, manifest android.Path, sdkContext sdkContext, +func manifestFixer(ctx android.ModuleContext, manifest android.Path, sdkContext sdkContext, sdkLibraries []string, isLibrary, useEmbeddedNativeLibs, usesNonSdkApis, useEmbeddedDex bool) android.Path { var args []string @@ -69,6 +79,14 @@ func manifestFixer(ctx android.ModuleContext, manifest android.Path, sdkContext args = append(args, "--use-embedded-dex") } + for _, usesLib := range sdkLibraries { + if inList(usesLib, optionalUsesLibs) { + args = append(args, "--optional-uses-library", usesLib) + } else { + args = append(args, "--uses-library", usesLib) + } + } + var deps android.Paths targetSdkVersion := sdkVersionOrDefault(ctx, sdkContext.targetSdkVersion()) if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&