Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 1 | // Copyright 2018 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 | import ( |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 18 | "fmt" |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 19 | "strings" |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 20 | |
| 21 | "github.com/google/blueprint" |
| 22 | |
| 23 | "android/soong/android" |
| 24 | ) |
| 25 | |
| 26 | var manifestFixerRule = pctx.AndroidStaticRule("manifestFixer", |
| 27 | blueprint.RuleParams{ |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 28 | Command: `${config.ManifestFixerCmd} ` + |
| 29 | `--minSdkVersion ${minSdkVersion} ` + |
| 30 | `--targetSdkVersion ${targetSdkVersion} ` + |
| 31 | `--raise-min-sdk-version ` + |
| 32 | `$args $in $out`, |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 33 | CommandDeps: []string{"${config.ManifestFixerCmd}"}, |
| 34 | }, |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 35 | "minSdkVersion", "targetSdkVersion", "args") |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 36 | |
| 37 | var manifestMergerRule = pctx.AndroidStaticRule("manifestMerger", |
| 38 | blueprint.RuleParams{ |
Colin Cross | aa1c6f1 | 2019-04-19 16:25:38 -0700 | [diff] [blame] | 39 | Command: `${config.ManifestMergerCmd} $args --main $in $libs --out $out`, |
Colin Cross | 87ecbfe | 2019-02-26 22:14:04 -0800 | [diff] [blame] | 40 | CommandDeps: []string{"${config.ManifestMergerCmd}"}, |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 41 | }, |
Colin Cross | aa1c6f1 | 2019-04-19 16:25:38 -0700 | [diff] [blame] | 42 | "args", "libs") |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 43 | |
Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 44 | // These two libs are added as optional dependencies (<uses-library> with |
| 45 | // android:required set to false). This is because they haven't existed in pre-P |
| 46 | // devices, but classes in them were in bootclasspath jars, etc. So making them |
| 47 | // hard dependencies (android:required=true) would prevent apps from being |
| 48 | // installed to such legacy devices. |
| 49 | var optionalUsesLibs = []string{ |
| 50 | "android.test.base", |
| 51 | "android.test.mock", |
| 52 | } |
| 53 | |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 54 | // Uses manifest_fixer.py to inject minSdkVersion, etc. into an AndroidManifest.xml |
Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 55 | func manifestFixer(ctx android.ModuleContext, manifest android.Path, sdkContext sdkContext, sdkLibraries []string, |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 56 | isLibrary, useEmbeddedNativeLibs, usesNonSdkApis, useEmbeddedDex, hasNoCode bool) android.Path { |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 57 | |
| 58 | var args []string |
| 59 | if isLibrary { |
| 60 | args = append(args, "--library") |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 61 | } else { |
| 62 | minSdkVersion, err := sdkVersionToNumber(ctx, sdkContext.minSdkVersion()) |
| 63 | if err != nil { |
| 64 | ctx.ModuleErrorf("invalid minSdkVersion: %s", err) |
| 65 | } |
| 66 | if minSdkVersion >= 23 { |
Sasha Smundak | 6ad7725 | 2019-05-01 13:16:22 -0700 | [diff] [blame] | 67 | args = append(args, fmt.Sprintf("--extract-native-libs=%v", !useEmbeddedNativeLibs)) |
| 68 | } else if useEmbeddedNativeLibs { |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 69 | ctx.ModuleErrorf("module attempted to store uncompressed native libraries, but minSdkVersion=%d doesn't support it", |
| 70 | minSdkVersion) |
| 71 | } |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 72 | } |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 73 | |
David Brazdil | d25060a | 2019-02-18 18:24:16 +0000 | [diff] [blame] | 74 | if usesNonSdkApis { |
| 75 | args = append(args, "--uses-non-sdk-api") |
| 76 | } |
| 77 | |
Colin Cross | 46abdad | 2019-02-07 13:07:08 -0800 | [diff] [blame] | 78 | if useEmbeddedDex { |
Sasha Smundak | d2e5361 | 2019-05-14 17:03:24 -0700 | [diff] [blame] | 79 | args = append(args, "--use-embedded-dex") |
Colin Cross | 46abdad | 2019-02-07 13:07:08 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 82 | for _, usesLib := range sdkLibraries { |
| 83 | if inList(usesLib, optionalUsesLibs) { |
| 84 | args = append(args, "--optional-uses-library", usesLib) |
| 85 | } else { |
| 86 | args = append(args, "--uses-library", usesLib) |
| 87 | } |
| 88 | } |
| 89 | |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 90 | if hasNoCode { |
| 91 | args = append(args, "--has-no-code") |
| 92 | } |
| 93 | |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 94 | var deps android.Paths |
| 95 | targetSdkVersion := sdkVersionOrDefault(ctx, sdkContext.targetSdkVersion()) |
| 96 | if targetSdkVersion == ctx.Config().PlatformSdkCodename() && |
| 97 | ctx.Config().UnbundledBuild() && |
| 98 | !ctx.Config().UnbundledBuildUsePrebuiltSdks() && |
| 99 | ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") { |
Jiyong Park | 71b519d | 2019-04-18 17:25:49 +0900 | [diff] [blame] | 100 | apiFingerprint := ApiFingerprintPath(ctx) |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 101 | targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String()) |
| 102 | deps = append(deps, apiFingerprint) |
| 103 | } |
| 104 | |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 105 | fixedManifest := android.PathForModuleOut(ctx, "manifest_fixer", "AndroidManifest.xml") |
| 106 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 107 | Rule: manifestFixerRule, |
| 108 | Description: "fix manifest", |
| 109 | Input: manifest, |
| 110 | Implicits: deps, |
| 111 | Output: fixedManifest, |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 112 | Args: map[string]string{ |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 113 | "minSdkVersion": sdkVersionOrDefault(ctx, sdkContext.minSdkVersion()), |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 114 | "targetSdkVersion": targetSdkVersion, |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 115 | "args": strings.Join(args, " "), |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 116 | }, |
| 117 | }) |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 118 | |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 119 | return fixedManifest |
| 120 | } |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 121 | |
Colin Cross | aa1c6f1 | 2019-04-19 16:25:38 -0700 | [diff] [blame] | 122 | func manifestMerger(ctx android.ModuleContext, manifest android.Path, staticLibManifests android.Paths, |
| 123 | isLibrary bool) android.Path { |
| 124 | |
| 125 | var args string |
| 126 | if !isLibrary { |
| 127 | // Follow Gradle's behavior, only pass --remove-tools-declarations when merging app manifests. |
| 128 | args = "--remove-tools-declarations" |
| 129 | } |
| 130 | |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 131 | mergedManifest := android.PathForModuleOut(ctx, "manifest_merger", "AndroidManifest.xml") |
| 132 | ctx.Build(pctx, android.BuildParams{ |
| 133 | Rule: manifestMergerRule, |
| 134 | Description: "merge manifest", |
| 135 | Input: manifest, |
| 136 | Implicits: staticLibManifests, |
| 137 | Output: mergedManifest, |
| 138 | Args: map[string]string{ |
| 139 | "libs": android.JoinWithPrefix(staticLibManifests.Strings(), "--libs "), |
Colin Cross | aa1c6f1 | 2019-04-19 16:25:38 -0700 | [diff] [blame] | 140 | "args": args, |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 141 | }, |
| 142 | }) |
| 143 | |
| 144 | return mergedManifest |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 145 | } |