blob: dc7a3fc7e2e0b6dff47775a7bc025caa4f959dac [file] [log] [blame]
Colin Cross31656952018-05-24 16:11:20 -07001// 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
15package java
16
17import (
Colin Crosse4246ab2019-02-05 21:55:21 -080018 "fmt"
Colin Cross1b6a3cf2018-07-24 14:51:30 -070019 "strings"
Colin Cross31656952018-05-24 16:11:20 -070020
21 "github.com/google/blueprint"
22
23 "android/soong/android"
24)
25
26var manifestFixerRule = pctx.AndroidStaticRule("manifestFixer",
27 blueprint.RuleParams{
Colin Cross7b59e7b2018-09-10 13:35:13 -070028 Command: `${config.ManifestFixerCmd} ` +
29 `--minSdkVersion ${minSdkVersion} ` +
30 `--targetSdkVersion ${targetSdkVersion} ` +
31 `--raise-min-sdk-version ` +
32 `$args $in $out`,
Colin Cross31656952018-05-24 16:11:20 -070033 CommandDeps: []string{"${config.ManifestFixerCmd}"},
34 },
Colin Cross7b59e7b2018-09-10 13:35:13 -070035 "minSdkVersion", "targetSdkVersion", "args")
Colin Cross31656952018-05-24 16:11:20 -070036
37var manifestMergerRule = pctx.AndroidStaticRule("manifestMerger",
38 blueprint.RuleParams{
Colin Crossaa1c6f12019-04-19 16:25:38 -070039 Command: `${config.ManifestMergerCmd} $args --main $in $libs --out $out`,
Colin Cross87ecbfe2019-02-26 22:14:04 -080040 CommandDeps: []string{"${config.ManifestMergerCmd}"},
Colin Cross31656952018-05-24 16:11:20 -070041 },
Colin Crossaa1c6f12019-04-19 16:25:38 -070042 "args", "libs")
Colin Cross31656952018-05-24 16:11:20 -070043
Colin Cross5446e882019-05-22 10:46:27 -070044// 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.
49var optionalUsesLibs = []string{
50 "android.test.base",
51 "android.test.mock",
52}
53
Colin Cross90c25c62019-04-19 16:22:57 -070054// Uses manifest_fixer.py to inject minSdkVersion, etc. into an AndroidManifest.xml
Colin Cross5446e882019-05-22 10:46:27 -070055func manifestFixer(ctx android.ModuleContext, manifest android.Path, sdkContext sdkContext, sdkLibraries []string,
Jaewoong Jungc27ab662019-05-30 15:51:14 -070056 isLibrary, useEmbeddedNativeLibs, usesNonSdkApis, useEmbeddedDex, hasNoCode bool) android.Path {
Colin Cross1b6a3cf2018-07-24 14:51:30 -070057
58 var args []string
59 if isLibrary {
60 args = append(args, "--library")
Colin Crosse4246ab2019-02-05 21:55:21 -080061 } 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 Smundak6ad77252019-05-01 13:16:22 -070067 args = append(args, fmt.Sprintf("--extract-native-libs=%v", !useEmbeddedNativeLibs))
68 } else if useEmbeddedNativeLibs {
Colin Crosse4246ab2019-02-05 21:55:21 -080069 ctx.ModuleErrorf("module attempted to store uncompressed native libraries, but minSdkVersion=%d doesn't support it",
70 minSdkVersion)
71 }
Colin Cross1b6a3cf2018-07-24 14:51:30 -070072 }
Colin Cross31656952018-05-24 16:11:20 -070073
David Brazdild25060a2019-02-18 18:24:16 +000074 if usesNonSdkApis {
75 args = append(args, "--uses-non-sdk-api")
76 }
77
Colin Cross46abdad2019-02-07 13:07:08 -080078 if useEmbeddedDex {
Sasha Smundakd2e53612019-05-14 17:03:24 -070079 args = append(args, "--use-embedded-dex")
Colin Cross46abdad2019-02-07 13:07:08 -080080 }
81
Colin Cross5446e882019-05-22 10:46:27 -070082 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 Jungc27ab662019-05-30 15:51:14 -070090 if hasNoCode {
91 args = append(args, "--has-no-code")
92 }
93
Colin Cross10932872019-04-18 14:27:12 -070094 var deps android.Paths
95 targetSdkVersion := sdkVersionOrDefault(ctx, sdkContext.targetSdkVersion())
Baligh Uddinf6201372020-01-24 23:15:44 +000096 minSdkVersion := sdkVersionOrDefault(ctx, sdkContext.minSdkVersion())
97 if (UseApiFingerprint(ctx, sdkContext.targetSdkVersion()) ||
98 UseApiFingerprint(ctx, sdkContext.minSdkVersion())) {
99 apiFingerprint := ApiFingerprintPath(ctx)
100 deps = append(deps, apiFingerprint)
Colin Cross10932872019-04-18 14:27:12 -0700101 }
102
Colin Cross31656952018-05-24 16:11:20 -0700103 fixedManifest := android.PathForModuleOut(ctx, "manifest_fixer", "AndroidManifest.xml")
104 ctx.Build(pctx, android.BuildParams{
Colin Cross90c25c62019-04-19 16:22:57 -0700105 Rule: manifestFixerRule,
106 Description: "fix manifest",
107 Input: manifest,
108 Implicits: deps,
109 Output: fixedManifest,
Colin Cross31656952018-05-24 16:11:20 -0700110 Args: map[string]string{
Baligh Uddinf6201372020-01-24 23:15:44 +0000111 "minSdkVersion": minSdkVersion,
Colin Cross10932872019-04-18 14:27:12 -0700112 "targetSdkVersion": targetSdkVersion,
Colin Cross7b59e7b2018-09-10 13:35:13 -0700113 "args": strings.Join(args, " "),
Colin Cross31656952018-05-24 16:11:20 -0700114 },
115 })
Colin Cross31656952018-05-24 16:11:20 -0700116
Colin Cross90c25c62019-04-19 16:22:57 -0700117 return fixedManifest
118}
Colin Cross31656952018-05-24 16:11:20 -0700119
Colin Crossaa1c6f12019-04-19 16:25:38 -0700120func manifestMerger(ctx android.ModuleContext, manifest android.Path, staticLibManifests android.Paths,
121 isLibrary bool) android.Path {
122
123 var args string
124 if !isLibrary {
125 // Follow Gradle's behavior, only pass --remove-tools-declarations when merging app manifests.
126 args = "--remove-tools-declarations"
127 }
128
Colin Cross90c25c62019-04-19 16:22:57 -0700129 mergedManifest := android.PathForModuleOut(ctx, "manifest_merger", "AndroidManifest.xml")
130 ctx.Build(pctx, android.BuildParams{
131 Rule: manifestMergerRule,
132 Description: "merge manifest",
133 Input: manifest,
134 Implicits: staticLibManifests,
135 Output: mergedManifest,
136 Args: map[string]string{
137 "libs": android.JoinWithPrefix(staticLibManifests.Strings(), "--libs "),
Colin Crossaa1c6f12019-04-19 16:25:38 -0700138 "args": args,
Colin Cross90c25c62019-04-19 16:22:57 -0700139 },
140 })
141
142 return mergedManifest
Colin Cross31656952018-05-24 16:11:20 -0700143}