• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2015 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
17// This file generates the final rules for compiling all Java.  All properties related to
18// compiling should have been translated into javaBuilderFlags or another argument to the Transform*
19// functions.
20
21import (
22	"path/filepath"
23	"strings"
24
25	"github.com/google/blueprint"
26	"github.com/google/blueprint/proptools"
27
28	"android/soong/android"
29	"android/soong/remoteexec"
30)
31
32var (
33	Signapk, SignapkRE = pctx.RemoteStaticRules("signapk",
34		blueprint.RuleParams{
35			Command: `rm -f $out && $reTemplate${config.JavaCmd} ${config.JavaVmFlags} -Djava.library.path=$$(dirname ${config.SignapkJniLibrary}) ` +
36				`-jar ${config.SignapkCmd} $flags $certificates $in $out`,
37			CommandDeps: []string{"${config.SignapkCmd}", "${config.SignapkJniLibrary}"},
38		},
39		&remoteexec.REParams{Labels: map[string]string{"type": "tool", "name": "signapk"},
40			ExecStrategy:    "${config.RESignApkExecStrategy}",
41			Inputs:          []string{"${config.SignapkCmd}", "$in", "$$(dirname ${config.SignapkJniLibrary})", "$implicits"},
42			OutputFiles:     []string{"$outCommaList"},
43			ToolchainInputs: []string{"${config.JavaCmd}"},
44			Platform:        map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
45		}, []string{"flags", "certificates"}, []string{"implicits", "outCommaList"})
46)
47
48var combineApk = pctx.AndroidStaticRule("combineApk",
49	blueprint.RuleParams{
50		Command:     `${config.MergeZipsCmd} $out $in`,
51		CommandDeps: []string{"${config.MergeZipsCmd}"},
52	})
53
54func CreateAndSignAppPackage(ctx android.ModuleContext, outputFile android.WritablePath,
55	packageFile, jniJarFile, dexJarFile android.Path, certificates []Certificate, deps android.Paths, v4SignatureFile android.WritablePath, lineageFile android.Path, rotationMinSdkVersion string) {
56
57	unsignedApkName := strings.TrimSuffix(outputFile.Base(), ".apk") + "-unsigned.apk"
58	unsignedApk := android.PathForModuleOut(ctx, unsignedApkName)
59
60	var inputs android.Paths
61	if dexJarFile != nil {
62		inputs = append(inputs, dexJarFile)
63	}
64	inputs = append(inputs, packageFile)
65	if jniJarFile != nil {
66		inputs = append(inputs, jniJarFile)
67	}
68
69	ctx.Build(pctx, android.BuildParams{
70		Rule:      combineApk,
71		Inputs:    inputs,
72		Output:    unsignedApk,
73		Implicits: deps,
74	})
75
76	SignAppPackage(ctx, outputFile, unsignedApk, certificates, v4SignatureFile, lineageFile, rotationMinSdkVersion)
77}
78
79func SignAppPackage(ctx android.ModuleContext, signedApk android.WritablePath, unsignedApk android.Path, certificates []Certificate, v4SignatureFile android.WritablePath, lineageFile android.Path, rotationMinSdkVersion string) {
80
81	var certificateArgs []string
82	var deps android.Paths
83	for _, c := range certificates {
84		certificateArgs = append(certificateArgs, c.Pem.String(), c.Key.String())
85		deps = append(deps, c.Pem, c.Key)
86	}
87
88	outputFiles := android.WritablePaths{signedApk}
89	var flags []string
90	if v4SignatureFile != nil {
91		outputFiles = append(outputFiles, v4SignatureFile)
92		flags = append(flags, "--enable-v4")
93	}
94
95	if lineageFile != nil {
96		flags = append(flags, "--lineage", lineageFile.String())
97		deps = append(deps, lineageFile)
98	}
99
100	if rotationMinSdkVersion != "" {
101		flags = append(flags, "--rotation-min-sdk-version", rotationMinSdkVersion)
102	}
103
104	rule := Signapk
105	args := map[string]string{
106		"certificates": strings.Join(certificateArgs, " "),
107		"flags":        strings.Join(flags, " "),
108	}
109	if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_SIGNAPK") {
110		rule = SignapkRE
111		args["implicits"] = strings.Join(deps.Strings(), ",")
112		args["outCommaList"] = strings.Join(outputFiles.Strings(), ",")
113	}
114	ctx.Build(pctx, android.BuildParams{
115		Rule:        rule,
116		Description: "signapk",
117		Outputs:     outputFiles,
118		Input:       unsignedApk,
119		Implicits:   deps,
120		Args:        args,
121	})
122}
123
124var buildAAR = pctx.AndroidStaticRule("buildAAR",
125	blueprint.RuleParams{
126		Command: `rm -rf ${outDir} && mkdir -p ${outDir} && ` +
127			`cp ${manifest} ${outDir}/AndroidManifest.xml && ` +
128			`cp ${classesJar} ${outDir}/classes.jar && ` +
129			`cp ${rTxt} ${outDir}/R.txt && ` +
130			`${config.SoongZipCmd} -jar -o $out -C ${outDir} -D ${outDir}`,
131		CommandDeps: []string{"${config.SoongZipCmd}"},
132	},
133	"manifest", "classesJar", "rTxt", "outDir")
134
135func BuildAAR(ctx android.ModuleContext, outputFile android.WritablePath,
136	classesJar, manifest, rTxt android.Path, res android.Paths) {
137
138	// TODO(ccross): uniquify and copy resources with dependencies
139
140	deps := android.Paths{manifest, rTxt}
141	classesJarPath := ""
142	if classesJar != nil {
143		deps = append(deps, classesJar)
144		classesJarPath = classesJar.String()
145	}
146
147	ctx.Build(pctx, android.BuildParams{
148		Rule:        buildAAR,
149		Description: "aar",
150		Implicits:   deps,
151		Output:      outputFile,
152		Args: map[string]string{
153			"manifest":   manifest.String(),
154			"classesJar": classesJarPath,
155			"rTxt":       rTxt.String(),
156			"outDir":     android.PathForModuleOut(ctx, "aar").String(),
157		},
158	})
159}
160
161var buildBundleModule = pctx.AndroidStaticRule("buildBundleModule",
162	blueprint.RuleParams{
163		Command:     `${config.MergeZipsCmd} ${out} ${in}`,
164		CommandDeps: []string{"${config.MergeZipsCmd}"},
165	})
166
167var bundleMungePackage = pctx.AndroidStaticRule("bundleMungePackage",
168	blueprint.RuleParams{
169		Command:     `${config.Zip2ZipCmd} -i ${in} -o ${out} AndroidManifest.xml:manifest/AndroidManifest.xml resources.pb "res/**/*" "assets/**/*"`,
170		CommandDeps: []string{"${config.Zip2ZipCmd}"},
171	})
172
173var bundleMungeDexJar = pctx.AndroidStaticRule("bundleMungeDexJar",
174	blueprint.RuleParams{
175		Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "classes*.dex:dex/" && ` +
176			`${config.Zip2ZipCmd} -i ${in} -o ${resJar} -x "classes*.dex" "**/*:root/"`,
177		CommandDeps: []string{"${config.Zip2ZipCmd}"},
178	}, "resJar")
179
180// Builds an app into a module suitable for input to bundletool
181func BuildBundleModule(ctx android.ModuleContext, outputFile android.WritablePath,
182	packageFile, jniJarFile, dexJarFile android.Path) {
183
184	protoResJarFile := android.PathForModuleOut(ctx, "package-res.pb.apk")
185	aapt2Convert(ctx, protoResJarFile, packageFile)
186
187	var zips android.Paths
188
189	mungedPackage := android.PathForModuleOut(ctx, "bundle", "apk.zip")
190	ctx.Build(pctx, android.BuildParams{
191		Rule:        bundleMungePackage,
192		Input:       protoResJarFile,
193		Output:      mungedPackage,
194		Description: "bundle apk",
195	})
196	zips = append(zips, mungedPackage)
197
198	if dexJarFile != nil {
199		mungedDexJar := android.PathForModuleOut(ctx, "bundle", "dex.zip")
200		mungedResJar := android.PathForModuleOut(ctx, "bundle", "res.zip")
201		ctx.Build(pctx, android.BuildParams{
202			Rule:           bundleMungeDexJar,
203			Input:          dexJarFile,
204			Output:         mungedDexJar,
205			ImplicitOutput: mungedResJar,
206			Description:    "bundle dex",
207			Args: map[string]string{
208				"resJar": mungedResJar.String(),
209			},
210		})
211		zips = append(zips, mungedDexJar, mungedResJar)
212	}
213	if jniJarFile != nil {
214		zips = append(zips, jniJarFile)
215	}
216
217	ctx.Build(pctx, android.BuildParams{
218		Rule:        buildBundleModule,
219		Inputs:      zips,
220		Output:      outputFile,
221		Description: "bundle",
222	})
223}
224
225func TransformJniLibsToJar(ctx android.ModuleContext, outputFile android.WritablePath,
226	jniLibs []jniLib, uncompressJNI bool) {
227
228	var deps android.Paths
229	jarArgs := []string{
230		"-j", // junk paths, they will be added back with -P arguments
231	}
232
233	if uncompressJNI {
234		jarArgs = append(jarArgs, "-L", "0")
235	}
236
237	for _, j := range jniLibs {
238		deps = append(deps, j.path)
239		jarArgs = append(jarArgs,
240			"-P", targetToJniDir(j.target),
241			"-f", j.path.String())
242	}
243
244	rule := zip
245	args := map[string]string{
246		"jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
247	}
248	if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ZIP") {
249		rule = zipRE
250		args["implicits"] = strings.Join(deps.Strings(), ",")
251	}
252	ctx.Build(pctx, android.BuildParams{
253		Rule:        rule,
254		Description: "zip jni libs",
255		Output:      outputFile,
256		Implicits:   deps,
257		Args:        args,
258	})
259}
260
261func (a *AndroidApp) generateJavaUsedByApex(ctx android.ModuleContext) {
262	javaApiUsedByOutputFile := android.PathForModuleOut(ctx, a.installApkName+"_using.xml")
263	javaUsedByRule := android.NewRuleBuilder(pctx, ctx)
264	javaUsedByRule.Command().
265		Tool(android.PathForSource(ctx, "build/soong/scripts/gen_java_usedby_apex.sh")).
266		BuiltTool("dexdeps").
267		Output(javaApiUsedByOutputFile).
268		Input(a.Library.Module.outputFile)
269	javaUsedByRule.Build("java_usedby_list", "Generate Java APIs used by Apex")
270	a.javaApiUsedByOutputFile = javaApiUsedByOutputFile
271}
272
273func targetToJniDir(target android.Target) string {
274	return filepath.Join("lib", target.Arch.Abi[0])
275}
276