• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)
30
31var (
32	Signapk = pctx.AndroidStaticRule("signapk",
33		blueprint.RuleParams{
34			Command: `${config.JavaCmd} -Djava.library.path=$$(dirname $signapkJniLibrary) ` +
35				`-jar $signapkCmd $flags $certificates $in $out`,
36			CommandDeps: []string{"$signapkCmd", "$signapkJniLibrary"},
37		},
38		"flags", "certificates")
39
40	androidManifestMerger = pctx.AndroidStaticRule("androidManifestMerger",
41		blueprint.RuleParams{
42			Command: "java -classpath $androidManifestMergerCmd com.android.manifmerger.Main merge " +
43				"--main $in --libs $libsManifests --out $out",
44			CommandDeps: []string{"$androidManifestMergerCmd"},
45			Description: "merge manifest files",
46		},
47		"libsManifests")
48)
49
50func init() {
51	pctx.SourcePathVariable("androidManifestMergerCmd", "prebuilts/devtools/tools/lib/manifest-merger.jar")
52	pctx.HostBinToolVariable("aaptCmd", "aapt")
53	pctx.HostJavaToolVariable("signapkCmd", "signapk.jar")
54	// TODO(ccross): this should come from the signapk dependencies, but we don't have any way
55	// to express host JNI dependencies yet.
56	pctx.HostJNIToolVariable("signapkJniLibrary", "libconscrypt_openjdk_jni")
57}
58
59var combineApk = pctx.AndroidStaticRule("combineApk",
60	blueprint.RuleParams{
61		Command:     `${config.MergeZipsCmd} $out $in`,
62		CommandDeps: []string{"${config.MergeZipsCmd}"},
63	})
64
65func CreateAppPackage(ctx android.ModuleContext, outputFile android.WritablePath,
66	packageFile, jniJarFile, dexJarFile android.Path, certificates []Certificate) {
67
68	unsignedApkName := strings.TrimSuffix(outputFile.Base(), ".apk") + "-unsigned.apk"
69	unsignedApk := android.PathForModuleOut(ctx, unsignedApkName)
70
71	var inputs android.Paths
72	if dexJarFile != nil {
73		inputs = append(inputs, dexJarFile)
74	}
75	inputs = append(inputs, packageFile)
76	if jniJarFile != nil {
77		inputs = append(inputs, jniJarFile)
78	}
79
80	ctx.Build(pctx, android.BuildParams{
81		Rule:   combineApk,
82		Inputs: inputs,
83		Output: unsignedApk,
84	})
85
86	var certificateArgs []string
87	var deps android.Paths
88	for _, c := range certificates {
89		certificateArgs = append(certificateArgs, c.Pem.String(), c.Key.String())
90		deps = append(deps, c.Pem, c.Key)
91	}
92
93	ctx.Build(pctx, android.BuildParams{
94		Rule:        Signapk,
95		Description: "signapk",
96		Output:      outputFile,
97		Input:       unsignedApk,
98		Implicits:   deps,
99		Args: map[string]string{
100			"certificates": strings.Join(certificateArgs, " "),
101		},
102	})
103}
104
105var buildAAR = pctx.AndroidStaticRule("buildAAR",
106	blueprint.RuleParams{
107		Command: `rm -rf ${outDir} && mkdir -p ${outDir} && ` +
108			`cp ${manifest} ${outDir}/AndroidManifest.xml && ` +
109			`cp ${classesJar} ${outDir}/classes.jar && ` +
110			`cp ${rTxt} ${outDir}/R.txt && ` +
111			`${config.SoongZipCmd} -jar -o $out -C ${outDir} -D ${outDir}`,
112		CommandDeps: []string{"${config.SoongZipCmd}"},
113	},
114	"manifest", "classesJar", "rTxt", "outDir")
115
116func BuildAAR(ctx android.ModuleContext, outputFile android.WritablePath,
117	classesJar, manifest, rTxt android.Path, res android.Paths) {
118
119	// TODO(ccross): uniquify and copy resources with dependencies
120
121	deps := android.Paths{manifest, rTxt}
122	classesJarPath := ""
123	if classesJar != nil {
124		deps = append(deps, classesJar)
125		classesJarPath = classesJar.String()
126	}
127
128	ctx.Build(pctx, android.BuildParams{
129		Rule:        buildAAR,
130		Description: "aar",
131		Implicits:   deps,
132		Output:      outputFile,
133		Args: map[string]string{
134			"manifest":   manifest.String(),
135			"classesJar": classesJarPath,
136			"rTxt":       rTxt.String(),
137			"outDir":     android.PathForModuleOut(ctx, "aar").String(),
138		},
139	})
140}
141
142var buildBundleModule = pctx.AndroidStaticRule("buildBundleModule",
143	blueprint.RuleParams{
144		Command:     `${config.MergeZipsCmd} ${out} ${in}`,
145		CommandDeps: []string{"${config.MergeZipsCmd}"},
146	})
147
148var bundleMungePackage = pctx.AndroidStaticRule("bundleMungePackage",
149	blueprint.RuleParams{
150		Command:     `${config.Zip2ZipCmd} -i ${in} -o ${out} AndroidManifest.xml:manifest/AndroidManifest.xml resources.pb "res/**/*" "assets/**/*"`,
151		CommandDeps: []string{"${config.Zip2ZipCmd}"},
152	})
153
154var bundleMungeDexJar = pctx.AndroidStaticRule("bundleMungeDexJar",
155	blueprint.RuleParams{
156		Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "classes*.dex:dex/" && ` +
157			`${config.Zip2ZipCmd} -i ${in} -o ${resJar} -x "classes*.dex" "**/*:root/"`,
158		CommandDeps: []string{"${config.Zip2ZipCmd}"},
159	}, "resJar")
160
161// Builds an app into a module suitable for input to bundletool
162func BuildBundleModule(ctx android.ModuleContext, outputFile android.WritablePath,
163	packageFile, jniJarFile, dexJarFile android.Path) {
164
165	protoResJarFile := android.PathForModuleOut(ctx, "package-res.pb.apk")
166	aapt2Convert(ctx, protoResJarFile, packageFile)
167
168	var zips android.Paths
169
170	mungedPackage := android.PathForModuleOut(ctx, "bundle", "apk.zip")
171	ctx.Build(pctx, android.BuildParams{
172		Rule:        bundleMungePackage,
173		Input:       protoResJarFile,
174		Output:      mungedPackage,
175		Description: "bundle apk",
176	})
177	zips = append(zips, mungedPackage)
178
179	if dexJarFile != nil {
180		mungedDexJar := android.PathForModuleOut(ctx, "bundle", "dex.zip")
181		mungedResJar := android.PathForModuleOut(ctx, "bundle", "res.zip")
182		ctx.Build(pctx, android.BuildParams{
183			Rule:           bundleMungeDexJar,
184			Input:          dexJarFile,
185			Output:         mungedDexJar,
186			ImplicitOutput: mungedResJar,
187			Description:    "bundle dex",
188			Args: map[string]string{
189				"resJar": mungedResJar.String(),
190			},
191		})
192		zips = append(zips, mungedDexJar, mungedResJar)
193	}
194	if jniJarFile != nil {
195		zips = append(zips, jniJarFile)
196	}
197
198	ctx.Build(pctx, android.BuildParams{
199		Rule:        buildBundleModule,
200		Inputs:      zips,
201		Output:      outputFile,
202		Description: "bundle",
203	})
204}
205
206func TransformJniLibsToJar(ctx android.ModuleContext, outputFile android.WritablePath,
207	jniLibs []jniLib, uncompressJNI bool) {
208
209	var deps android.Paths
210	jarArgs := []string{
211		"-j", // junk paths, they will be added back with -P arguments
212	}
213
214	if uncompressJNI {
215		jarArgs = append(jarArgs, "-L 0")
216	}
217
218	for _, j := range jniLibs {
219		deps = append(deps, j.path)
220		jarArgs = append(jarArgs,
221			"-P "+targetToJniDir(j.target),
222			"-f "+j.path.String())
223	}
224
225	ctx.Build(pctx, android.BuildParams{
226		Rule:        zip,
227		Description: "zip jni libs",
228		Output:      outputFile,
229		Implicits:   deps,
230		Args: map[string]string{
231			"jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
232		},
233	})
234}
235
236func targetToJniDir(target android.Target) string {
237	return filepath.Join("lib", target.Arch.Abi[0])
238}
239