diff options
author | 2017-11-22 16:19:37 -0800 | |
---|---|---|
committer | 2017-11-29 05:05:07 +0000 | |
commit | 3bc7ffa59b8277b3a36eb9e16192583b3b9a93ea (patch) | |
tree | 1a768f402d17a3181a18177e17935dbd1d3c60b2 /java/aapt2.go | |
parent | 0875c52de753b858b74a9ac285626536bee9cb57 (diff) |
Replace aapt support with aapt2
Use aapt2 instead of aapt to compile Android app resources.
Also generate all files into srcjars instead of individual
sources.
Test: m checkbuild
Change-Id: I5a67991a0daf0017e8159b46fcff7d5564a91468
Diffstat (limited to 'java/aapt2.go')
-rw-r--r-- | java/aapt2.go | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/java/aapt2.go b/java/aapt2.go new file mode 100644 index 000000000..ce307fe79 --- /dev/null +++ b/java/aapt2.go @@ -0,0 +1,162 @@ +// Copyright 2017 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 + +import ( + "path/filepath" + "strconv" + "strings" + + "github.com/google/blueprint" + + "android/soong/android" +) + +const AAPT2_SHARD_SIZE = 100 + +// Convert input resource file path to output file path. +// values-[config]/<file>.xml -> values-[config]_<file>.arsc.flat; +// For other resource file, just replace the last "/" with "_" and +// add .flat extension. +func pathToAapt2Path(ctx android.ModuleContext, res android.Path) android.WritablePath { + + name := res.Base() + subDir := filepath.Dir(res.String()) + subDir, lastDir := filepath.Split(subDir) + if strings.HasPrefix(lastDir, "values") { + name = strings.TrimSuffix(name, ".xml") + ".arsc" + } + name = lastDir + "_" + name + ".flat" + return android.PathForModuleOut(ctx, "aapt2", subDir, name) +} + +func pathsToAapt2Paths(ctx android.ModuleContext, resPaths android.Paths) android.WritablePaths { + outPaths := make(android.WritablePaths, len(resPaths)) + + for i, res := range resPaths { + outPaths[i] = pathToAapt2Path(ctx, res) + } + + return outPaths +} + +var aapt2CompileRule = pctx.AndroidStaticRule("aapt2Compile", + blueprint.RuleParams{ + Command: `${config.Aapt2Cmd compile -o $outDir $cFlags --legacy $in`, + CommandDeps: []string{"${config.Aapt2Cmd}"}, + }, + "outDir", "cFlags") + +func aapt2Compile(ctx android.ModuleContext, dir android.Path, paths android.Paths) android.WritablePaths { + shards := shardPaths(paths, AAPT2_SHARD_SIZE) + + ret := make(android.WritablePaths, 0, len(paths)) + + for i, shard := range shards { + outPaths := pathsToAapt2Paths(ctx, shard) + ret = append(ret, outPaths...) + + shardDesc := "" + if i != 0 { + shardDesc = " " + strconv.Itoa(i+1) + } + + ctx.Build(pctx, android.BuildParams{ + Rule: aapt2CompileRule, + Description: "aapt2 compile " + dir.String() + shardDesc, + Inputs: shard, + Outputs: outPaths, + Args: map[string]string{ + "outDir": android.PathForModuleOut(ctx, "aapt2", dir.String()).String(), + "cFlags": "--pseudo-localize", + }, + }) + } + + return ret +} + +var aapt2LinkRule = pctx.AndroidStaticRule("aapt2Link", + blueprint.RuleParams{ + Command: `$aapt2Cmd link -o $out $flags --java $genDir --proguard $proguardOptions $inFlags && ` + + `${config.SoongZipCmd} -write_if_changed -jar -o $genJar -C $genDir -D $genDir`, + CommandDeps: []string{ + "$aapt2Cmd", + "${config.SoongZipCmd}", + }, + Restat: true, + }, + "flags", "inFlags", "proguardOptions", "genDir", "genJar") + +var fileListToFileRule = pctx.AndroidStaticRule("fileListToFile", + blueprint.RuleParams{ + Command: `cp $out.rsp $out`, + Rspfile: "$out.rsp", + RspfileContent: "$in", + }) + +func aapt2Link(ctx android.ModuleContext, + packageRes, genJar, proguardOptions android.WritablePath, + flags []string, deps android.Paths, + compiledRes, compiledOverlay android.Paths) { + + genDir := android.PathForModuleGen(ctx, "aapt2", "R") + + var inFlags []string + + if len(compiledRes) > 0 { + resFileList := android.PathForModuleOut(ctx, "aapt2", "res.list") + // Write out file lists to files + ctx.Build(pctx, android.BuildParams{ + Rule: fileListToFileRule, + Description: "resource file list", + Inputs: compiledRes, + Output: resFileList, + }) + + deps = append(deps, compiledRes...) + deps = append(deps, resFileList) + inFlags = append(inFlags, "@"+resFileList.String()) + } + + if len(compiledOverlay) > 0 { + overlayFileList := android.PathForModuleOut(ctx, "aapt2", "overlay.list") + ctx.Build(pctx, android.BuildParams{ + Rule: fileListToFileRule, + Description: "overlay resource file list", + Inputs: compiledOverlay, + Output: overlayFileList, + }) + + deps = append(deps, compiledOverlay...) + deps = append(deps, overlayFileList) + inFlags = append(inFlags, "-R", "@"+overlayFileList.String()) + } + + ctx.Build(pctx, android.BuildParams{ + Rule: aapt2LinkRule, + Description: "aapt2 link", + Implicits: deps, + Output: packageRes, + ImplicitOutputs: android.WritablePaths{proguardOptions, genJar}, + Args: map[string]string{ + "flags": strings.Join(flags, " "), + "inFlags": strings.Join(inFlags, " "), + "proguardOptions": proguardOptions.String(), + "genDir": genDir.String(), + "genJar": genJar.String(), + }, + }) +} |