blob: d20a6bfe4271df9b71dcdea3c581832d4dc1a3fd [file] [log] [blame]
Colin Cross30e076a2015-04-13 13:58:27 -07001// 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 (
Colin Crossa4f08812018-10-02 22:03:40 -070022 "path/filepath"
Colin Cross30e076a2015-04-13 13:58:27 -070023 "strings"
24
25 "github.com/google/blueprint"
Colin Crossa4f08812018-10-02 22:03:40 -070026 "github.com/google/blueprint/proptools"
Colin Cross30e076a2015-04-13 13:58:27 -070027
Colin Cross635c3b02016-05-18 15:37:25 -070028 "android/soong/android"
Kousik Kumar309b1c02020-05-28 06:13:33 -070029 "android/soong/remoteexec"
Colin Cross30e076a2015-04-13 13:58:27 -070030)
31
32var (
Colin Cross77cdcfd2021-03-12 11:28:25 -080033 Signapk, SignapkRE = pctx.RemoteStaticRules("signapk",
Colin Cross30e076a2015-04-13 13:58:27 -070034 blueprint.RuleParams{
Ulf Adams7e7ef562020-11-25 23:07:23 +010035 Command: `rm -f $out && $reTemplate${config.JavaCmd} ${config.JavaVmFlags} -Djava.library.path=$$(dirname ${config.SignapkJniLibrary}) ` +
Colin Crossbdf95142019-08-08 12:56:34 -070036 `-jar ${config.SignapkCmd} $flags $certificates $in $out`,
37 CommandDeps: []string{"${config.SignapkCmd}", "${config.SignapkJniLibrary}"},
Colin Cross30e076a2015-04-13 13:58:27 -070038 },
Kousik Kumar309b1c02020-05-28 06:13:33 -070039 &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"})
Colin Cross30e076a2015-04-13 13:58:27 -070046)
47
Colin Cross3bc7ffa2017-11-22 16:19:37 -080048var combineApk = pctx.AndroidStaticRule("combineApk",
49 blueprint.RuleParams{
50 Command: `${config.MergeZipsCmd} $out $in`,
51 CommandDeps: []string{"${config.MergeZipsCmd}"},
Colin Cross30e076a2015-04-13 13:58:27 -070052 })
53
Jaewoong Jungccbb3932019-04-15 09:48:31 -070054func CreateAndSignAppPackage(ctx android.ModuleContext, outputFile android.WritablePath,
Rico Wind351bac92022-09-22 10:41:42 +020055 packageFile, jniJarFile, dexJarFile android.Path, certificates []Certificate, deps android.Paths, v4SignatureFile android.WritablePath, lineageFile android.Path, rotationMinSdkVersion string, shrinkResources bool) {
Colin Cross3bc7ffa2017-11-22 16:19:37 -080056
Colin Crosse560c4a2019-03-19 16:03:11 -070057 unsignedApkName := strings.TrimSuffix(outputFile.Base(), ".apk") + "-unsigned.apk"
58 unsignedApk := android.PathForModuleOut(ctx, unsignedApkName)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080059
Colin Crossa4f08812018-10-02 22:03:40 -070060 var inputs android.Paths
Colin Cross3bc7ffa2017-11-22 16:19:37 -080061 if dexJarFile != nil {
62 inputs = append(inputs, dexJarFile)
63 }
Colin Crossfd94c402018-11-01 14:50:55 -070064 inputs = append(inputs, packageFile)
Colin Crossa4f08812018-10-02 22:03:40 -070065 if jniJarFile != nil {
66 inputs = append(inputs, jniJarFile)
67 }
Colin Crossae887032017-10-23 17:16:14 -070068 ctx.Build(pctx, android.BuildParams{
Colin Cross50ddcc42019-05-16 12:28:22 -070069 Rule: combineApk,
70 Inputs: inputs,
71 Output: unsignedApk,
72 Implicits: deps,
Colin Cross30e076a2015-04-13 13:58:27 -070073 })
74
Rico Wind351bac92022-09-22 10:41:42 +020075 if shrinkResources {
76 shrunkenApk := android.PathForModuleOut(ctx, "resource-shrunken", unsignedApk.Base())
77 ShrinkResources(ctx, unsignedApk, shrunkenApk)
78 unsignedApk = shrunkenApk
79 }
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -040080 SignAppPackage(ctx, outputFile, unsignedApk, certificates, v4SignatureFile, lineageFile, rotationMinSdkVersion)
Jaewoong Jungccbb3932019-04-15 09:48:31 -070081}
82
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -040083func SignAppPackage(ctx android.ModuleContext, signedApk android.WritablePath, unsignedApk android.Path, certificates []Certificate, v4SignatureFile android.WritablePath, lineageFile android.Path, rotationMinSdkVersion string) {
Jaewoong Jungccbb3932019-04-15 09:48:31 -070084
Colin Cross30e076a2015-04-13 13:58:27 -070085 var certificateArgs []string
Dan Willemsenc4bd8f82019-04-09 21:26:14 -070086 var deps android.Paths
Colin Cross30e076a2015-04-13 13:58:27 -070087 for _, c := range certificates {
Jiyong Parkc00cbd92018-10-30 21:20:05 +090088 certificateArgs = append(certificateArgs, c.Pem.String(), c.Key.String())
Dan Willemsenc4bd8f82019-04-09 21:26:14 -070089 deps = append(deps, c.Pem, c.Key)
Colin Cross30e076a2015-04-13 13:58:27 -070090 }
Songchun Fan688de9a2020-03-24 20:32:24 -070091 outputFiles := android.WritablePaths{signedApk}
Liz Kammer70dd74d2020-05-07 13:24:05 -070092 var flags []string
Songchun Fan688de9a2020-03-24 20:32:24 -070093 if v4SignatureFile != nil {
94 outputFiles = append(outputFiles, v4SignatureFile)
Liz Kammer70dd74d2020-05-07 13:24:05 -070095 flags = append(flags, "--enable-v4")
96 }
97
98 if lineageFile != nil {
99 flags = append(flags, "--lineage", lineageFile.String())
Liz Kammer9cd4f812020-05-08 17:19:26 -0700100 deps = append(deps, lineageFile)
Songchun Fan688de9a2020-03-24 20:32:24 -0700101 }
102
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400103 if rotationMinSdkVersion != "" {
104 flags = append(flags, "--rotation-min-sdk-version", rotationMinSdkVersion)
105 }
106
Kousik Kumar309b1c02020-05-28 06:13:33 -0700107 rule := Signapk
108 args := map[string]string{
109 "certificates": strings.Join(certificateArgs, " "),
110 "flags": strings.Join(flags, " "),
111 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400112 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_SIGNAPK") {
Kousik Kumar309b1c02020-05-28 06:13:33 -0700113 rule = SignapkRE
114 args["implicits"] = strings.Join(deps.Strings(), ",")
115 args["outCommaList"] = strings.Join(outputFiles.Strings(), ",")
116 }
Colin Crossae887032017-10-23 17:16:14 -0700117 ctx.Build(pctx, android.BuildParams{
Kousik Kumar309b1c02020-05-28 06:13:33 -0700118 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700119 Description: "signapk",
Songchun Fan688de9a2020-03-24 20:32:24 -0700120 Outputs: outputFiles,
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800121 Input: unsignedApk,
Dan Willemsenc4bd8f82019-04-09 21:26:14 -0700122 Implicits: deps,
Kousik Kumar309b1c02020-05-28 06:13:33 -0700123 Args: args,
Colin Cross30e076a2015-04-13 13:58:27 -0700124 })
Colin Cross30e076a2015-04-13 13:58:27 -0700125}
Colin Crossa97c5d32018-03-28 14:58:31 -0700126
127var buildAAR = pctx.AndroidStaticRule("buildAAR",
128 blueprint.RuleParams{
129 Command: `rm -rf ${outDir} && mkdir -p ${outDir} && ` +
130 `cp ${manifest} ${outDir}/AndroidManifest.xml && ` +
131 `cp ${classesJar} ${outDir}/classes.jar && ` +
132 `cp ${rTxt} ${outDir}/R.txt && ` +
Colin Cross1d98ee22018-09-18 17:05:15 -0700133 `${config.SoongZipCmd} -jar -o $out -C ${outDir} -D ${outDir}`,
Colin Crossa97c5d32018-03-28 14:58:31 -0700134 CommandDeps: []string{"${config.SoongZipCmd}"},
135 },
Colin Cross1d98ee22018-09-18 17:05:15 -0700136 "manifest", "classesJar", "rTxt", "outDir")
Colin Crossa97c5d32018-03-28 14:58:31 -0700137
138func BuildAAR(ctx android.ModuleContext, outputFile android.WritablePath,
139 classesJar, manifest, rTxt android.Path, res android.Paths) {
140
141 // TODO(ccross): uniquify and copy resources with dependencies
142
143 deps := android.Paths{manifest, rTxt}
144 classesJarPath := ""
145 if classesJar != nil {
146 deps = append(deps, classesJar)
147 classesJarPath = classesJar.String()
148 }
149
150 ctx.Build(pctx, android.BuildParams{
Colin Crossf57c5782019-01-25 13:20:38 -0800151 Rule: buildAAR,
152 Description: "aar",
153 Implicits: deps,
154 Output: outputFile,
Colin Crossa97c5d32018-03-28 14:58:31 -0700155 Args: map[string]string{
156 "manifest": manifest.String(),
157 "classesJar": classesJarPath,
158 "rTxt": rTxt.String(),
159 "outDir": android.PathForModuleOut(ctx, "aar").String(),
160 },
161 })
162}
Colin Crossa4f08812018-10-02 22:03:40 -0700163
Colin Crossf6237212018-10-29 23:14:58 -0700164var buildBundleModule = pctx.AndroidStaticRule("buildBundleModule",
165 blueprint.RuleParams{
Colin Crossfd94c402018-11-01 14:50:55 -0700166 Command: `${config.MergeZipsCmd} ${out} ${in}`,
167 CommandDeps: []string{"${config.MergeZipsCmd}"},
168 })
169
170var bundleMungePackage = pctx.AndroidStaticRule("bundleMungePackage",
171 blueprint.RuleParams{
172 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} AndroidManifest.xml:manifest/AndroidManifest.xml resources.pb "res/**/*" "assets/**/*"`,
173 CommandDeps: []string{"${config.Zip2ZipCmd}"},
174 })
175
176var bundleMungeDexJar = pctx.AndroidStaticRule("bundleMungeDexJar",
177 blueprint.RuleParams{
178 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "classes*.dex:dex/" && ` +
179 `${config.Zip2ZipCmd} -i ${in} -o ${resJar} -x "classes*.dex" "**/*:root/"`,
180 CommandDeps: []string{"${config.Zip2ZipCmd}"},
181 }, "resJar")
Colin Crossf6237212018-10-29 23:14:58 -0700182
183// Builds an app into a module suitable for input to bundletool
184func BuildBundleModule(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Crossfd94c402018-11-01 14:50:55 -0700185 packageFile, jniJarFile, dexJarFile android.Path) {
Colin Crossf6237212018-10-29 23:14:58 -0700186
187 protoResJarFile := android.PathForModuleOut(ctx, "package-res.pb.apk")
Rico Wind351bac92022-09-22 10:41:42 +0200188 aapt2Convert(ctx, protoResJarFile, packageFile, "proto")
Colin Crossf6237212018-10-29 23:14:58 -0700189
Colin Crossfd94c402018-11-01 14:50:55 -0700190 var zips android.Paths
191
192 mungedPackage := android.PathForModuleOut(ctx, "bundle", "apk.zip")
193 ctx.Build(pctx, android.BuildParams{
194 Rule: bundleMungePackage,
195 Input: protoResJarFile,
196 Output: mungedPackage,
197 Description: "bundle apk",
198 })
199 zips = append(zips, mungedPackage)
200
Colin Crossf6237212018-10-29 23:14:58 -0700201 if dexJarFile != nil {
Colin Crossfd94c402018-11-01 14:50:55 -0700202 mungedDexJar := android.PathForModuleOut(ctx, "bundle", "dex.zip")
203 mungedResJar := android.PathForModuleOut(ctx, "bundle", "res.zip")
204 ctx.Build(pctx, android.BuildParams{
205 Rule: bundleMungeDexJar,
206 Input: dexJarFile,
207 Output: mungedDexJar,
208 ImplicitOutput: mungedResJar,
209 Description: "bundle dex",
210 Args: map[string]string{
211 "resJar": mungedResJar.String(),
212 },
213 })
214 zips = append(zips, mungedDexJar, mungedResJar)
Colin Crossf6237212018-10-29 23:14:58 -0700215 }
216 if jniJarFile != nil {
Colin Crossfd94c402018-11-01 14:50:55 -0700217 zips = append(zips, jniJarFile)
Colin Crossf6237212018-10-29 23:14:58 -0700218 }
219
220 ctx.Build(pctx, android.BuildParams{
221 Rule: buildBundleModule,
Colin Crossfd94c402018-11-01 14:50:55 -0700222 Inputs: zips,
Colin Crossf6237212018-10-29 23:14:58 -0700223 Output: outputFile,
224 Description: "bundle",
Colin Crossf6237212018-10-29 23:14:58 -0700225 })
226}
227
Sam Delmerico82602492022-06-10 17:05:42 +0000228const jniJarOutputPathString = "jniJarOutput.zip"
229
230func TransformJniLibsToJar(
231 ctx android.ModuleContext,
232 outputFile android.WritablePath,
233 jniLibs []jniLib,
234 prebuiltJniPackages android.Paths,
235 uncompressJNI bool) {
Colin Crossa4f08812018-10-02 22:03:40 -0700236
237 var deps android.Paths
238 jarArgs := []string{
239 "-j", // junk paths, they will be added back with -P arguments
240 }
241
Colin Crosse4246ab2019-02-05 21:55:21 -0800242 if uncompressJNI {
Peter Collingbournead84f972019-12-17 16:46:18 -0800243 jarArgs = append(jarArgs, "-L", "0")
Colin Crossa4f08812018-10-02 22:03:40 -0700244 }
245
246 for _, j := range jniLibs {
247 deps = append(deps, j.path)
248 jarArgs = append(jarArgs,
Peter Collingbournead84f972019-12-17 16:46:18 -0800249 "-P", targetToJniDir(j.target),
250 "-f", j.path.String())
Colin Crossa4f08812018-10-02 22:03:40 -0700251 }
252
Kousik Kumar366afc52020-05-20 11:27:16 -0700253 rule := zip
254 args := map[string]string{
255 "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
256 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400257 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ZIP") {
Kousik Kumar366afc52020-05-20 11:27:16 -0700258 rule = zipRE
259 args["implicits"] = strings.Join(deps.Strings(), ",")
260 }
Sam Delmerico82602492022-06-10 17:05:42 +0000261 jniJarPath := android.PathForModuleOut(ctx, jniJarOutputPathString)
Colin Crossa4f08812018-10-02 22:03:40 -0700262 ctx.Build(pctx, android.BuildParams{
Kousik Kumar366afc52020-05-20 11:27:16 -0700263 Rule: rule,
Colin Crossa4f08812018-10-02 22:03:40 -0700264 Description: "zip jni libs",
Sam Delmerico82602492022-06-10 17:05:42 +0000265 Output: jniJarPath,
Colin Crossa4f08812018-10-02 22:03:40 -0700266 Implicits: deps,
Kousik Kumar366afc52020-05-20 11:27:16 -0700267 Args: args,
Colin Crossa4f08812018-10-02 22:03:40 -0700268 })
Sam Delmerico82602492022-06-10 17:05:42 +0000269 ctx.Build(pctx, android.BuildParams{
270 Rule: mergeAssetsRule,
271 Description: "merge prebuilt JNI packages",
272 Inputs: append(prebuiltJniPackages, jniJarPath),
273 Output: outputFile,
274 })
Colin Crossa4f08812018-10-02 22:03:40 -0700275}
276
Matt Banda8c801262022-04-01 17:48:31 +0000277func (a *AndroidApp) generateJavaUsedByApex(ctx android.ModuleContext) {
278 javaApiUsedByOutputFile := android.PathForModuleOut(ctx, a.installApkName+"_using.xml")
279 javaUsedByRule := android.NewRuleBuilder(pctx, ctx)
280 javaUsedByRule.Command().
281 Tool(android.PathForSource(ctx, "build/soong/scripts/gen_java_usedby_apex.sh")).
282 BuiltTool("dexdeps").
283 Output(javaApiUsedByOutputFile).
284 Input(a.Library.Module.outputFile)
285 javaUsedByRule.Build("java_usedby_list", "Generate Java APIs used by Apex")
286 a.javaApiUsedByOutputFile = javaApiUsedByOutputFile
287}
288
Colin Crossa4f08812018-10-02 22:03:40 -0700289func targetToJniDir(target android.Target) string {
290 return filepath.Join("lib", target.Arch.Abi[0])
291}