Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 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 | |
| 15 | package 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 | |
| 21 | import ( |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 22 | "path/filepath" |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 23 | "strings" |
| 24 | |
| 25 | "github.com/google/blueprint" |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 26 | "github.com/google/blueprint/proptools" |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 27 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 28 | "android/soong/android" |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | var ( |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 32 | Signapk = pctx.AndroidStaticRule("signapk", |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 33 | blueprint.RuleParams{ |
Colin Cross | bdf9514 | 2019-08-08 12:56:34 -0700 | [diff] [blame] | 34 | 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 Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 37 | }, |
Jiyong Park | bfe64a1 | 2018-11-22 02:51:54 +0900 | [diff] [blame] | 38 | "flags", "certificates") |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 39 | ) |
| 40 | |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 41 | var combineApk = pctx.AndroidStaticRule("combineApk", |
| 42 | blueprint.RuleParams{ |
| 43 | Command: `${config.MergeZipsCmd} $out $in`, |
| 44 | CommandDeps: []string{"${config.MergeZipsCmd}"}, |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 45 | }) |
| 46 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 47 | func CreateAndSignAppPackage(ctx android.ModuleContext, outputFile android.WritablePath, |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 48 | packageFile, jniJarFile, dexJarFile android.Path, certificates []Certificate, deps android.Paths, lineageFile android.Path) { |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 49 | |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 50 | unsignedApkName := strings.TrimSuffix(outputFile.Base(), ".apk") + "-unsigned.apk" |
| 51 | unsignedApk := android.PathForModuleOut(ctx, unsignedApkName) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 52 | |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 53 | var inputs android.Paths |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 54 | if dexJarFile != nil { |
| 55 | inputs = append(inputs, dexJarFile) |
| 56 | } |
Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 57 | inputs = append(inputs, packageFile) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 58 | if jniJarFile != nil { |
| 59 | inputs = append(inputs, jniJarFile) |
| 60 | } |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 61 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 62 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 63 | Rule: combineApk, |
| 64 | Inputs: inputs, |
| 65 | Output: unsignedApk, |
| 66 | Implicits: deps, |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 67 | }) |
| 68 | |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 69 | SignAppPackage(ctx, outputFile, unsignedApk, certificates, lineageFile) |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 72 | func SignAppPackage(ctx android.ModuleContext, signedApk android.WritablePath, unsignedApk android.Path, certificates []Certificate, lineageFile android.Path) { |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 73 | |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 74 | var certificateArgs []string |
Dan Willemsen | c4bd8f8 | 2019-04-09 21:26:14 -0700 | [diff] [blame] | 75 | var deps android.Paths |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 76 | for _, c := range certificates { |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 77 | certificateArgs = append(certificateArgs, c.Pem.String(), c.Key.String()) |
Dan Willemsen | c4bd8f8 | 2019-04-09 21:26:14 -0700 | [diff] [blame] | 78 | deps = append(deps, c.Pem, c.Key) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 81 | var flags []string |
| 82 | if lineageFile != nil { |
| 83 | flags = append(flags, "--lineage", lineageFile.String()) |
Liz Kammer | a712655 | 2020-05-08 17:19:26 -0700 | [diff] [blame] | 84 | deps = append(deps, lineageFile) |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 87 | ctx.Build(pctx, android.BuildParams{ |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 88 | Rule: Signapk, |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 89 | Description: "signapk", |
Songchun Fan | 4b25e31 | 2020-04-06 18:28:17 +0000 | [diff] [blame] | 90 | Output: signedApk, |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 91 | Input: unsignedApk, |
Dan Willemsen | c4bd8f8 | 2019-04-09 21:26:14 -0700 | [diff] [blame] | 92 | Implicits: deps, |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 93 | Args: map[string]string{ |
| 94 | "certificates": strings.Join(certificateArgs, " "), |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 95 | "flags": strings.Join(flags, " "), |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 96 | }, |
| 97 | }) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 98 | } |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 99 | |
| 100 | var 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 Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 106 | `${config.SoongZipCmd} -jar -o $out -C ${outDir} -D ${outDir}`, |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 107 | CommandDeps: []string{"${config.SoongZipCmd}"}, |
| 108 | }, |
Colin Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 109 | "manifest", "classesJar", "rTxt", "outDir") |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 110 | |
| 111 | func 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 Cross | f57c578 | 2019-01-25 13:20:38 -0800 | [diff] [blame] | 124 | Rule: buildAAR, |
| 125 | Description: "aar", |
| 126 | Implicits: deps, |
| 127 | Output: outputFile, |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 128 | 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 Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 136 | |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 137 | var buildBundleModule = pctx.AndroidStaticRule("buildBundleModule", |
| 138 | blueprint.RuleParams{ |
Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 139 | Command: `${config.MergeZipsCmd} ${out} ${in}`, |
| 140 | CommandDeps: []string{"${config.MergeZipsCmd}"}, |
| 141 | }) |
| 142 | |
| 143 | var 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 | |
| 149 | var 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 Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 155 | |
| 156 | // Builds an app into a module suitable for input to bundletool |
| 157 | func BuildBundleModule(ctx android.ModuleContext, outputFile android.WritablePath, |
Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 158 | packageFile, jniJarFile, dexJarFile android.Path) { |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 159 | |
| 160 | protoResJarFile := android.PathForModuleOut(ctx, "package-res.pb.apk") |
Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 161 | aapt2Convert(ctx, protoResJarFile, packageFile) |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 162 | |
Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 163 | 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 Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 174 | if dexJarFile != nil { |
Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 175 | 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 Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 188 | } |
| 189 | if jniJarFile != nil { |
Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 190 | zips = append(zips, jniJarFile) |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | ctx.Build(pctx, android.BuildParams{ |
| 194 | Rule: buildBundleModule, |
Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 195 | Inputs: zips, |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 196 | Output: outputFile, |
| 197 | Description: "bundle", |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 198 | }) |
| 199 | } |
| 200 | |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 201 | func TransformJniLibsToJar(ctx android.ModuleContext, outputFile android.WritablePath, |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 202 | jniLibs []jniLib, uncompressJNI bool) { |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 203 | |
| 204 | var deps android.Paths |
| 205 | jarArgs := []string{ |
| 206 | "-j", // junk paths, they will be added back with -P arguments |
| 207 | } |
| 208 | |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 209 | if uncompressJNI { |
Peter Collingbourne | ad84f97 | 2019-12-17 16:46:18 -0800 | [diff] [blame] | 210 | jarArgs = append(jarArgs, "-L", "0") |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | for _, j := range jniLibs { |
| 214 | deps = append(deps, j.path) |
| 215 | jarArgs = append(jarArgs, |
Peter Collingbourne | ad84f97 | 2019-12-17 16:46:18 -0800 | [diff] [blame] | 216 | "-P", targetToJniDir(j.target), |
| 217 | "-f", j.path.String()) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 218 | } |
| 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 Cross | 0b9f31f | 2019-02-28 11:00:01 -0800 | [diff] [blame] | 226 | "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "), |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 227 | }, |
| 228 | }) |
| 229 | } |
| 230 | |
| 231 | func targetToJniDir(target android.Target) string { |
| 232 | return filepath.Join("lib", target.Arch.Abi[0]) |
| 233 | } |