2015-04-13 22:58:27 +02:00
|
|
|
// Copyright 2015 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package java
|
|
|
|
|
|
|
|
// This file generates the final rules for compiling all Java. All properties related to
|
|
|
|
// compiling should have been translated into javaBuilderFlags or another argument to the Transform*
|
|
|
|
// functions.
|
|
|
|
|
|
|
|
import (
|
2018-10-03 07:03:40 +02:00
|
|
|
"path/filepath"
|
2015-04-13 22:58:27 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
2018-10-03 07:03:40 +02:00
|
|
|
"github.com/google/blueprint/proptools"
|
2015-04-13 22:58:27 +02:00
|
|
|
|
2016-05-19 00:37:25 +02:00
|
|
|
"android/soong/android"
|
2020-05-28 15:13:33 +02:00
|
|
|
"android/soong/remoteexec"
|
2015-04-13 22:58:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-03-12 20:28:25 +01:00
|
|
|
Signapk, SignapkRE = pctx.RemoteStaticRules("signapk",
|
2015-04-13 22:58:27 +02:00
|
|
|
blueprint.RuleParams{
|
2020-11-25 23:07:23 +01:00
|
|
|
Command: `rm -f $out && $reTemplate${config.JavaCmd} ${config.JavaVmFlags} -Djava.library.path=$$(dirname ${config.SignapkJniLibrary}) ` +
|
2019-08-08 21:56:34 +02:00
|
|
|
`-jar ${config.SignapkCmd} $flags $certificates $in $out`,
|
|
|
|
CommandDeps: []string{"${config.SignapkCmd}", "${config.SignapkJniLibrary}"},
|
2015-04-13 22:58:27 +02:00
|
|
|
},
|
2020-05-28 15:13:33 +02:00
|
|
|
&remoteexec.REParams{Labels: map[string]string{"type": "tool", "name": "signapk"},
|
|
|
|
ExecStrategy: "${config.RESignApkExecStrategy}",
|
|
|
|
Inputs: []string{"${config.SignapkCmd}", "$in", "$$(dirname ${config.SignapkJniLibrary})", "$implicits"},
|
|
|
|
OutputFiles: []string{"$outCommaList"},
|
|
|
|
ToolchainInputs: []string{"${config.JavaCmd}"},
|
|
|
|
Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
|
|
|
|
}, []string{"flags", "certificates"}, []string{"implicits", "outCommaList"})
|
2015-04-13 22:58:27 +02:00
|
|
|
)
|
|
|
|
|
2017-11-23 01:19:37 +01:00
|
|
|
var combineApk = pctx.AndroidStaticRule("combineApk",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: `${config.MergeZipsCmd} $out $in`,
|
|
|
|
CommandDeps: []string{"${config.MergeZipsCmd}"},
|
2015-04-13 22:58:27 +02:00
|
|
|
})
|
|
|
|
|
2019-04-15 18:48:31 +02:00
|
|
|
func CreateAndSignAppPackage(ctx android.ModuleContext, outputFile android.WritablePath,
|
2021-11-03 15:39:39 +01:00
|
|
|
packageFile, jniJarFile, dexJarFile android.Path, certificates []Certificate, deps android.Paths, v4SignatureFile android.WritablePath, lineageFile android.Path, rotationMinSdkVersion string) {
|
2015-04-13 22:58:27 +02:00
|
|
|
|
2019-03-20 00:03:11 +01:00
|
|
|
unsignedApkName := strings.TrimSuffix(outputFile.Base(), ".apk") + "-unsigned.apk"
|
|
|
|
unsignedApk := android.PathForModuleOut(ctx, unsignedApkName)
|
2015-04-13 22:58:27 +02:00
|
|
|
|
2018-10-03 07:03:40 +02:00
|
|
|
var inputs android.Paths
|
2017-11-23 01:19:37 +01:00
|
|
|
if dexJarFile != nil {
|
|
|
|
inputs = append(inputs, dexJarFile)
|
|
|
|
}
|
2018-11-01 22:50:55 +01:00
|
|
|
inputs = append(inputs, packageFile)
|
2018-10-03 07:03:40 +02:00
|
|
|
if jniJarFile != nil {
|
|
|
|
inputs = append(inputs, jniJarFile)
|
|
|
|
}
|
2015-04-13 22:58:27 +02:00
|
|
|
|
2017-10-24 02:16:14 +02:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
2019-05-16 21:28:22 +02:00
|
|
|
Rule: combineApk,
|
|
|
|
Inputs: inputs,
|
|
|
|
Output: unsignedApk,
|
|
|
|
Implicits: deps,
|
2015-04-13 22:58:27 +02:00
|
|
|
})
|
|
|
|
|
2021-11-03 15:39:39 +01:00
|
|
|
SignAppPackage(ctx, outputFile, unsignedApk, certificates, v4SignatureFile, lineageFile, rotationMinSdkVersion)
|
2019-04-15 18:48:31 +02:00
|
|
|
}
|
|
|
|
|
2021-11-03 15:39:39 +01:00
|
|
|
func SignAppPackage(ctx android.ModuleContext, signedApk android.WritablePath, unsignedApk android.Path, certificates []Certificate, v4SignatureFile android.WritablePath, lineageFile android.Path, rotationMinSdkVersion string) {
|
2019-04-15 18:48:31 +02:00
|
|
|
|
2015-04-13 22:58:27 +02:00
|
|
|
var certificateArgs []string
|
2019-04-10 06:26:14 +02:00
|
|
|
var deps android.Paths
|
2015-04-13 22:58:27 +02:00
|
|
|
for _, c := range certificates {
|
2018-10-30 13:20:05 +01:00
|
|
|
certificateArgs = append(certificateArgs, c.Pem.String(), c.Key.String())
|
2019-04-10 06:26:14 +02:00
|
|
|
deps = append(deps, c.Pem, c.Key)
|
2015-04-13 22:58:27 +02:00
|
|
|
}
|
|
|
|
|
2020-03-25 04:32:24 +01:00
|
|
|
outputFiles := android.WritablePaths{signedApk}
|
2020-05-07 22:24:05 +02:00
|
|
|
var flags []string
|
2020-03-25 04:32:24 +01:00
|
|
|
if v4SignatureFile != nil {
|
|
|
|
outputFiles = append(outputFiles, v4SignatureFile)
|
2020-05-07 22:24:05 +02:00
|
|
|
flags = append(flags, "--enable-v4")
|
|
|
|
}
|
|
|
|
|
|
|
|
if lineageFile != nil {
|
|
|
|
flags = append(flags, "--lineage", lineageFile.String())
|
2020-05-09 02:19:26 +02:00
|
|
|
deps = append(deps, lineageFile)
|
2020-03-25 04:32:24 +01:00
|
|
|
}
|
|
|
|
|
2021-11-03 15:39:39 +01:00
|
|
|
if rotationMinSdkVersion != "" {
|
|
|
|
flags = append(flags, "--rotation-min-sdk-version", rotationMinSdkVersion)
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:13:33 +02:00
|
|
|
rule := Signapk
|
|
|
|
args := map[string]string{
|
|
|
|
"certificates": strings.Join(certificateArgs, " "),
|
|
|
|
"flags": strings.Join(flags, " "),
|
|
|
|
}
|
2020-09-03 07:29:49 +02:00
|
|
|
if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_SIGNAPK") {
|
2020-05-28 15:13:33 +02:00
|
|
|
rule = SignapkRE
|
|
|
|
args["implicits"] = strings.Join(deps.Strings(), ",")
|
|
|
|
args["outCommaList"] = strings.Join(outputFiles.Strings(), ",")
|
|
|
|
}
|
2017-10-24 02:16:14 +02:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
2020-05-28 15:13:33 +02:00
|
|
|
Rule: rule,
|
2017-05-09 22:45:28 +02:00
|
|
|
Description: "signapk",
|
2020-03-25 04:32:24 +01:00
|
|
|
Outputs: outputFiles,
|
2017-11-23 01:19:37 +01:00
|
|
|
Input: unsignedApk,
|
2019-04-10 06:26:14 +02:00
|
|
|
Implicits: deps,
|
2020-05-28 15:13:33 +02:00
|
|
|
Args: args,
|
2015-04-13 22:58:27 +02:00
|
|
|
})
|
|
|
|
}
|
2018-03-28 23:58:31 +02:00
|
|
|
|
|
|
|
var buildAAR = pctx.AndroidStaticRule("buildAAR",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: `rm -rf ${outDir} && mkdir -p ${outDir} && ` +
|
|
|
|
`cp ${manifest} ${outDir}/AndroidManifest.xml && ` +
|
|
|
|
`cp ${classesJar} ${outDir}/classes.jar && ` +
|
|
|
|
`cp ${rTxt} ${outDir}/R.txt && ` +
|
2018-09-19 02:05:15 +02:00
|
|
|
`${config.SoongZipCmd} -jar -o $out -C ${outDir} -D ${outDir}`,
|
2018-03-28 23:58:31 +02:00
|
|
|
CommandDeps: []string{"${config.SoongZipCmd}"},
|
|
|
|
},
|
2018-09-19 02:05:15 +02:00
|
|
|
"manifest", "classesJar", "rTxt", "outDir")
|
2018-03-28 23:58:31 +02:00
|
|
|
|
|
|
|
func BuildAAR(ctx android.ModuleContext, outputFile android.WritablePath,
|
|
|
|
classesJar, manifest, rTxt android.Path, res android.Paths) {
|
|
|
|
|
|
|
|
// TODO(ccross): uniquify and copy resources with dependencies
|
|
|
|
|
|
|
|
deps := android.Paths{manifest, rTxt}
|
|
|
|
classesJarPath := ""
|
|
|
|
if classesJar != nil {
|
|
|
|
deps = append(deps, classesJar)
|
|
|
|
classesJarPath = classesJar.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
2019-01-25 22:20:38 +01:00
|
|
|
Rule: buildAAR,
|
|
|
|
Description: "aar",
|
|
|
|
Implicits: deps,
|
|
|
|
Output: outputFile,
|
2018-03-28 23:58:31 +02:00
|
|
|
Args: map[string]string{
|
|
|
|
"manifest": manifest.String(),
|
|
|
|
"classesJar": classesJarPath,
|
|
|
|
"rTxt": rTxt.String(),
|
|
|
|
"outDir": android.PathForModuleOut(ctx, "aar").String(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2018-10-03 07:03:40 +02:00
|
|
|
|
2018-10-30 07:14:58 +01:00
|
|
|
var buildBundleModule = pctx.AndroidStaticRule("buildBundleModule",
|
|
|
|
blueprint.RuleParams{
|
2018-11-01 22:50:55 +01:00
|
|
|
Command: `${config.MergeZipsCmd} ${out} ${in}`,
|
|
|
|
CommandDeps: []string{"${config.MergeZipsCmd}"},
|
|
|
|
})
|
|
|
|
|
|
|
|
var bundleMungePackage = pctx.AndroidStaticRule("bundleMungePackage",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} AndroidManifest.xml:manifest/AndroidManifest.xml resources.pb "res/**/*" "assets/**/*"`,
|
|
|
|
CommandDeps: []string{"${config.Zip2ZipCmd}"},
|
|
|
|
})
|
|
|
|
|
|
|
|
var bundleMungeDexJar = pctx.AndroidStaticRule("bundleMungeDexJar",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "classes*.dex:dex/" && ` +
|
|
|
|
`${config.Zip2ZipCmd} -i ${in} -o ${resJar} -x "classes*.dex" "**/*:root/"`,
|
|
|
|
CommandDeps: []string{"${config.Zip2ZipCmd}"},
|
|
|
|
}, "resJar")
|
2018-10-30 07:14:58 +01:00
|
|
|
|
|
|
|
// Builds an app into a module suitable for input to bundletool
|
|
|
|
func BuildBundleModule(ctx android.ModuleContext, outputFile android.WritablePath,
|
2018-11-01 22:50:55 +01:00
|
|
|
packageFile, jniJarFile, dexJarFile android.Path) {
|
2018-10-30 07:14:58 +01:00
|
|
|
|
|
|
|
protoResJarFile := android.PathForModuleOut(ctx, "package-res.pb.apk")
|
2018-11-01 22:50:55 +01:00
|
|
|
aapt2Convert(ctx, protoResJarFile, packageFile)
|
|
|
|
|
|
|
|
var zips android.Paths
|
|
|
|
|
|
|
|
mungedPackage := android.PathForModuleOut(ctx, "bundle", "apk.zip")
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: bundleMungePackage,
|
|
|
|
Input: protoResJarFile,
|
|
|
|
Output: mungedPackage,
|
|
|
|
Description: "bundle apk",
|
|
|
|
})
|
|
|
|
zips = append(zips, mungedPackage)
|
2018-10-30 07:14:58 +01:00
|
|
|
|
|
|
|
if dexJarFile != nil {
|
2018-11-01 22:50:55 +01:00
|
|
|
mungedDexJar := android.PathForModuleOut(ctx, "bundle", "dex.zip")
|
|
|
|
mungedResJar := android.PathForModuleOut(ctx, "bundle", "res.zip")
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: bundleMungeDexJar,
|
|
|
|
Input: dexJarFile,
|
|
|
|
Output: mungedDexJar,
|
|
|
|
ImplicitOutput: mungedResJar,
|
|
|
|
Description: "bundle dex",
|
|
|
|
Args: map[string]string{
|
|
|
|
"resJar": mungedResJar.String(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
zips = append(zips, mungedDexJar, mungedResJar)
|
2018-10-30 07:14:58 +01:00
|
|
|
}
|
|
|
|
if jniJarFile != nil {
|
2018-11-01 22:50:55 +01:00
|
|
|
zips = append(zips, jniJarFile)
|
2018-10-30 07:14:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: buildBundleModule,
|
2018-11-01 22:50:55 +01:00
|
|
|
Inputs: zips,
|
2018-10-30 07:14:58 +01:00
|
|
|
Output: outputFile,
|
|
|
|
Description: "bundle",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-03 07:03:40 +02:00
|
|
|
func TransformJniLibsToJar(ctx android.ModuleContext, outputFile android.WritablePath,
|
2019-02-06 06:55:21 +01:00
|
|
|
jniLibs []jniLib, uncompressJNI bool) {
|
2018-10-03 07:03:40 +02:00
|
|
|
|
|
|
|
var deps android.Paths
|
|
|
|
jarArgs := []string{
|
|
|
|
"-j", // junk paths, they will be added back with -P arguments
|
|
|
|
}
|
|
|
|
|
2019-02-06 06:55:21 +01:00
|
|
|
if uncompressJNI {
|
2019-12-18 01:46:18 +01:00
|
|
|
jarArgs = append(jarArgs, "-L", "0")
|
2018-10-03 07:03:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, j := range jniLibs {
|
|
|
|
deps = append(deps, j.path)
|
|
|
|
jarArgs = append(jarArgs,
|
2019-12-18 01:46:18 +01:00
|
|
|
"-P", targetToJniDir(j.target),
|
|
|
|
"-f", j.path.String())
|
2018-10-03 07:03:40 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 20:27:16 +02:00
|
|
|
rule := zip
|
|
|
|
args := map[string]string{
|
|
|
|
"jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
|
|
|
|
}
|
2020-09-03 07:29:49 +02:00
|
|
|
if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ZIP") {
|
2020-05-20 20:27:16 +02:00
|
|
|
rule = zipRE
|
|
|
|
args["implicits"] = strings.Join(deps.Strings(), ",")
|
|
|
|
}
|
2018-10-03 07:03:40 +02:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
2020-05-20 20:27:16 +02:00
|
|
|
Rule: rule,
|
2018-10-03 07:03:40 +02:00
|
|
|
Description: "zip jni libs",
|
|
|
|
Output: outputFile,
|
|
|
|
Implicits: deps,
|
2020-05-20 20:27:16 +02:00
|
|
|
Args: args,
|
2018-10-03 07:03:40 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-01 19:48:31 +02:00
|
|
|
func (a *AndroidApp) generateJavaUsedByApex(ctx android.ModuleContext) {
|
|
|
|
javaApiUsedByOutputFile := android.PathForModuleOut(ctx, a.installApkName+"_using.xml")
|
|
|
|
javaUsedByRule := android.NewRuleBuilder(pctx, ctx)
|
|
|
|
javaUsedByRule.Command().
|
|
|
|
Tool(android.PathForSource(ctx, "build/soong/scripts/gen_java_usedby_apex.sh")).
|
|
|
|
BuiltTool("dexdeps").
|
|
|
|
Output(javaApiUsedByOutputFile).
|
|
|
|
Input(a.Library.Module.outputFile)
|
|
|
|
javaUsedByRule.Build("java_usedby_list", "Generate Java APIs used by Apex")
|
|
|
|
a.javaApiUsedByOutputFile = javaApiUsedByOutputFile
|
|
|
|
}
|
|
|
|
|
2018-10-03 07:03:40 +02:00
|
|
|
func targetToJniDir(target android.Target) string {
|
|
|
|
return filepath.Join("lib", target.Arch.Abi[0])
|
|
|
|
}
|