blob: 69c6fe1dcc6af2153222867ddbf952183198ba1f [file] [log] [blame]
Colin Cross30e076a2015-04-13 13:58:27 -07001// 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
27 "android/soong/common"
28)
29
30var (
31 aaptCreateResourceJavaFile = pctx.StaticRule("aaptCreateResourceJavaFile",
32 blueprint.RuleParams{
33 Command: `rm -rf "$javaDir" && mkdir -p "$javaDir" && ` +
34 `$aaptCmd package -m $aaptFlags -P $publicResourcesFile -G $proguardOptionsFile ` +
35 `-J $javaDir || ( rm -rf "$javaDir/*"; exit 41 ) && ` +
36 `find $javaDir -name "*.java" > $javaFileList`,
37 Description: "aapt create R.java $out",
38 },
39 "aaptFlags", "publicResourcesFile", "proguardOptionsFile", "javaDir", "javaFileList")
40
41 aaptCreateAssetsPackage = pctx.StaticRule("aaptCreateAssetsPackage",
42 blueprint.RuleParams{
43 Command: `$aaptCmd package $aaptFlags -F $out`,
44 Description: "aapt export package $out",
45 },
46 "aaptFlags", "publicResourcesFile", "proguardOptionsFile", "javaDir", "javaFileList")
47
48 aaptAddResources = pctx.StaticRule("aaptAddResources",
49 blueprint.RuleParams{
50 // TODO: add-jni-shared-libs-to-package
51 Command: `cp -f $in $out.tmp && $aaptCmd package -u $aaptFlags -F $out.tmp && mv $out.tmp $out`,
52 Description: "aapt package $out",
53 },
54 "aaptFlags")
55
56 zipalign = pctx.StaticRule("zipalign",
57 blueprint.RuleParams{
58 Command: `$zipalignCmd -f $zipalignFlags 4 $in $out`,
59 Description: "zipalign $out",
60 },
61 "zipalignFlags")
62
63 signapk = pctx.StaticRule("signapk",
64 blueprint.RuleParams{
65 Command: `java -jar $signapkCmd $certificates $in $out`,
66 Description: "signapk $out",
67 },
68 "certificates")
69
70 androidManifestMerger = pctx.StaticRule("androidManifestMerger",
71 blueprint.RuleParams{
72 Command: "java -classpath $androidManifestMergerCmd com.android.manifmerger.Main merge " +
73 "--main $in --libs $libsManifests --out $out",
74 Description: "merge manifest files $out",
75 },
76 "libsManifests")
77)
78
79func init() {
80 pctx.StaticVariable("androidManifestMergerCmd", "${srcDir}/prebuilts/devtools/tools/lib/manifest-merger.jar")
81 pctx.VariableFunc("aaptCmd", func(c interface{}) (string, error) {
82 return c.(common.Config).HostBinTool("aapt")
83 })
84 pctx.VariableFunc("zipalignCmd", func(c interface{}) (string, error) {
85 return c.(common.Config).HostBinTool("zipalign")
86 })
87 pctx.VariableFunc("signapkCmd", func(c interface{}) (string, error) {
88 return c.(common.Config).HostJavaTool("signapk.jar")
89 })
90}
91
92func CreateResourceJavaFiles(ctx common.AndroidModuleContext, flags []string,
93 deps []string) (string, string, string) {
94 javaDir := filepath.Join(common.ModuleGenDir(ctx), "R")
95 javaFileList := filepath.Join(common.ModuleOutDir(ctx), "R.filelist")
96 publicResourcesFile := filepath.Join(common.ModuleOutDir(ctx), "public_resources.xml")
97 proguardOptionsFile := filepath.Join(common.ModuleOutDir(ctx), "proguard.options")
98
99 ctx.Build(pctx, blueprint.BuildParams{
100 Rule: aaptCreateResourceJavaFile,
101 Outputs: []string{publicResourcesFile, proguardOptionsFile, javaFileList},
102 Implicits: deps,
103 Args: map[string]string{
104 "aaptFlags": strings.Join(flags, " "),
105 "publicResourcesFile": publicResourcesFile,
106 "proguardOptionsFile": proguardOptionsFile,
107 "javaDir": javaDir,
108 "javaFileList": javaFileList,
109 },
110 })
111
112 return publicResourcesFile, proguardOptionsFile, javaFileList
113}
114
115func CreateExportPackage(ctx common.AndroidModuleContext, flags []string, deps []string) string {
116 outputFile := filepath.Join(common.ModuleOutDir(ctx), "package-export.apk")
117
118 ctx.Build(pctx, blueprint.BuildParams{
119 Rule: aaptCreateAssetsPackage,
120 Outputs: []string{outputFile},
121 Implicits: deps,
122 Args: map[string]string{
123 "aaptFlags": strings.Join(flags, " "),
124 },
125 })
126
127 return outputFile
128}
129
130func CreateAppPackage(ctx common.AndroidModuleContext, flags []string, jarFile string,
131 certificates []string) string {
132
133 resourceApk := filepath.Join(common.ModuleOutDir(ctx), "resources.apk")
134
135 ctx.Build(pctx, blueprint.BuildParams{
136 Rule: aaptAddResources,
137 Outputs: []string{resourceApk},
138 Inputs: []string{jarFile},
139 Args: map[string]string{
140 "aaptFlags": strings.Join(flags, " "),
141 },
142 })
143
144 signedApk := filepath.Join(common.ModuleOutDir(ctx), "signed.apk")
145
146 var certificateArgs []string
147 for _, c := range certificates {
148 certificateArgs = append(certificateArgs, c+".x509.pem", c+".pk8")
149 }
150
151 ctx.Build(pctx, blueprint.BuildParams{
152 Rule: signapk,
153 Outputs: []string{signedApk},
154 Inputs: []string{resourceApk},
155 Args: map[string]string{
156 "certificates": strings.Join(certificateArgs, " "),
157 },
158 })
159
160 outputFile := filepath.Join(common.ModuleOutDir(ctx), "package.apk")
161
162 ctx.Build(pctx, blueprint.BuildParams{
163 Rule: zipalign,
164 Outputs: []string{outputFile},
165 Inputs: []string{signedApk},
166 Args: map[string]string{
167 "zipalignFlags": "",
168 },
169 })
170
171 return outputFile
172}