blob: 5e7fbe6de357ad2897b36c6db2284c7835c9df6c [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,
Colin Cross50ddcc42019-05-16 12:28:22 -070048 packageFile, jniJarFile, dexJarFile android.Path, certificates []Certificate, deps android.Paths) {
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
Jaewoong Jungccbb3932019-04-15 09:48:31 -070069 SignAppPackage(ctx, outputFile, unsignedApk, certificates)
70}
71
72func SignAppPackage(ctx android.ModuleContext, signedApk android.WritablePath, unsignedApk android.Path, certificates []Certificate) {
73
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
Colin Crossae887032017-10-23 17:16:14 -070081 ctx.Build(pctx, android.BuildParams{
Jiyong Parkc00cbd92018-10-30 21:20:05 +090082 Rule: Signapk,
Colin Cross67a5c132017-05-09 13:45:28 -070083 Description: "signapk",
Jaewoong Jungccbb3932019-04-15 09:48:31 -070084 Output: signedApk,
Colin Cross3bc7ffa2017-11-22 16:19:37 -080085 Input: unsignedApk,
Dan Willemsenc4bd8f82019-04-09 21:26:14 -070086 Implicits: deps,
Colin Cross30e076a2015-04-13 13:58:27 -070087 Args: map[string]string{
88 "certificates": strings.Join(certificateArgs, " "),
89 },
90 })
Colin Cross30e076a2015-04-13 13:58:27 -070091}
Colin Crossa97c5d32018-03-28 14:58:31 -070092
93var buildAAR = pctx.AndroidStaticRule("buildAAR",
94 blueprint.RuleParams{
95 Command: `rm -rf ${outDir} && mkdir -p ${outDir} && ` +
96 `cp ${manifest} ${outDir}/AndroidManifest.xml && ` +
97 `cp ${classesJar} ${outDir}/classes.jar && ` +
98 `cp ${rTxt} ${outDir}/R.txt && ` +
Colin Cross1d98ee22018-09-18 17:05:15 -070099 `${config.SoongZipCmd} -jar -o $out -C ${outDir} -D ${outDir}`,
Colin Crossa97c5d32018-03-28 14:58:31 -0700100 CommandDeps: []string{"${config.SoongZipCmd}"},
101 },
Colin Cross1d98ee22018-09-18 17:05:15 -0700102 "manifest", "classesJar", "rTxt", "outDir")
Colin Crossa97c5d32018-03-28 14:58:31 -0700103
104func BuildAAR(ctx android.ModuleContext, outputFile android.WritablePath,
105 classesJar, manifest, rTxt android.Path, res android.Paths) {
106
107 // TODO(ccross): uniquify and copy resources with dependencies
108
109 deps := android.Paths{manifest, rTxt}
110 classesJarPath := ""
111 if classesJar != nil {
112 deps = append(deps, classesJar)
113 classesJarPath = classesJar.String()
114 }
115
116 ctx.Build(pctx, android.BuildParams{
Colin Crossf57c5782019-01-25 13:20:38 -0800117 Rule: buildAAR,
118 Description: "aar",
119 Implicits: deps,
120 Output: outputFile,
Colin Crossa97c5d32018-03-28 14:58:31 -0700121 Args: map[string]string{
122 "manifest": manifest.String(),
123 "classesJar": classesJarPath,
124 "rTxt": rTxt.String(),
125 "outDir": android.PathForModuleOut(ctx, "aar").String(),
126 },
127 })
128}
Colin Crossa4f08812018-10-02 22:03:40 -0700129
Colin Crossf6237212018-10-29 23:14:58 -0700130var buildBundleModule = pctx.AndroidStaticRule("buildBundleModule",
131 blueprint.RuleParams{
Colin Crossfd94c402018-11-01 14:50:55 -0700132 Command: `${config.MergeZipsCmd} ${out} ${in}`,
133 CommandDeps: []string{"${config.MergeZipsCmd}"},
134 })
135
136var bundleMungePackage = pctx.AndroidStaticRule("bundleMungePackage",
137 blueprint.RuleParams{
138 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} AndroidManifest.xml:manifest/AndroidManifest.xml resources.pb "res/**/*" "assets/**/*"`,
139 CommandDeps: []string{"${config.Zip2ZipCmd}"},
140 })
141
142var bundleMungeDexJar = pctx.AndroidStaticRule("bundleMungeDexJar",
143 blueprint.RuleParams{
144 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "classes*.dex:dex/" && ` +
145 `${config.Zip2ZipCmd} -i ${in} -o ${resJar} -x "classes*.dex" "**/*:root/"`,
146 CommandDeps: []string{"${config.Zip2ZipCmd}"},
147 }, "resJar")
Colin Crossf6237212018-10-29 23:14:58 -0700148
149// Builds an app into a module suitable for input to bundletool
150func BuildBundleModule(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Crossfd94c402018-11-01 14:50:55 -0700151 packageFile, jniJarFile, dexJarFile android.Path) {
Colin Crossf6237212018-10-29 23:14:58 -0700152
153 protoResJarFile := android.PathForModuleOut(ctx, "package-res.pb.apk")
Colin Crossfd94c402018-11-01 14:50:55 -0700154 aapt2Convert(ctx, protoResJarFile, packageFile)
Colin Crossf6237212018-10-29 23:14:58 -0700155
Colin Crossfd94c402018-11-01 14:50:55 -0700156 var zips android.Paths
157
158 mungedPackage := android.PathForModuleOut(ctx, "bundle", "apk.zip")
159 ctx.Build(pctx, android.BuildParams{
160 Rule: bundleMungePackage,
161 Input: protoResJarFile,
162 Output: mungedPackage,
163 Description: "bundle apk",
164 })
165 zips = append(zips, mungedPackage)
166
Colin Crossf6237212018-10-29 23:14:58 -0700167 if dexJarFile != nil {
Colin Crossfd94c402018-11-01 14:50:55 -0700168 mungedDexJar := android.PathForModuleOut(ctx, "bundle", "dex.zip")
169 mungedResJar := android.PathForModuleOut(ctx, "bundle", "res.zip")
170 ctx.Build(pctx, android.BuildParams{
171 Rule: bundleMungeDexJar,
172 Input: dexJarFile,
173 Output: mungedDexJar,
174 ImplicitOutput: mungedResJar,
175 Description: "bundle dex",
176 Args: map[string]string{
177 "resJar": mungedResJar.String(),
178 },
179 })
180 zips = append(zips, mungedDexJar, mungedResJar)
Colin Crossf6237212018-10-29 23:14:58 -0700181 }
182 if jniJarFile != nil {
Colin Crossfd94c402018-11-01 14:50:55 -0700183 zips = append(zips, jniJarFile)
Colin Crossf6237212018-10-29 23:14:58 -0700184 }
185
186 ctx.Build(pctx, android.BuildParams{
187 Rule: buildBundleModule,
Colin Crossfd94c402018-11-01 14:50:55 -0700188 Inputs: zips,
Colin Crossf6237212018-10-29 23:14:58 -0700189 Output: outputFile,
190 Description: "bundle",
Colin Crossf6237212018-10-29 23:14:58 -0700191 })
192}
193
Colin Crossa4f08812018-10-02 22:03:40 -0700194func TransformJniLibsToJar(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Crosse4246ab2019-02-05 21:55:21 -0800195 jniLibs []jniLib, uncompressJNI bool) {
Colin Crossa4f08812018-10-02 22:03:40 -0700196
197 var deps android.Paths
198 jarArgs := []string{
199 "-j", // junk paths, they will be added back with -P arguments
200 }
201
Colin Crosse4246ab2019-02-05 21:55:21 -0800202 if uncompressJNI {
Peter Collingbournead84f972019-12-17 16:46:18 -0800203 jarArgs = append(jarArgs, "-L", "0")
Colin Crossa4f08812018-10-02 22:03:40 -0700204 }
205
206 for _, j := range jniLibs {
207 deps = append(deps, j.path)
208 jarArgs = append(jarArgs,
Peter Collingbournead84f972019-12-17 16:46:18 -0800209 "-P", targetToJniDir(j.target),
210 "-f", j.path.String())
Colin Crossa4f08812018-10-02 22:03:40 -0700211 }
212
213 ctx.Build(pctx, android.BuildParams{
214 Rule: zip,
215 Description: "zip jni libs",
216 Output: outputFile,
217 Implicits: deps,
218 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -0800219 "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
Colin Crossa4f08812018-10-02 22:03:40 -0700220 },
221 })
222}
223
224func targetToJniDir(target android.Target) string {
225 return filepath.Join("lib", target.Arch.Abi[0])
226}