blob: 31023cb9cfc517f46a3ffe907f594a818ae9199a [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,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -040055 packageFile, jniJarFile, dexJarFile android.Path, certificates []Certificate, deps android.Paths, v4SignatureFile android.WritablePath, lineageFile android.Path, rotationMinSdkVersion string) {
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 Cross30e076a2015-04-13 13:58:27 -070068
Colin Crossae887032017-10-23 17:16:14 -070069 ctx.Build(pctx, android.BuildParams{
Colin Cross50ddcc42019-05-16 12:28:22 -070070 Rule: combineApk,
71 Inputs: inputs,
72 Output: unsignedApk,
73 Implicits: deps,
Colin Cross30e076a2015-04-13 13:58:27 -070074 })
75
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -040076 SignAppPackage(ctx, outputFile, unsignedApk, certificates, v4SignatureFile, lineageFile, rotationMinSdkVersion)
Jaewoong Jungccbb3932019-04-15 09:48:31 -070077}
78
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -040079func 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 -070080
Colin Cross30e076a2015-04-13 13:58:27 -070081 var certificateArgs []string
Dan Willemsenc4bd8f82019-04-09 21:26:14 -070082 var deps android.Paths
Colin Cross30e076a2015-04-13 13:58:27 -070083 for _, c := range certificates {
Jiyong Parkc00cbd92018-10-30 21:20:05 +090084 certificateArgs = append(certificateArgs, c.Pem.String(), c.Key.String())
Dan Willemsenc4bd8f82019-04-09 21:26:14 -070085 deps = append(deps, c.Pem, c.Key)
Colin Cross30e076a2015-04-13 13:58:27 -070086 }
87
Songchun Fan688de9a2020-03-24 20:32:24 -070088 outputFiles := android.WritablePaths{signedApk}
Liz Kammer70dd74d2020-05-07 13:24:05 -070089 var flags []string
Songchun Fan688de9a2020-03-24 20:32:24 -070090 if v4SignatureFile != nil {
91 outputFiles = append(outputFiles, v4SignatureFile)
Liz Kammer70dd74d2020-05-07 13:24:05 -070092 flags = append(flags, "--enable-v4")
93 }
94
95 if lineageFile != nil {
96 flags = append(flags, "--lineage", lineageFile.String())
Liz Kammer9cd4f812020-05-08 17:19:26 -070097 deps = append(deps, lineageFile)
Songchun Fan688de9a2020-03-24 20:32:24 -070098 }
99
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400100 if rotationMinSdkVersion != "" {
101 flags = append(flags, "--rotation-min-sdk-version", rotationMinSdkVersion)
102 }
103
Kousik Kumar309b1c02020-05-28 06:13:33 -0700104 rule := Signapk
105 args := map[string]string{
106 "certificates": strings.Join(certificateArgs, " "),
107 "flags": strings.Join(flags, " "),
108 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400109 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_SIGNAPK") {
Kousik Kumar309b1c02020-05-28 06:13:33 -0700110 rule = SignapkRE
111 args["implicits"] = strings.Join(deps.Strings(), ",")
112 args["outCommaList"] = strings.Join(outputFiles.Strings(), ",")
113 }
Colin Crossae887032017-10-23 17:16:14 -0700114 ctx.Build(pctx, android.BuildParams{
Kousik Kumar309b1c02020-05-28 06:13:33 -0700115 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700116 Description: "signapk",
Songchun Fan688de9a2020-03-24 20:32:24 -0700117 Outputs: outputFiles,
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800118 Input: unsignedApk,
Dan Willemsenc4bd8f82019-04-09 21:26:14 -0700119 Implicits: deps,
Kousik Kumar309b1c02020-05-28 06:13:33 -0700120 Args: args,
Colin Cross30e076a2015-04-13 13:58:27 -0700121 })
Colin Cross30e076a2015-04-13 13:58:27 -0700122}
Colin Crossa97c5d32018-03-28 14:58:31 -0700123
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 && ` +
Colin Cross1d98ee22018-09-18 17:05:15 -0700130 `${config.SoongZipCmd} -jar -o $out -C ${outDir} -D ${outDir}`,
Colin Crossa97c5d32018-03-28 14:58:31 -0700131 CommandDeps: []string{"${config.SoongZipCmd}"},
132 },
Colin Cross1d98ee22018-09-18 17:05:15 -0700133 "manifest", "classesJar", "rTxt", "outDir")
Colin Crossa97c5d32018-03-28 14:58:31 -0700134
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{
Colin Crossf57c5782019-01-25 13:20:38 -0800148 Rule: buildAAR,
149 Description: "aar",
150 Implicits: deps,
151 Output: outputFile,
Colin Crossa97c5d32018-03-28 14:58:31 -0700152 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}
Colin Crossa4f08812018-10-02 22:03:40 -0700160
Colin Crossf6237212018-10-29 23:14:58 -0700161var buildBundleModule = pctx.AndroidStaticRule("buildBundleModule",
162 blueprint.RuleParams{
Colin Crossfd94c402018-11-01 14:50:55 -0700163 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")
Colin Crossf6237212018-10-29 23:14:58 -0700179
180// Builds an app into a module suitable for input to bundletool
181func BuildBundleModule(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Crossfd94c402018-11-01 14:50:55 -0700182 packageFile, jniJarFile, dexJarFile android.Path) {
Colin Crossf6237212018-10-29 23:14:58 -0700183
184 protoResJarFile := android.PathForModuleOut(ctx, "package-res.pb.apk")
Colin Crossfd94c402018-11-01 14:50:55 -0700185 aapt2Convert(ctx, protoResJarFile, packageFile)
Colin Crossf6237212018-10-29 23:14:58 -0700186
Colin Crossfd94c402018-11-01 14:50:55 -0700187 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
Colin Crossf6237212018-10-29 23:14:58 -0700198 if dexJarFile != nil {
Colin Crossfd94c402018-11-01 14:50:55 -0700199 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)
Colin Crossf6237212018-10-29 23:14:58 -0700212 }
213 if jniJarFile != nil {
Colin Crossfd94c402018-11-01 14:50:55 -0700214 zips = append(zips, jniJarFile)
Colin Crossf6237212018-10-29 23:14:58 -0700215 }
216
217 ctx.Build(pctx, android.BuildParams{
218 Rule: buildBundleModule,
Colin Crossfd94c402018-11-01 14:50:55 -0700219 Inputs: zips,
Colin Crossf6237212018-10-29 23:14:58 -0700220 Output: outputFile,
221 Description: "bundle",
Colin Crossf6237212018-10-29 23:14:58 -0700222 })
223}
224
Colin Crossa4f08812018-10-02 22:03:40 -0700225func TransformJniLibsToJar(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Crosse4246ab2019-02-05 21:55:21 -0800226 jniLibs []jniLib, uncompressJNI bool) {
Colin Crossa4f08812018-10-02 22:03:40 -0700227
228 var deps android.Paths
229 jarArgs := []string{
230 "-j", // junk paths, they will be added back with -P arguments
231 }
232
Colin Crosse4246ab2019-02-05 21:55:21 -0800233 if uncompressJNI {
Peter Collingbournead84f972019-12-17 16:46:18 -0800234 jarArgs = append(jarArgs, "-L", "0")
Colin Crossa4f08812018-10-02 22:03:40 -0700235 }
236
237 for _, j := range jniLibs {
238 deps = append(deps, j.path)
239 jarArgs = append(jarArgs,
Peter Collingbournead84f972019-12-17 16:46:18 -0800240 "-P", targetToJniDir(j.target),
241 "-f", j.path.String())
Colin Crossa4f08812018-10-02 22:03:40 -0700242 }
243
Kousik Kumar366afc52020-05-20 11:27:16 -0700244 rule := zip
245 args := map[string]string{
246 "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
247 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400248 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ZIP") {
Kousik Kumar366afc52020-05-20 11:27:16 -0700249 rule = zipRE
250 args["implicits"] = strings.Join(deps.Strings(), ",")
251 }
Colin Crossa4f08812018-10-02 22:03:40 -0700252 ctx.Build(pctx, android.BuildParams{
Kousik Kumar366afc52020-05-20 11:27:16 -0700253 Rule: rule,
Colin Crossa4f08812018-10-02 22:03:40 -0700254 Description: "zip jni libs",
255 Output: outputFile,
256 Implicits: deps,
Kousik Kumar366afc52020-05-20 11:27:16 -0700257 Args: args,
Colin Crossa4f08812018-10-02 22:03:40 -0700258 })
259}
260
Matt Banda8c801262022-04-01 17:48:31 +0000261func (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
Colin Crossa4f08812018-10-02 22:03:40 -0700273func targetToJniDir(target android.Target) string {
274 return filepath.Join("lib", target.Arch.Abi[0])
275}