| 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, | 
| Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 48 | packageFile, jniJarFile, dexJarFile android.Path, certificates []Certificate, deps android.Paths) { | 
| 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 |  | 
| Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 69 | SignAppPackage(ctx, outputFile, unsignedApk, certificates) | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | func SignAppPackage(ctx android.ModuleContext, signedApk android.WritablePath, unsignedApk android.Path, certificates []Certificate) { | 
|  | 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 |  | 
| Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 81 | ctx.Build(pctx, android.BuildParams{ | 
| Jiyong Park | c00cbd9 | 2018-10-30 21:20:05 +0900 | [diff] [blame] | 82 | Rule:        Signapk, | 
| Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 83 | Description: "signapk", | 
| Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 84 | Output:      signedApk, | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 85 | Input:       unsignedApk, | 
| Dan Willemsen | c4bd8f8 | 2019-04-09 21:26:14 -0700 | [diff] [blame] | 86 | Implicits:   deps, | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 87 | Args: map[string]string{ | 
|  | 88 | "certificates": strings.Join(certificateArgs, " "), | 
|  | 89 | }, | 
|  | 90 | }) | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 91 | } | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 92 |  | 
|  | 93 | var 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 Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 99 | `${config.SoongZipCmd} -jar -o $out -C ${outDir} -D ${outDir}`, | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 100 | CommandDeps: []string{"${config.SoongZipCmd}"}, | 
|  | 101 | }, | 
| Colin Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 102 | "manifest", "classesJar", "rTxt", "outDir") | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 103 |  | 
|  | 104 | func 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 Cross | f57c578 | 2019-01-25 13:20:38 -0800 | [diff] [blame] | 117 | Rule:        buildAAR, | 
|  | 118 | Description: "aar", | 
|  | 119 | Implicits:   deps, | 
|  | 120 | Output:      outputFile, | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 121 | 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 Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 129 |  | 
| Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 130 | var buildBundleModule = pctx.AndroidStaticRule("buildBundleModule", | 
|  | 131 | blueprint.RuleParams{ | 
| Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 132 | Command:     `${config.MergeZipsCmd} ${out} ${in}`, | 
|  | 133 | CommandDeps: []string{"${config.MergeZipsCmd}"}, | 
|  | 134 | }) | 
|  | 135 |  | 
|  | 136 | var 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 |  | 
|  | 142 | var 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 Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 148 |  | 
|  | 149 | // Builds an app into a module suitable for input to bundletool | 
|  | 150 | func BuildBundleModule(ctx android.ModuleContext, outputFile android.WritablePath, | 
| Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 151 | packageFile, jniJarFile, dexJarFile android.Path) { | 
| Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 152 |  | 
|  | 153 | protoResJarFile := android.PathForModuleOut(ctx, "package-res.pb.apk") | 
| Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 154 | aapt2Convert(ctx, protoResJarFile, packageFile) | 
| Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 155 |  | 
| Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 156 | 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 Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 167 | if dexJarFile != nil { | 
| Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 168 | 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 Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 181 | } | 
|  | 182 | if jniJarFile != nil { | 
| Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 183 | zips = append(zips, jniJarFile) | 
| Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 184 | } | 
|  | 185 |  | 
|  | 186 | ctx.Build(pctx, android.BuildParams{ | 
|  | 187 | Rule:        buildBundleModule, | 
| Colin Cross | fd94c40 | 2018-11-01 14:50:55 -0700 | [diff] [blame] | 188 | Inputs:      zips, | 
| Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 189 | Output:      outputFile, | 
|  | 190 | Description: "bundle", | 
| Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 191 | }) | 
|  | 192 | } | 
|  | 193 |  | 
| Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 194 | func TransformJniLibsToJar(ctx android.ModuleContext, outputFile android.WritablePath, | 
| Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 195 | jniLibs []jniLib, uncompressJNI bool) { | 
| Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 196 |  | 
|  | 197 | var deps android.Paths | 
|  | 198 | jarArgs := []string{ | 
|  | 199 | "-j", // junk paths, they will be added back with -P arguments | 
|  | 200 | } | 
|  | 201 |  | 
| Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 202 | if uncompressJNI { | 
| Peter Collingbourne | ad84f97 | 2019-12-17 16:46:18 -0800 | [diff] [blame] | 203 | jarArgs = append(jarArgs, "-L", "0") | 
| Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 204 | } | 
|  | 205 |  | 
|  | 206 | for _, j := range jniLibs { | 
|  | 207 | deps = append(deps, j.path) | 
|  | 208 | jarArgs = append(jarArgs, | 
| Peter Collingbourne | ad84f97 | 2019-12-17 16:46:18 -0800 | [diff] [blame] | 209 | "-P", targetToJniDir(j.target), | 
|  | 210 | "-f", j.path.String()) | 
| Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 211 | } | 
|  | 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 Cross | 0b9f31f | 2019-02-28 11:00:01 -0800 | [diff] [blame] | 219 | "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "), | 
| Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 220 | }, | 
|  | 221 | }) | 
|  | 222 | } | 
|  | 223 |  | 
|  | 224 | func targetToJniDir(target android.Target) string { | 
|  | 225 | return filepath.Join("lib", target.Arch.Abi[0]) | 
|  | 226 | } |