blob: fb9ab4295781579540fe5b349be232c10da85950 [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"
Colin Cross30e076a2015-04-13 13:58:27 -070029)
30
31var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +090032 Signapk = pctx.AndroidStaticRule("signapk",
Colin Cross30e076a2015-04-13 13:58:27 -070033 blueprint.RuleParams{
Colin Crossbdf95142019-08-08 12:56:34 -070034 Command: `${config.JavaCmd} ${config.JavaVmFlags} -Djava.library.path=$$(dirname ${config.SignapkJniLibrary}) ` +
35 `-jar ${config.SignapkCmd} $flags $certificates $in $out`,
36 CommandDeps: []string{"${config.SignapkCmd}", "${config.SignapkJniLibrary}"},
Colin Cross30e076a2015-04-13 13:58:27 -070037 },
Jiyong Parkbfe64a12018-11-22 02:51:54 +090038 "flags", "certificates")
Colin Cross30e076a2015-04-13 13:58:27 -070039)
40
Colin Cross3bc7ffa2017-11-22 16:19:37 -080041var combineApk = pctx.AndroidStaticRule("combineApk",
42 blueprint.RuleParams{
43 Command: `${config.MergeZipsCmd} $out $in`,
44 CommandDeps: []string{"${config.MergeZipsCmd}"},
Colin Cross30e076a2015-04-13 13:58:27 -070045 })
46
Jaewoong Jungccbb3932019-04-15 09:48:31 -070047func CreateAndSignAppPackage(ctx android.ModuleContext, outputFile android.WritablePath,
Liz Kammere2b27f42020-05-07 13:24:05 -070048 packageFile, jniJarFile, dexJarFile android.Path, certificates []Certificate, deps android.Paths, lineageFile android.Path) {
Colin Cross3bc7ffa2017-11-22 16:19:37 -080049
Colin Crosse560c4a2019-03-19 16:03:11 -070050 unsignedApkName := strings.TrimSuffix(outputFile.Base(), ".apk") + "-unsigned.apk"
51 unsignedApk := android.PathForModuleOut(ctx, unsignedApkName)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080052
Colin Crossa4f08812018-10-02 22:03:40 -070053 var inputs android.Paths
Colin Cross3bc7ffa2017-11-22 16:19:37 -080054 if dexJarFile != nil {
55 inputs = append(inputs, dexJarFile)
56 }
Colin Crossfd94c402018-11-01 14:50:55 -070057 inputs = append(inputs, packageFile)
Colin Crossa4f08812018-10-02 22:03:40 -070058 if jniJarFile != nil {
59 inputs = append(inputs, jniJarFile)
60 }
Colin Cross30e076a2015-04-13 13:58:27 -070061
Colin Crossae887032017-10-23 17:16:14 -070062 ctx.Build(pctx, android.BuildParams{
Colin Cross50ddcc42019-05-16 12:28:22 -070063 Rule: combineApk,
64 Inputs: inputs,
65 Output: unsignedApk,
66 Implicits: deps,
Colin Cross30e076a2015-04-13 13:58:27 -070067 })
68
Liz Kammere2b27f42020-05-07 13:24:05 -070069 SignAppPackage(ctx, outputFile, unsignedApk, certificates, lineageFile)
Jaewoong Jungccbb3932019-04-15 09:48:31 -070070}
71
Liz Kammere2b27f42020-05-07 13:24:05 -070072func SignAppPackage(ctx android.ModuleContext, signedApk android.WritablePath, unsignedApk android.Path, certificates []Certificate, lineageFile android.Path) {
Jaewoong Jungccbb3932019-04-15 09:48:31 -070073
Colin Cross30e076a2015-04-13 13:58:27 -070074 var certificateArgs []string
Dan Willemsenc4bd8f82019-04-09 21:26:14 -070075 var deps android.Paths
Colin Cross30e076a2015-04-13 13:58:27 -070076 for _, c := range certificates {
Jiyong Parkc00cbd92018-10-30 21:20:05 +090077 certificateArgs = append(certificateArgs, c.Pem.String(), c.Key.String())
Dan Willemsenc4bd8f82019-04-09 21:26:14 -070078 deps = append(deps, c.Pem, c.Key)
Colin Cross30e076a2015-04-13 13:58:27 -070079 }
80
Liz Kammere2b27f42020-05-07 13:24:05 -070081 var flags []string
82 if lineageFile != nil {
83 flags = append(flags, "--lineage", lineageFile.String())
Liz Kammera7126552020-05-08 17:19:26 -070084 deps = append(deps, lineageFile)
Liz Kammere2b27f42020-05-07 13:24:05 -070085 }
86
Colin Crossae887032017-10-23 17:16:14 -070087 ctx.Build(pctx, android.BuildParams{
Jiyong Parkc00cbd92018-10-30 21:20:05 +090088 Rule: Signapk,
Colin Cross67a5c132017-05-09 13:45:28 -070089 Description: "signapk",
Songchun Fan4b25e312020-04-06 18:28:17 +000090 Output: signedApk,
Colin Cross3bc7ffa2017-11-22 16:19:37 -080091 Input: unsignedApk,
Dan Willemsenc4bd8f82019-04-09 21:26:14 -070092 Implicits: deps,
Colin Cross30e076a2015-04-13 13:58:27 -070093 Args: map[string]string{
94 "certificates": strings.Join(certificateArgs, " "),
Liz Kammere2b27f42020-05-07 13:24:05 -070095 "flags": strings.Join(flags, " "),
Colin Cross30e076a2015-04-13 13:58:27 -070096 },
97 })
Colin Cross30e076a2015-04-13 13:58:27 -070098}
Colin Crossa97c5d32018-03-28 14:58:31 -070099
100var buildAAR = pctx.AndroidStaticRule("buildAAR",
101 blueprint.RuleParams{
102 Command: `rm -rf ${outDir} && mkdir -p ${outDir} && ` +
103 `cp ${manifest} ${outDir}/AndroidManifest.xml && ` +
104 `cp ${classesJar} ${outDir}/classes.jar && ` +
105 `cp ${rTxt} ${outDir}/R.txt && ` +
Colin Cross1d98ee22018-09-18 17:05:15 -0700106 `${config.SoongZipCmd} -jar -o $out -C ${outDir} -D ${outDir}`,
Colin Crossa97c5d32018-03-28 14:58:31 -0700107 CommandDeps: []string{"${config.SoongZipCmd}"},
108 },
Colin Cross1d98ee22018-09-18 17:05:15 -0700109 "manifest", "classesJar", "rTxt", "outDir")
Colin Crossa97c5d32018-03-28 14:58:31 -0700110
111func BuildAAR(ctx android.ModuleContext, outputFile android.WritablePath,
112 classesJar, manifest, rTxt android.Path, res android.Paths) {
113
114 // TODO(ccross): uniquify and copy resources with dependencies
115
116 deps := android.Paths{manifest, rTxt}
117 classesJarPath := ""
118 if classesJar != nil {
119 deps = append(deps, classesJar)
120 classesJarPath = classesJar.String()
121 }
122
123 ctx.Build(pctx, android.BuildParams{
Colin Crossf57c5782019-01-25 13:20:38 -0800124 Rule: buildAAR,
125 Description: "aar",
126 Implicits: deps,
127 Output: outputFile,
Colin Crossa97c5d32018-03-28 14:58:31 -0700128 Args: map[string]string{
129 "manifest": manifest.String(),
130 "classesJar": classesJarPath,
131 "rTxt": rTxt.String(),
132 "outDir": android.PathForModuleOut(ctx, "aar").String(),
133 },
134 })
135}
Colin Crossa4f08812018-10-02 22:03:40 -0700136
Colin Crossf6237212018-10-29 23:14:58 -0700137var buildBundleModule = pctx.AndroidStaticRule("buildBundleModule",
138 blueprint.RuleParams{
Colin Crossfd94c402018-11-01 14:50:55 -0700139 Command: `${config.MergeZipsCmd} ${out} ${in}`,
140 CommandDeps: []string{"${config.MergeZipsCmd}"},
141 })
142
143var bundleMungePackage = pctx.AndroidStaticRule("bundleMungePackage",
144 blueprint.RuleParams{
145 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} AndroidManifest.xml:manifest/AndroidManifest.xml resources.pb "res/**/*" "assets/**/*"`,
146 CommandDeps: []string{"${config.Zip2ZipCmd}"},
147 })
148
149var bundleMungeDexJar = pctx.AndroidStaticRule("bundleMungeDexJar",
150 blueprint.RuleParams{
151 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "classes*.dex:dex/" && ` +
152 `${config.Zip2ZipCmd} -i ${in} -o ${resJar} -x "classes*.dex" "**/*:root/"`,
153 CommandDeps: []string{"${config.Zip2ZipCmd}"},
154 }, "resJar")
Colin Crossf6237212018-10-29 23:14:58 -0700155
156// Builds an app into a module suitable for input to bundletool
157func BuildBundleModule(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Crossfd94c402018-11-01 14:50:55 -0700158 packageFile, jniJarFile, dexJarFile android.Path) {
Colin Crossf6237212018-10-29 23:14:58 -0700159
160 protoResJarFile := android.PathForModuleOut(ctx, "package-res.pb.apk")
Colin Crossfd94c402018-11-01 14:50:55 -0700161 aapt2Convert(ctx, protoResJarFile, packageFile)
Colin Crossf6237212018-10-29 23:14:58 -0700162
Colin Crossfd94c402018-11-01 14:50:55 -0700163 var zips android.Paths
164
165 mungedPackage := android.PathForModuleOut(ctx, "bundle", "apk.zip")
166 ctx.Build(pctx, android.BuildParams{
167 Rule: bundleMungePackage,
168 Input: protoResJarFile,
169 Output: mungedPackage,
170 Description: "bundle apk",
171 })
172 zips = append(zips, mungedPackage)
173
Colin Crossf6237212018-10-29 23:14:58 -0700174 if dexJarFile != nil {
Colin Crossfd94c402018-11-01 14:50:55 -0700175 mungedDexJar := android.PathForModuleOut(ctx, "bundle", "dex.zip")
176 mungedResJar := android.PathForModuleOut(ctx, "bundle", "res.zip")
177 ctx.Build(pctx, android.BuildParams{
178 Rule: bundleMungeDexJar,
179 Input: dexJarFile,
180 Output: mungedDexJar,
181 ImplicitOutput: mungedResJar,
182 Description: "bundle dex",
183 Args: map[string]string{
184 "resJar": mungedResJar.String(),
185 },
186 })
187 zips = append(zips, mungedDexJar, mungedResJar)
Colin Crossf6237212018-10-29 23:14:58 -0700188 }
189 if jniJarFile != nil {
Colin Crossfd94c402018-11-01 14:50:55 -0700190 zips = append(zips, jniJarFile)
Colin Crossf6237212018-10-29 23:14:58 -0700191 }
192
193 ctx.Build(pctx, android.BuildParams{
194 Rule: buildBundleModule,
Colin Crossfd94c402018-11-01 14:50:55 -0700195 Inputs: zips,
Colin Crossf6237212018-10-29 23:14:58 -0700196 Output: outputFile,
197 Description: "bundle",
Colin Crossf6237212018-10-29 23:14:58 -0700198 })
199}
200
Colin Crossa4f08812018-10-02 22:03:40 -0700201func TransformJniLibsToJar(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Crosse4246ab2019-02-05 21:55:21 -0800202 jniLibs []jniLib, uncompressJNI bool) {
Colin Crossa4f08812018-10-02 22:03:40 -0700203
204 var deps android.Paths
205 jarArgs := []string{
206 "-j", // junk paths, they will be added back with -P arguments
207 }
208
Colin Crosse4246ab2019-02-05 21:55:21 -0800209 if uncompressJNI {
Peter Collingbournead84f972019-12-17 16:46:18 -0800210 jarArgs = append(jarArgs, "-L", "0")
Colin Crossa4f08812018-10-02 22:03:40 -0700211 }
212
213 for _, j := range jniLibs {
214 deps = append(deps, j.path)
215 jarArgs = append(jarArgs,
Peter Collingbournead84f972019-12-17 16:46:18 -0800216 "-P", targetToJniDir(j.target),
217 "-f", j.path.String())
Colin Crossa4f08812018-10-02 22:03:40 -0700218 }
219
220 ctx.Build(pctx, android.BuildParams{
221 Rule: zip,
222 Description: "zip jni libs",
223 Output: outputFile,
224 Implicits: deps,
225 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -0800226 "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
Colin Crossa4f08812018-10-02 22:03:40 -0700227 },
228 })
229}
230
231func targetToJniDir(target android.Target) string {
232 return filepath.Join("lib", target.Arch.Abi[0])
233}