• 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	"strings"
23
24	"github.com/google/blueprint"
25
26	"android/soong/android"
27)
28
29var (
30	aaptCreateResourceJavaFile = pctx.AndroidStaticRule("aaptCreateResourceJavaFile",
31		blueprint.RuleParams{
32			Command: `rm -rf "$javaDir" && mkdir -p "$javaDir" && ` +
33				`$aaptCmd package -m $aaptFlags -P $publicResourcesFile -G $proguardOptionsFile ` +
34				`-J $javaDir || ( rm -rf "$javaDir/*"; exit 41 ) && ` +
35				`find $javaDir -name "*.java" > $javaFileList`,
36			CommandDeps: []string{"$aaptCmd"},
37		},
38		"aaptFlags", "publicResourcesFile", "proguardOptionsFile", "javaDir", "javaFileList")
39
40	aaptCreateAssetsPackage = pctx.AndroidStaticRule("aaptCreateAssetsPackage",
41		blueprint.RuleParams{
42			Command:     `rm -f $out && $aaptCmd package $aaptFlags -F $out`,
43			CommandDeps: []string{"$aaptCmd"},
44		},
45		"aaptFlags", "publicResourcesFile", "proguardOptionsFile", "javaDir", "javaFileList")
46
47	aaptAddResources = pctx.AndroidStaticRule("aaptAddResources",
48		blueprint.RuleParams{
49			// TODO: add-jni-shared-libs-to-package
50			Command:     `cp -f $in $out.tmp && $aaptCmd package -u $aaptFlags -F $out.tmp && mv $out.tmp $out`,
51			CommandDeps: []string{"$aaptCmd"},
52		},
53		"aaptFlags")
54
55	signapk = pctx.AndroidStaticRule("signapk",
56		blueprint.RuleParams{
57			Command:     `java -jar $signapkCmd $certificates $in $out`,
58			CommandDeps: []string{"$signapkCmd"},
59		},
60		"certificates")
61
62	androidManifestMerger = pctx.AndroidStaticRule("androidManifestMerger",
63		blueprint.RuleParams{
64			Command: "java -classpath $androidManifestMergerCmd com.android.manifmerger.Main merge " +
65				"--main $in --libs $libsManifests --out $out",
66			CommandDeps: []string{"$androidManifestMergerCmd"},
67			Description: "merge manifest files",
68		},
69		"libsManifests")
70)
71
72func init() {
73	pctx.SourcePathVariable("androidManifestMergerCmd", "prebuilts/devtools/tools/lib/manifest-merger.jar")
74	pctx.HostBinToolVariable("aaptCmd", "aapt")
75	pctx.HostJavaToolVariable("signapkCmd", "signapk.jar")
76}
77
78func CreateResourceJavaFiles(ctx android.ModuleContext, flags []string,
79	deps android.Paths) (android.Path, android.Path, android.Path) {
80	javaDir := android.PathForModuleGen(ctx, "R")
81	javaFileList := android.PathForModuleOut(ctx, "R.filelist")
82	publicResourcesFile := android.PathForModuleOut(ctx, "public_resources.xml")
83	proguardOptionsFile := android.PathForModuleOut(ctx, "proguard.options")
84
85	ctx.ModuleBuild(pctx, android.ModuleBuildParams{
86		Rule:        aaptCreateResourceJavaFile,
87		Description: "aapt create R.java",
88		Outputs:     android.WritablePaths{publicResourcesFile, proguardOptionsFile, javaFileList},
89		Implicits:   deps,
90		Args: map[string]string{
91			"aaptFlags":           strings.Join(flags, " "),
92			"publicResourcesFile": publicResourcesFile.String(),
93			"proguardOptionsFile": proguardOptionsFile.String(),
94			"javaDir":             javaDir.String(),
95			"javaFileList":        javaFileList.String(),
96		},
97	})
98
99	return publicResourcesFile, proguardOptionsFile, javaFileList
100}
101
102func CreateExportPackage(ctx android.ModuleContext, flags []string, deps android.Paths) android.ModuleOutPath {
103	outputFile := android.PathForModuleOut(ctx, "package-export.apk")
104
105	ctx.ModuleBuild(pctx, android.ModuleBuildParams{
106		Rule:        aaptCreateAssetsPackage,
107		Description: "aapt export package",
108		Output:      outputFile,
109		Implicits:   deps,
110		Args: map[string]string{
111			"aaptFlags": strings.Join(flags, " "),
112		},
113	})
114
115	return outputFile
116}
117
118func CreateAppPackage(ctx android.ModuleContext, flags []string, jarFile android.Path,
119	certificates []string) android.Path {
120
121	resourceApk := android.PathForModuleOut(ctx, "resources.apk")
122
123	ctx.ModuleBuild(pctx, android.ModuleBuildParams{
124		Rule:        aaptAddResources,
125		Description: "aapt package",
126		Output:      resourceApk,
127		Input:       jarFile,
128		Args: map[string]string{
129			"aaptFlags": strings.Join(flags, " "),
130		},
131	})
132
133	outputFile := android.PathForModuleOut(ctx, "package.apk")
134
135	var certificateArgs []string
136	for _, c := range certificates {
137		certificateArgs = append(certificateArgs, c+".x509.pem", c+".pk8")
138	}
139
140	ctx.ModuleBuild(pctx, android.ModuleBuildParams{
141		Rule:        signapk,
142		Description: "signapk",
143		Output:      outputFile,
144		Input:       resourceApk,
145		Args: map[string]string{
146			"certificates": strings.Join(certificateArgs, " "),
147		},
148	})
149
150	return outputFile
151}
152