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 | 70dd74d | 2020-05-07 13:24:05 -0700 | [diff] [blame^] | 48 | packageFile, jniJarFile, dexJarFile android.Path, certificates []Certificate, deps android.Paths, v4SignatureFile android.WritablePath, 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 | 70dd74d | 2020-05-07 13:24:05 -0700 | [diff] [blame^] | 69 | SignAppPackage(ctx, outputFile, unsignedApk, certificates, v4SignatureFile, lineageFile) |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Liz Kammer | 70dd74d | 2020-05-07 13:24:05 -0700 | [diff] [blame^] | 72 | func SignAppPackage(ctx android.ModuleContext, signedApk android.WritablePath, unsignedApk android.Path, certificates []Certificate, v4SignatureFile android.WritablePath, 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 | |
Songchun Fan | 688de9a | 2020-03-24 20:32:24 -0700 | [diff] [blame] | 81 | outputFiles := android.WritablePaths{signedApk} |
Liz Kammer | 70dd74d | 2020-05-07 13:24:05 -0700 | [diff] [blame^] | 82 | var flags []string |
Songchun Fan | 688de9a | 2020-03-24 20:32:24 -0700 | [diff] [blame] | 83 | if v4SignatureFile != nil { |
| 84 | outputFiles = append(outputFiles, v4SignatureFile) |
Liz Kammer | 70dd74d | 2020-05-07 13:24:05 -0700 | [diff] [blame^] | 85 | flags = append(flags, "--enable-v4") |
| 86 | } |
| 87 | |
| 88 | if lineageFile != nil { |
| 89 | flags = append(flags, "--lineage", lineageFile.String()) |
Songchun Fan | 688de9a | 2020-03-24 20:32:24 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 92 | ctx.Build(pctx, android.BuildParams{ |
Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 93 | Rule: Signapk, |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 94 | Description: "signapk", |
Songchun Fan | 688de9a | 2020-03-24 20:32:24 -0700 | [diff] [blame] | 95 | Outputs: outputFiles, |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 96 | Input: unsignedApk, |
Dan Willemsen | c4bd8f8 | 2019-04-09 21:26:14 -0700 | [diff] [blame] | 97 | Implicits: deps, |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 98 | Args: map[string]string{ |
| 99 | "certificates": strings.Join(certificateArgs, " "), |
Liz Kammer | 70dd74d | 2020-05-07 13:24:05 -0700 | [diff] [blame^] | 100 | "flags": strings.Join(flags, " "), |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 101 | }, |
| 102 | }) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 103 | } |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 104 | |
| 105 | var buildAAR = pctx.AndroidStaticRule("buildAAR", |
| 106 | blueprint.RuleParams{ |
| 107 | Command: `rm -rf ${outDir} && mkdir -p ${outDir} && ` + |
| 108 | `cp ${manifest} ${outDir}/AndroidManifest.xml && ` + |
| 109 | `cp ${classesJar} ${outDir}/classes.jar && ` + |
| 110 | `cp ${rTxt} ${outDir}/R.txt && ` + |
Colin Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 111 | `${config.SoongZipCmd} -jar -o $out -C ${outDir} -D ${outDir}`, |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 112 | CommandDeps: []string{"${config.SoongZipCmd}"}, |
| 113 | }, |
Colin Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 114 | "manifest", "classesJar", "rTxt", "outDir") |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 115 | |
| 116 | func BuildAAR(ctx android.ModuleContext, outputFile android.WritablePath, |
| 117 | classesJar, manifest, rTxt android.Path, res android.Paths) { |
| 118 | |
| 119 | // TODO(ccross): uniquify and copy resources with dependencies |
| 120 | |
| 121 | deps := android.Paths{manifest, rTxt} |
| 122 | classesJarPath := "" |
| 123 | if classesJar != nil { |
| 124 | deps = append(deps, classesJar) |
| 125 | classesJarPath = classesJar.String() |
| 126 | } |
| 127 | |
| 128 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | f57c578 | 2019-01-25 13:20:38 -0800 | [diff] [blame] | 129 | Rule: buildAAR, |
| 130 | Description: "aar", |
| 131 | Implicits: deps, |
| 132 | Output: outputFile, |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 133 | Args: map[string]string{ |
| 134 | "manifest": manifest.String(), |
| 135 | "classesJar": classesJarPath, |
| 136 | "rTxt": rTxt.String(), |
| 137 | "outDir": android.PathForModuleOut(ctx, "aar").String(), |
| 138 | }, |
| 139 | }) |
| 140 | } |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 141 | |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 142 | var buildBundleModule = pctx.AndroidStaticRule("buildBundleModule", |
| 143 | blueprint.RuleParams{ |
Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 144 | Command: `${config.MergeZipsCmd} ${out} ${in}`, |
| 145 | CommandDeps: []string{"${config.MergeZipsCmd}"}, |
| 146 | }) |
| 147 | |
| 148 | var bundleMungePackage = pctx.AndroidStaticRule("bundleMungePackage", |
| 149 | blueprint.RuleParams{ |
| 150 | Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} AndroidManifest.xml:manifest/AndroidManifest.xml resources.pb "res/**/*" "assets/**/*"`, |
| 151 | CommandDeps: []string{"${config.Zip2ZipCmd}"}, |
| 152 | }) |
| 153 | |
| 154 | var bundleMungeDexJar = pctx.AndroidStaticRule("bundleMungeDexJar", |
| 155 | blueprint.RuleParams{ |
| 156 | Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "classes*.dex:dex/" && ` + |
| 157 | `${config.Zip2ZipCmd} -i ${in} -o ${resJar} -x "classes*.dex" "**/*:root/"`, |
| 158 | CommandDeps: []string{"${config.Zip2ZipCmd}"}, |
| 159 | }, "resJar") |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 160 | |
| 161 | // Builds an app into a module suitable for input to bundletool |
| 162 | func BuildBundleModule(ctx android.ModuleContext, outputFile android.WritablePath, |
Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 163 | packageFile, jniJarFile, dexJarFile android.Path) { |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 164 | |
| 165 | protoResJarFile := android.PathForModuleOut(ctx, "package-res.pb.apk") |
Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 166 | aapt2Convert(ctx, protoResJarFile, packageFile) |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 167 | |
Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 168 | var zips android.Paths |
| 169 | |
| 170 | mungedPackage := android.PathForModuleOut(ctx, "bundle", "apk.zip") |
| 171 | ctx.Build(pctx, android.BuildParams{ |
| 172 | Rule: bundleMungePackage, |
| 173 | Input: protoResJarFile, |
| 174 | Output: mungedPackage, |
| 175 | Description: "bundle apk", |
| 176 | }) |
| 177 | zips = append(zips, mungedPackage) |
| 178 | |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 179 | if dexJarFile != nil { |
Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 180 | mungedDexJar := android.PathForModuleOut(ctx, "bundle", "dex.zip") |
| 181 | mungedResJar := android.PathForModuleOut(ctx, "bundle", "res.zip") |
| 182 | ctx.Build(pctx, android.BuildParams{ |
| 183 | Rule: bundleMungeDexJar, |
| 184 | Input: dexJarFile, |
| 185 | Output: mungedDexJar, |
| 186 | ImplicitOutput: mungedResJar, |
| 187 | Description: "bundle dex", |
| 188 | Args: map[string]string{ |
| 189 | "resJar": mungedResJar.String(), |
| 190 | }, |
| 191 | }) |
| 192 | zips = append(zips, mungedDexJar, mungedResJar) |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 193 | } |
| 194 | if jniJarFile != nil { |
Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 195 | zips = append(zips, jniJarFile) |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | ctx.Build(pctx, android.BuildParams{ |
| 199 | Rule: buildBundleModule, |
Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 200 | Inputs: zips, |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 201 | Output: outputFile, |
| 202 | Description: "bundle", |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 203 | }) |
| 204 | } |
| 205 | |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 206 | func TransformJniLibsToJar(ctx android.ModuleContext, outputFile android.WritablePath, |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 207 | jniLibs []jniLib, uncompressJNI bool) { |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 208 | |
| 209 | var deps android.Paths |
| 210 | jarArgs := []string{ |
| 211 | "-j", // junk paths, they will be added back with -P arguments |
| 212 | } |
| 213 | |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 214 | if uncompressJNI { |
Peter Collingbourne | ad84f97 | 2019-12-17 16:46:18 -0800 | [diff] [blame] | 215 | jarArgs = append(jarArgs, "-L", "0") |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | for _, j := range jniLibs { |
| 219 | deps = append(deps, j.path) |
| 220 | jarArgs = append(jarArgs, |
Peter Collingbourne | ad84f97 | 2019-12-17 16:46:18 -0800 | [diff] [blame] | 221 | "-P", targetToJniDir(j.target), |
| 222 | "-f", j.path.String()) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | ctx.Build(pctx, android.BuildParams{ |
| 226 | Rule: zip, |
| 227 | Description: "zip jni libs", |
| 228 | Output: outputFile, |
| 229 | Implicits: deps, |
| 230 | Args: map[string]string{ |
Colin Cross | 0b9f31f | 2019-02-28 11:00:01 -0800 | [diff] [blame] | 231 | "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "), |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 232 | }, |
| 233 | }) |
| 234 | } |
| 235 | |
| 236 | func targetToJniDir(target android.Target) string { |
| 237 | return filepath.Join("lib", target.Arch.Abi[0]) |
| 238 | } |