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 = pctx.RemoteStaticRules("signapk", 34 blueprint.RuleParams{ 35 Command: `rm -f $out && $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, v4SignatureFile android.WritablePath, 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, v4SignatureFile, lineageFile) 77} 78 79func SignAppPackage(ctx android.ModuleContext, signedApk android.WritablePath, unsignedApk android.Path, certificates []Certificate, v4SignatureFile android.WritablePath, 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 v4SignatureFile != nil { 91 outputFiles = append(outputFiles, v4SignatureFile) 92 flags = append(flags, "--enable-v4") 93 } 94 95 if lineageFile != nil { 96 flags = append(flags, "--lineage", lineageFile.String()) 97 deps = append(deps, lineageFile) 98 } 99 100 rule := Signapk 101 args := map[string]string{ 102 "certificates": strings.Join(certificateArgs, " "), 103 "flags": strings.Join(flags, " "), 104 } 105 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_SIGNAPK") { 106 rule = SignapkRE 107 args["implicits"] = strings.Join(deps.Strings(), ",") 108 args["outCommaList"] = strings.Join(outputFiles.Strings(), ",") 109 } 110 ctx.Build(pctx, android.BuildParams{ 111 Rule: rule, 112 Description: "signapk", 113 Outputs: outputFiles, 114 Input: unsignedApk, 115 Implicits: deps, 116 Args: args, 117 }) 118} 119 120var buildAAR = pctx.AndroidStaticRule("buildAAR", 121 blueprint.RuleParams{ 122 Command: `rm -rf ${outDir} && mkdir -p ${outDir} && ` + 123 `cp ${manifest} ${outDir}/AndroidManifest.xml && ` + 124 `cp ${classesJar} ${outDir}/classes.jar && ` + 125 `cp ${rTxt} ${outDir}/R.txt && ` + 126 `${config.SoongZipCmd} -jar -o $out -C ${outDir} -D ${outDir}`, 127 CommandDeps: []string{"${config.SoongZipCmd}"}, 128 }, 129 "manifest", "classesJar", "rTxt", "outDir") 130 131func BuildAAR(ctx android.ModuleContext, outputFile android.WritablePath, 132 classesJar, manifest, rTxt android.Path, res android.Paths) { 133 134 // TODO(ccross): uniquify and copy resources with dependencies 135 136 deps := android.Paths{manifest, rTxt} 137 classesJarPath := "" 138 if classesJar != nil { 139 deps = append(deps, classesJar) 140 classesJarPath = classesJar.String() 141 } 142 143 ctx.Build(pctx, android.BuildParams{ 144 Rule: buildAAR, 145 Description: "aar", 146 Implicits: deps, 147 Output: outputFile, 148 Args: map[string]string{ 149 "manifest": manifest.String(), 150 "classesJar": classesJarPath, 151 "rTxt": rTxt.String(), 152 "outDir": android.PathForModuleOut(ctx, "aar").String(), 153 }, 154 }) 155} 156 157var buildBundleModule = pctx.AndroidStaticRule("buildBundleModule", 158 blueprint.RuleParams{ 159 Command: `${config.MergeZipsCmd} ${out} ${in}`, 160 CommandDeps: []string{"${config.MergeZipsCmd}"}, 161 }) 162 163var bundleMungePackage = pctx.AndroidStaticRule("bundleMungePackage", 164 blueprint.RuleParams{ 165 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} AndroidManifest.xml:manifest/AndroidManifest.xml resources.pb "res/**/*" "assets/**/*"`, 166 CommandDeps: []string{"${config.Zip2ZipCmd}"}, 167 }) 168 169var bundleMungeDexJar = pctx.AndroidStaticRule("bundleMungeDexJar", 170 blueprint.RuleParams{ 171 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "classes*.dex:dex/" && ` + 172 `${config.Zip2ZipCmd} -i ${in} -o ${resJar} -x "classes*.dex" "**/*:root/"`, 173 CommandDeps: []string{"${config.Zip2ZipCmd}"}, 174 }, "resJar") 175 176// Builds an app into a module suitable for input to bundletool 177func BuildBundleModule(ctx android.ModuleContext, outputFile android.WritablePath, 178 packageFile, jniJarFile, dexJarFile android.Path) { 179 180 protoResJarFile := android.PathForModuleOut(ctx, "package-res.pb.apk") 181 aapt2Convert(ctx, protoResJarFile, packageFile) 182 183 var zips android.Paths 184 185 mungedPackage := android.PathForModuleOut(ctx, "bundle", "apk.zip") 186 ctx.Build(pctx, android.BuildParams{ 187 Rule: bundleMungePackage, 188 Input: protoResJarFile, 189 Output: mungedPackage, 190 Description: "bundle apk", 191 }) 192 zips = append(zips, mungedPackage) 193 194 if dexJarFile != nil { 195 mungedDexJar := android.PathForModuleOut(ctx, "bundle", "dex.zip") 196 mungedResJar := android.PathForModuleOut(ctx, "bundle", "res.zip") 197 ctx.Build(pctx, android.BuildParams{ 198 Rule: bundleMungeDexJar, 199 Input: dexJarFile, 200 Output: mungedDexJar, 201 ImplicitOutput: mungedResJar, 202 Description: "bundle dex", 203 Args: map[string]string{ 204 "resJar": mungedResJar.String(), 205 }, 206 }) 207 zips = append(zips, mungedDexJar, mungedResJar) 208 } 209 if jniJarFile != nil { 210 zips = append(zips, jniJarFile) 211 } 212 213 ctx.Build(pctx, android.BuildParams{ 214 Rule: buildBundleModule, 215 Inputs: zips, 216 Output: outputFile, 217 Description: "bundle", 218 }) 219} 220 221func TransformJniLibsToJar(ctx android.ModuleContext, outputFile android.WritablePath, 222 jniLibs []jniLib, uncompressJNI bool) { 223 224 var deps android.Paths 225 jarArgs := []string{ 226 "-j", // junk paths, they will be added back with -P arguments 227 } 228 229 if uncompressJNI { 230 jarArgs = append(jarArgs, "-L", "0") 231 } 232 233 for _, j := range jniLibs { 234 deps = append(deps, j.path) 235 jarArgs = append(jarArgs, 236 "-P", targetToJniDir(j.target), 237 "-f", j.path.String()) 238 } 239 240 rule := zip 241 args := map[string]string{ 242 "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "), 243 } 244 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ZIP") { 245 rule = zipRE 246 args["implicits"] = strings.Join(deps.Strings(), ",") 247 } 248 ctx.Build(pctx, android.BuildParams{ 249 Rule: rule, 250 Description: "zip jni libs", 251 Output: outputFile, 252 Implicits: deps, 253 Args: args, 254 }) 255} 256 257func targetToJniDir(target android.Target) string { 258 return filepath.Join("lib", target.Arch.Abi[0]) 259} 260