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 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 ( 22 "path/filepath" 23 "strings" 24 25 "github.com/google/blueprint" 26 "github.com/google/blueprint/proptools" 27 28 "android/soong/android" 29 "android/soong/remoteexec" 30) 31 32var ( 33 Signapk, SignapkRE = remoteexec.StaticRules(pctx, "signapk", 34 blueprint.RuleParams{ 35 Command: `$reTemplate${config.JavaCmd} ${config.JavaVmFlags} -Djava.library.path=$$(dirname ${config.SignapkJniLibrary}) ` + 36 `-jar ${config.SignapkCmd} $flags $certificates $in $out`, 37 CommandDeps: []string{"${config.SignapkCmd}", "${config.SignapkJniLibrary}"}, 38 }, 39 &remoteexec.REParams{Labels: map[string]string{"type": "tool", "name": "signapk"}, 40 ExecStrategy: "${config.RESignApkExecStrategy}", 41 Inputs: []string{"${config.SignapkCmd}", "$in", "$$(dirname ${config.SignapkJniLibrary})", "$implicits"}, 42 OutputFiles: []string{"$outCommaList"}, 43 ToolchainInputs: []string{"${config.JavaCmd}"}, 44 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, 45 }, []string{"flags", "certificates"}, []string{"implicits", "outCommaList"}) 46) 47 48var combineApk = pctx.AndroidStaticRule("combineApk", 49 blueprint.RuleParams{ 50 Command: `${config.MergeZipsCmd} $out $in`, 51 CommandDeps: []string{"${config.MergeZipsCmd}"}, 52 }) 53 54func CreateAndSignAppPackage(ctx android.ModuleContext, outputFile android.WritablePath, 55 packageFile, jniJarFile, dexJarFile android.Path, certificates []Certificate, deps android.Paths, lineageFile android.Path) { 56 57 unsignedApkName := strings.TrimSuffix(outputFile.Base(), ".apk") + "-unsigned.apk" 58 unsignedApk := android.PathForModuleOut(ctx, unsignedApkName) 59 60 var inputs android.Paths 61 if dexJarFile != nil { 62 inputs = append(inputs, dexJarFile) 63 } 64 inputs = append(inputs, packageFile) 65 if jniJarFile != nil { 66 inputs = append(inputs, jniJarFile) 67 } 68 69 ctx.Build(pctx, android.BuildParams{ 70 Rule: combineApk, 71 Inputs: inputs, 72 Output: unsignedApk, 73 Implicits: deps, 74 }) 75 76 SignAppPackage(ctx, outputFile, unsignedApk, certificates, lineageFile) 77} 78 79func SignAppPackage(ctx android.ModuleContext, signedApk android.WritablePath, unsignedApk android.Path, certificates []Certificate, lineageFile android.Path) { 80 81 var certificateArgs []string 82 var deps android.Paths 83 for _, c := range certificates { 84 certificateArgs = append(certificateArgs, c.Pem.String(), c.Key.String()) 85 deps = append(deps, c.Pem, c.Key) 86 } 87 88 outputFiles := android.WritablePaths{signedApk} 89 var flags []string 90 if lineageFile != nil { 91 flags = append(flags, "--lineage", lineageFile.String()) 92 deps = append(deps, lineageFile) 93 } 94 95 rule := Signapk 96 args := map[string]string{ 97 "certificates": strings.Join(certificateArgs, " "), 98 "flags": strings.Join(flags, " "), 99 } 100 if ctx.Config().IsEnvTrue("RBE_SIGNAPK") { 101 rule = SignapkRE 102 args["implicits"] = strings.Join(deps.Strings(), ",") 103 args["outCommaList"] = strings.Join(outputFiles.Strings(), ",") 104 } 105 ctx.Build(pctx, android.BuildParams{ 106 Rule: rule, 107 Description: "signapk", 108 Outputs: outputFiles, 109 Input: unsignedApk, 110 Implicits: deps, 111 Args: args, 112 }) 113} 114 115var buildAAR = pctx.AndroidStaticRule("buildAAR", 116 blueprint.RuleParams{ 117 Command: `rm -rf ${outDir} && mkdir -p ${outDir} && ` + 118 `cp ${manifest} ${outDir}/AndroidManifest.xml && ` + 119 `cp ${classesJar} ${outDir}/classes.jar && ` + 120 `cp ${rTxt} ${outDir}/R.txt && ` + 121 `${config.SoongZipCmd} -jar -o $out -C ${outDir} -D ${outDir}`, 122 CommandDeps: []string{"${config.SoongZipCmd}"}, 123 }, 124 "manifest", "classesJar", "rTxt", "outDir") 125 126func BuildAAR(ctx android.ModuleContext, outputFile android.WritablePath, 127 classesJar, manifest, rTxt android.Path, res android.Paths) { 128 129 // TODO(ccross): uniquify and copy resources with dependencies 130 131 deps := android.Paths{manifest, rTxt} 132 classesJarPath := "" 133 if classesJar != nil { 134 deps = append(deps, classesJar) 135 classesJarPath = classesJar.String() 136 } 137 138 ctx.Build(pctx, android.BuildParams{ 139 Rule: buildAAR, 140 Description: "aar", 141 Implicits: deps, 142 Output: outputFile, 143 Args: map[string]string{ 144 "manifest": manifest.String(), 145 "classesJar": classesJarPath, 146 "rTxt": rTxt.String(), 147 "outDir": android.PathForModuleOut(ctx, "aar").String(), 148 }, 149 }) 150} 151 152var buildBundleModule = pctx.AndroidStaticRule("buildBundleModule", 153 blueprint.RuleParams{ 154 Command: `${config.MergeZipsCmd} ${out} ${in}`, 155 CommandDeps: []string{"${config.MergeZipsCmd}"}, 156 }) 157 158var bundleMungePackage = pctx.AndroidStaticRule("bundleMungePackage", 159 blueprint.RuleParams{ 160 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} AndroidManifest.xml:manifest/AndroidManifest.xml resources.pb "res/**/*" "assets/**/*"`, 161 CommandDeps: []string{"${config.Zip2ZipCmd}"}, 162 }) 163 164var bundleMungeDexJar = pctx.AndroidStaticRule("bundleMungeDexJar", 165 blueprint.RuleParams{ 166 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "classes*.dex:dex/" && ` + 167 `${config.Zip2ZipCmd} -i ${in} -o ${resJar} -x "classes*.dex" "**/*:root/"`, 168 CommandDeps: []string{"${config.Zip2ZipCmd}"}, 169 }, "resJar") 170 171// Builds an app into a module suitable for input to bundletool 172func BuildBundleModule(ctx android.ModuleContext, outputFile android.WritablePath, 173 packageFile, jniJarFile, dexJarFile android.Path) { 174 175 protoResJarFile := android.PathForModuleOut(ctx, "package-res.pb.apk") 176 aapt2Convert(ctx, protoResJarFile, packageFile) 177 178 var zips android.Paths 179 180 mungedPackage := android.PathForModuleOut(ctx, "bundle", "apk.zip") 181 ctx.Build(pctx, android.BuildParams{ 182 Rule: bundleMungePackage, 183 Input: protoResJarFile, 184 Output: mungedPackage, 185 Description: "bundle apk", 186 }) 187 zips = append(zips, mungedPackage) 188 189 if dexJarFile != nil { 190 mungedDexJar := android.PathForModuleOut(ctx, "bundle", "dex.zip") 191 mungedResJar := android.PathForModuleOut(ctx, "bundle", "res.zip") 192 ctx.Build(pctx, android.BuildParams{ 193 Rule: bundleMungeDexJar, 194 Input: dexJarFile, 195 Output: mungedDexJar, 196 ImplicitOutput: mungedResJar, 197 Description: "bundle dex", 198 Args: map[string]string{ 199 "resJar": mungedResJar.String(), 200 }, 201 }) 202 zips = append(zips, mungedDexJar, mungedResJar) 203 } 204 if jniJarFile != nil { 205 zips = append(zips, jniJarFile) 206 } 207 208 ctx.Build(pctx, android.BuildParams{ 209 Rule: buildBundleModule, 210 Inputs: zips, 211 Output: outputFile, 212 Description: "bundle", 213 }) 214} 215 216func TransformJniLibsToJar(ctx android.ModuleContext, outputFile android.WritablePath, 217 jniLibs []jniLib, uncompressJNI bool) { 218 219 var deps android.Paths 220 jarArgs := []string{ 221 "-j", // junk paths, they will be added back with -P arguments 222 } 223 224 if uncompressJNI { 225 jarArgs = append(jarArgs, "-L", "0") 226 } 227 228 for _, j := range jniLibs { 229 deps = append(deps, j.path) 230 jarArgs = append(jarArgs, 231 "-P", targetToJniDir(j.target), 232 "-f", j.path.String()) 233 } 234 235 rule := zip 236 args := map[string]string{ 237 "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "), 238 } 239 if ctx.Config().IsEnvTrue("RBE_ZIP") { 240 rule = zipRE 241 args["implicits"] = strings.Join(deps.Strings(), ",") 242 } 243 ctx.Build(pctx, android.BuildParams{ 244 Rule: rule, 245 Description: "zip jni libs", 246 Output: outputFile, 247 Implicits: deps, 248 Args: args, 249 }) 250} 251 252func targetToJniDir(target android.Target) string { 253 return filepath.Join("lib", target.Arch.Abi[0]) 254} 255