blob: 7772b709052c3454823fac6c2fa29ef6af271e0f [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"
Spandan Dasc8cb0c32021-12-08 17:00:38 +000019 "strconv"
Colin Cross1b6a3cf2018-07-24 14:51:30 -070020 "strings"
Colin Cross31656952018-05-24 16:11:20 -070021
22 "github.com/google/blueprint"
23
24 "android/soong/android"
Ulya Trafimovich31e444e2020-08-14 17:32:16 +010025 "android/soong/dexpreopt"
Colin Cross31656952018-05-24 16:11:20 -070026)
27
28var manifestFixerRule = pctx.AndroidStaticRule("manifestFixer",
29 blueprint.RuleParams{
Colin Cross7b59e7b2018-09-10 13:35:13 -070030 Command: `${config.ManifestFixerCmd} ` +
Colin Cross7b59e7b2018-09-10 13:35:13 -070031 `$args $in $out`,
Colin Cross31656952018-05-24 16:11:20 -070032 CommandDeps: []string{"${config.ManifestFixerCmd}"},
33 },
Gurpreet Singh75d65f32022-01-24 17:44:05 +000034 "args")
Colin Cross31656952018-05-24 16:11:20 -070035
36var manifestMergerRule = pctx.AndroidStaticRule("manifestMerger",
37 blueprint.RuleParams{
Colin Crossaa1c6f12019-04-19 16:25:38 -070038 Command: `${config.ManifestMergerCmd} $args --main $in $libs --out $out`,
Colin Cross87ecbfe2019-02-26 22:14:04 -080039 CommandDeps: []string{"${config.ManifestMergerCmd}"},
Colin Cross31656952018-05-24 16:11:20 -070040 },
Colin Crossaa1c6f12019-04-19 16:25:38 -070041 "args", "libs")
Colin Cross31656952018-05-24 16:11:20 -070042
Spandan Dasc8cb0c32021-12-08 17:00:38 +000043// targetSdkVersion for manifest_fixer
44// When TARGET_BUILD_APPS is not empty, this method returns 10000 for modules targeting an unreleased SDK
45// This enables release builds (that run with TARGET_BUILD_APPS=[val...]) to target APIs that have not yet been finalized as part of an SDK
46func targetSdkVersionForManifestFixer(ctx android.ModuleContext, sdkContext android.SdkContext) string {
47 targetSdkVersionSpec := sdkContext.TargetSdkVersion(ctx)
48 if ctx.Config().UnbundledBuildApps() && targetSdkVersionSpec.ApiLevel.IsPreview() {
49 return strconv.Itoa(android.FutureApiLevel.FinalOrFutureInt())
50 }
51 targetSdkVersion, err := targetSdkVersionSpec.EffectiveVersionString(ctx)
52 if err != nil {
53 ctx.ModuleErrorf("invalid targetSdkVersion: %s", err)
54 }
55 return targetSdkVersion
56}
57
Gurpreet Singh75d65f32022-01-24 17:44:05 +000058type ManifestFixerParams struct {
Gurpreet Singh75d65f32022-01-24 17:44:05 +000059 SdkContext android.SdkContext
60 ClassLoaderContexts dexpreopt.ClassLoaderContextMap
61 IsLibrary bool
62 UseEmbeddedNativeLibs bool
63 UsesNonSdkApis bool
64 UseEmbeddedDex bool
65 HasNoCode bool
66 TestOnly bool
67 LoggingParent string
68}
Colin Cross1b6a3cf2018-07-24 14:51:30 -070069
Gurpreet Singh75d65f32022-01-24 17:44:05 +000070// Uses manifest_fixer.py to inject minSdkVersion, etc. into an AndroidManifest.xml
Gurpreet Singh7deabfa2022-02-10 13:28:35 +000071func ManifestFixer(ctx android.ModuleContext, manifest android.Path,
72 params ManifestFixerParams) android.Path {
Colin Cross1b6a3cf2018-07-24 14:51:30 -070073 var args []string
Gurpreet Singh75d65f32022-01-24 17:44:05 +000074
75 if params.IsLibrary {
Colin Cross1b6a3cf2018-07-24 14:51:30 -070076 args = append(args, "--library")
Gurpreet Singh75d65f32022-01-24 17:44:05 +000077 } else if params.SdkContext != nil {
Gurpreet Singh7deabfa2022-02-10 13:28:35 +000078 minSdkVersion, err := params.SdkContext.MinSdkVersion(ctx).EffectiveVersion(ctx)
Colin Crosse4246ab2019-02-05 21:55:21 -080079 if err != nil {
Gurpreet Singh7deabfa2022-02-10 13:28:35 +000080 ctx.ModuleErrorf("invalid minSdkVersion: %s", err)
Colin Crosse4246ab2019-02-05 21:55:21 -080081 }
Jiyong Park54105c42021-03-31 18:17:53 +090082 if minSdkVersion.FinalOrFutureInt() >= 23 {
Gurpreet Singh75d65f32022-01-24 17:44:05 +000083 args = append(args, fmt.Sprintf("--extract-native-libs=%v", !params.UseEmbeddedNativeLibs))
84 } else if params.UseEmbeddedNativeLibs {
Gurpreet Singh7deabfa2022-02-10 13:28:35 +000085 ctx.ModuleErrorf("module attempted to store uncompressed native libraries, but minSdkVersion=%d doesn't support it",
Colin Crosse4246ab2019-02-05 21:55:21 -080086 minSdkVersion)
87 }
Colin Cross1b6a3cf2018-07-24 14:51:30 -070088 }
Colin Cross31656952018-05-24 16:11:20 -070089
Gurpreet Singh75d65f32022-01-24 17:44:05 +000090 if params.UsesNonSdkApis {
David Brazdild25060a2019-02-18 18:24:16 +000091 args = append(args, "--uses-non-sdk-api")
92 }
93
Gurpreet Singh75d65f32022-01-24 17:44:05 +000094 if params.UseEmbeddedDex {
Sasha Smundakd2e53612019-05-14 17:03:24 -070095 args = append(args, "--use-embedded-dex")
Colin Cross46abdad2019-02-07 13:07:08 -080096 }
97
Gurpreet Singh75d65f32022-01-24 17:44:05 +000098 if params.ClassLoaderContexts != nil {
99 // manifest_fixer should add only the implicit SDK libraries inferred by Soong, not those added
100 // explicitly via `uses_libs`/`optional_uses_libs`.
101 requiredUsesLibs, optionalUsesLibs := params.ClassLoaderContexts.ImplicitUsesLibs()
102
103 for _, usesLib := range requiredUsesLibs {
104 args = append(args, "--uses-library", usesLib)
105 }
106 for _, usesLib := range optionalUsesLibs {
107 args = append(args, "--optional-uses-library", usesLib)
108 }
Colin Cross5446e882019-05-22 10:46:27 -0700109 }
110
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000111 if params.HasNoCode {
Jaewoong Jungc27ab662019-05-30 15:51:14 -0700112 args = append(args, "--has-no-code")
113 }
114
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000115 if params.TestOnly {
116 args = append(args, "--test-only")
117 }
118
119 if params.LoggingParent != "" {
120 args = append(args, "--logging-parent", params.LoggingParent)
Baligh Uddin5b16dfb2020-02-11 17:27:19 -0800121 }
Colin Cross10932872019-04-18 14:27:12 -0700122 var deps android.Paths
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000123 var argsMapper = make(map[string]string)
Spandan Dasc8cb0c32021-12-08 17:00:38 +0000124
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000125 if params.SdkContext != nil {
Gurpreet Singh7deabfa2022-02-10 13:28:35 +0000126 targetSdkVersion := targetSdkVersionForManifestFixer(ctx, params.SdkContext)
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000127 args = append(args, "--targetSdkVersion ", targetSdkVersion)
128
Gurpreet Singh7deabfa2022-02-10 13:28:35 +0000129 if UseApiFingerprint(ctx) && ctx.ModuleName() != "framework-res" {
130 targetSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", ApiFingerprintPath(ctx).String())
131 deps = append(deps, ApiFingerprintPath(ctx))
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000132 }
133
Gurpreet Singh7deabfa2022-02-10 13:28:35 +0000134 minSdkVersion, err := params.SdkContext.MinSdkVersion(ctx).EffectiveVersionString(ctx)
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000135 if err != nil {
Gurpreet Singh7deabfa2022-02-10 13:28:35 +0000136 ctx.ModuleErrorf("invalid minSdkVersion: %s", err)
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000137 }
138
Gurpreet Singh7deabfa2022-02-10 13:28:35 +0000139 if UseApiFingerprint(ctx) && ctx.ModuleName() != "framework-res" {
140 minSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", ApiFingerprintPath(ctx).String())
141 deps = append(deps, ApiFingerprintPath(ctx))
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000142 }
143
144 if err != nil {
Gurpreet Singh7deabfa2022-02-10 13:28:35 +0000145 ctx.ModuleErrorf("invalid minSdkVersion: %s", err)
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000146 }
147 args = append(args, "--minSdkVersion ", minSdkVersion)
148 args = append(args, "--raise-min-sdk-version")
Jiyong Park6a927c42020-01-21 02:03:43 +0900149 }
150
Gurpreet Singh7deabfa2022-02-10 13:28:35 +0000151 fixedManifest := android.PathForModuleOut(ctx, "manifest_fixer", "AndroidManifest.xml")
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000152 argsMapper["args"] = strings.Join(args, " ")
Colin Cross10932872019-04-18 14:27:12 -0700153
Gurpreet Singh7deabfa2022-02-10 13:28:35 +0000154 ctx.Build(pctx, android.BuildParams{
Colin Cross90c25c62019-04-19 16:22:57 -0700155 Rule: manifestFixerRule,
156 Description: "fix manifest",
Gurpreet Singh7deabfa2022-02-10 13:28:35 +0000157 Input: manifest,
Colin Cross90c25c62019-04-19 16:22:57 -0700158 Implicits: deps,
159 Output: fixedManifest,
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000160 Args: argsMapper,
Colin Cross31656952018-05-24 16:11:20 -0700161 })
Colin Cross31656952018-05-24 16:11:20 -0700162
Colin Cross8eebb132020-01-29 20:07:03 -0800163 return fixedManifest.WithoutRel()
Colin Cross90c25c62019-04-19 16:22:57 -0700164}
Colin Cross31656952018-05-24 16:11:20 -0700165
Colin Crossaa1c6f12019-04-19 16:25:38 -0700166func manifestMerger(ctx android.ModuleContext, manifest android.Path, staticLibManifests android.Paths,
167 isLibrary bool) android.Path {
168
169 var args string
170 if !isLibrary {
171 // Follow Gradle's behavior, only pass --remove-tools-declarations when merging app manifests.
172 args = "--remove-tools-declarations"
173 }
174
Colin Cross90c25c62019-04-19 16:22:57 -0700175 mergedManifest := android.PathForModuleOut(ctx, "manifest_merger", "AndroidManifest.xml")
176 ctx.Build(pctx, android.BuildParams{
177 Rule: manifestMergerRule,
178 Description: "merge manifest",
179 Input: manifest,
180 Implicits: staticLibManifests,
181 Output: mergedManifest,
182 Args: map[string]string{
183 "libs": android.JoinWithPrefix(staticLibManifests.Strings(), "--libs "),
Colin Crossaa1c6f12019-04-19 16:25:38 -0700184 "args": args,
Colin Cross90c25c62019-04-19 16:22:57 -0700185 },
186 })
187
Colin Cross8eebb132020-01-29 20:07:03 -0800188 return mergedManifest.WithoutRel()
Colin Cross31656952018-05-24 16:11:20 -0700189}