• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15package java
16
17import (
18	"fmt"
19	"strings"
20
21	"github.com/google/blueprint"
22
23	"android/soong/android"
24)
25
26var manifestFixerRule = pctx.AndroidStaticRule("manifestFixer",
27	blueprint.RuleParams{
28		Command: `${config.ManifestFixerCmd} ` +
29			`--minSdkVersion ${minSdkVersion} ` +
30			`--targetSdkVersion ${targetSdkVersion} ` +
31			`--raise-min-sdk-version ` +
32			`$args $in $out`,
33		CommandDeps: []string{"${config.ManifestFixerCmd}"},
34	},
35	"minSdkVersion", "targetSdkVersion", "args")
36
37var manifestMergerRule = pctx.AndroidStaticRule("manifestMerger",
38	blueprint.RuleParams{
39		Command:     `${config.ManifestMergerCmd} --main $in $libs --out $out`,
40		CommandDeps: []string{"${config.ManifestMergerCmd}"},
41	},
42	"libs")
43
44func manifestMerger(ctx android.ModuleContext, manifest android.Path, sdkContext sdkContext,
45	staticLibManifests android.Paths, isLibrary, uncompressedJNI, useEmbeddedDex, usesNonSdkApis bool) android.Path {
46
47	var args []string
48	if isLibrary {
49		args = append(args, "--library")
50	} else {
51		minSdkVersion, err := sdkVersionToNumber(ctx, sdkContext.minSdkVersion())
52		if err != nil {
53			ctx.ModuleErrorf("invalid minSdkVersion: %s", err)
54		}
55		if minSdkVersion >= 23 {
56			args = append(args, fmt.Sprintf("--extract-native-libs=%v", !uncompressedJNI))
57		} else if uncompressedJNI {
58			ctx.ModuleErrorf("module attempted to store uncompressed native libraries, but minSdkVersion=%d doesn't support it",
59				minSdkVersion)
60		}
61	}
62
63	if usesNonSdkApis {
64		args = append(args, "--uses-non-sdk-api")
65	}
66
67	if useEmbeddedDex {
68		args = append(args, "--use-embedded-dex=true")
69	}
70
71	var deps android.Paths
72	targetSdkVersion := sdkVersionOrDefault(ctx, sdkContext.targetSdkVersion())
73	if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
74		ctx.Config().UnbundledBuild() &&
75		!ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
76		ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
77		apiFingerprint := ApiFingerprintPath(ctx)
78		targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
79		deps = append(deps, apiFingerprint)
80	}
81
82	// Inject minSdkVersion into the manifest
83	fixedManifest := android.PathForModuleOut(ctx, "manifest_fixer", "AndroidManifest.xml")
84	ctx.Build(pctx, android.BuildParams{
85		Rule:      manifestFixerRule,
86		Input:     manifest,
87		Implicits: deps,
88		Output:    fixedManifest,
89		Args: map[string]string{
90			"minSdkVersion":    sdkVersionOrDefault(ctx, sdkContext.minSdkVersion()),
91			"targetSdkVersion": targetSdkVersion,
92			"args":             strings.Join(args, " "),
93		},
94	})
95	manifest = fixedManifest
96
97	// Merge static aar dependency manifests if necessary
98	if len(staticLibManifests) > 0 {
99		mergedManifest := android.PathForModuleOut(ctx, "manifest_merger", "AndroidManifest.xml")
100		ctx.Build(pctx, android.BuildParams{
101			Rule:      manifestMergerRule,
102			Input:     manifest,
103			Implicits: staticLibManifests,
104			Output:    mergedManifest,
105			Args: map[string]string{
106				"libs": android.JoinWithPrefix(staticLibManifests.Strings(), "--libs "),
107			},
108		})
109		manifest = mergedManifest
110	}
111
112	return manifest
113}
114