blob: 2b787032e922fa006328c3e6ee4edb1180d5f1bd [file] [log] [blame]
Colin Crossf0056cb2017-12-22 15:56:08 -08001// Copyright 2017 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
17import (
Jiyong Park54105c42021-03-31 18:17:53 +090018 "strconv"
Colin Crossf0056cb2017-12-22 15:56:08 -080019 "strings"
20
21 "github.com/google/blueprint"
David Srbeckye033cba2020-05-20 22:20:28 +010022 "github.com/google/blueprint/proptools"
Colin Crossf0056cb2017-12-22 15:56:08 -080023
24 "android/soong/android"
Ramy Medhat1dcc27e2020-04-21 21:36:23 -040025 "android/soong/remoteexec"
Colin Crossf0056cb2017-12-22 15:56:08 -080026)
27
Liz Kammera7a64f32020-07-09 15:16:41 -070028type DexProperties struct {
29 // If set to true, compile dex regardless of installable. Defaults to false.
30 Compile_dex *bool
31
32 // list of module-specific flags that will be used for dex compiles
33 Dxflags []string `android:"arch_variant"`
34
Colin Cross5ea963e2021-09-16 19:13:43 -070035 // A list of files containing rules that specify the classes to keep in the main dex file.
36 Main_dex_rules []string `android:"path"`
37
Liz Kammera7a64f32020-07-09 15:16:41 -070038 Optimize struct {
Jared Duke63a3da92022-06-02 19:11:14 +000039 // If false, disable all optimization. Defaults to true for android_app and
40 // android_test_helper_app modules, false for android_test, java_library, and java_test modules.
Liz Kammera7a64f32020-07-09 15:16:41 -070041 Enabled *bool
42 // True if the module containing this has it set by default.
43 EnabledByDefault bool `blueprint:"mutated"`
44
Remi NGUYEN VANbdad3142022-08-04 13:19:03 +090045 // Whether to continue building even if warnings are emitted. Defaults to true.
46 Ignore_warnings *bool
47
Christoffer Quist Adamsenf2d7b162020-08-24 15:56:16 +020048 // If true, runs R8 in Proguard compatibility mode (default).
49 // Otherwise, runs R8 in full mode.
50 Proguard_compatibility *bool
51
Liz Kammera7a64f32020-07-09 15:16:41 -070052 // If true, optimize for size by removing unused code. Defaults to true for apps,
53 // false for libraries and tests.
54 Shrink *bool
55
56 // If true, optimize bytecode. Defaults to false.
57 Optimize *bool
58
59 // If true, obfuscate bytecode. Defaults to false.
60 Obfuscate *bool
61
62 // If true, do not use the flag files generated by aapt that automatically keep
63 // classes referenced by the app manifest. Defaults to false.
64 No_aapt_flags *bool
65
66 // Flags to pass to proguard.
67 Proguard_flags []string
68
69 // Specifies the locations of files containing proguard flags.
70 Proguard_flags_files []string `android:"path"`
71 }
72
73 // Keep the data uncompressed. We always need uncompressed dex for execution,
74 // so this might actually save space by avoiding storing the same data twice.
75 // This defaults to reasonable value based on module and should not be set.
76 // It exists only to support ART tests.
77 Uncompress_dex *bool
Wei Li1e73c652021-12-06 13:35:11 -080078
Wei Li92cd54b2022-01-12 13:22:55 -080079 // Exclude kotlinc generate files: *.kotlin_module, *.kotlin_builtins. Defaults to false.
Wei Li1e73c652021-12-06 13:35:11 -080080 Exclude_kotlinc_generated_files *bool
Liz Kammera7a64f32020-07-09 15:16:41 -070081}
82
83type dexer struct {
84 dexProperties DexProperties
85
86 // list of extra proguard flag files
87 extraProguardFlagFiles android.Paths
88 proguardDictionary android.OptionalPath
Colin Crosscb6143a2020-08-14 17:39:29 -070089 proguardUsageZip android.OptionalPath
Liz Kammera7a64f32020-07-09 15:16:41 -070090}
91
92func (d *dexer) effectiveOptimizeEnabled() bool {
93 return BoolDefault(d.dexProperties.Optimize.Enabled, d.dexProperties.Optimize.EnabledByDefault)
94}
95
Colin Cross77cdcfd2021-03-12 11:28:25 -080096var d8, d8RE = pctx.MultiCommandRemoteStaticRules("d8",
Colin Crossf0056cb2017-12-22 15:56:08 -080097 blueprint.RuleParams{
98 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Colin Crossa79a52c2021-08-04 10:52:44 -070099 `mkdir -p $$(dirname $tmpJar) && ` +
100 `${config.Zip2ZipCmd} -i $in -o $tmpJar -x '**/*.dex' && ` +
Jared Duke0cf7c962022-04-20 20:28:33 +0000101 `$d8Template${config.D8Cmd} ${config.D8Flags} --output $outDir $d8Flags $tmpJar && ` +
Kousik Kumar366afc52020-05-20 11:27:16 -0700102 `$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` +
Wei Li1e73c652021-12-06 13:35:11 -0800103 `${config.MergeZipsCmd} -D -stripFile "**/*.class" $mergeZipsFlags $out $outDir/classes.dex.jar $in`,
Colin Crossf0056cb2017-12-22 15:56:08 -0800104 CommandDeps: []string{
105 "${config.D8Cmd}",
Colin Crossa79a52c2021-08-04 10:52:44 -0700106 "${config.Zip2ZipCmd}",
Colin Crossf0056cb2017-12-22 15:56:08 -0800107 "${config.SoongZipCmd}",
108 "${config.MergeZipsCmd}",
109 },
Kousik Kumar366afc52020-05-20 11:27:16 -0700110 }, map[string]*remoteexec.REParams{
111 "$d8Template": &remoteexec.REParams{
112 Labels: map[string]string{"type": "compile", "compiler": "d8"},
113 Inputs: []string{"${config.D8Jar}"},
114 ExecStrategy: "${config.RED8ExecStrategy}",
115 ToolchainInputs: []string{"${config.JavaCmd}"},
116 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
117 },
118 "$zipTemplate": &remoteexec.REParams{
119 Labels: map[string]string{"type": "tool", "name": "soong_zip"},
120 Inputs: []string{"${config.SoongZipCmd}", "$outDir"},
121 OutputFiles: []string{"$outDir/classes.dex.jar"},
122 ExecStrategy: "${config.RED8ExecStrategy}",
123 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
124 },
Wei Li1e73c652021-12-06 13:35:11 -0800125 }, []string{"outDir", "d8Flags", "zipFlags", "tmpJar", "mergeZipsFlags"}, nil)
Colin Crossf0056cb2017-12-22 15:56:08 -0800126
Colin Cross77cdcfd2021-03-12 11:28:25 -0800127var r8, r8RE = pctx.MultiCommandRemoteStaticRules("r8",
Colin Cross66dbc0b2017-12-28 12:23:20 -0800128 blueprint.RuleParams{
129 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Colin Crosscb6143a2020-08-14 17:39:29 -0700130 `rm -f "$outDict" && rm -rf "${outUsageDir}" && ` +
131 `mkdir -p $$(dirname ${outUsage}) && ` +
Colin Crossa79a52c2021-08-04 10:52:44 -0700132 `mkdir -p $$(dirname $tmpJar) && ` +
133 `${config.Zip2ZipCmd} -i $in -o $tmpJar -x '**/*.dex' && ` +
Jared Duke0cf7c962022-04-20 20:28:33 +0000134 `$r8Template${config.R8Cmd} ${config.R8Flags} -injars $tmpJar --output $outDir ` +
Søren Gjesse24f17022018-09-14 15:20:42 +0200135 `--no-data-resources ` +
Colin Crosscb6143a2020-08-14 17:39:29 -0700136 `-printmapping ${outDict} ` +
137 `-printusage ${outUsage} ` +
Colin Cross22e6a6f2022-03-21 12:19:44 -0700138 `--deps-file ${out}.d ` +
Colin Crossffb657e2018-09-21 12:29:22 -0700139 `$r8Flags && ` +
Colin Crosscb6143a2020-08-14 17:39:29 -0700140 `touch "${outDict}" "${outUsage}" && ` +
141 `${config.SoongZipCmd} -o ${outUsageZip} -C ${outUsageDir} -f ${outUsage} && ` +
142 `rm -rf ${outUsageDir} && ` +
Kousik Kumar366afc52020-05-20 11:27:16 -0700143 `$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` +
Wei Li1e73c652021-12-06 13:35:11 -0800144 `${config.MergeZipsCmd} -D -stripFile "**/*.class" $mergeZipsFlags $out $outDir/classes.dex.jar $in`,
Colin Cross22e6a6f2022-03-21 12:19:44 -0700145 Depfile: "${out}.d",
146 Deps: blueprint.DepsGCC,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800147 CommandDeps: []string{
Colin Crossa832a042021-08-05 16:56:18 -0700148 "${config.R8Cmd}",
Colin Crossa79a52c2021-08-04 10:52:44 -0700149 "${config.Zip2ZipCmd}",
Colin Cross66dbc0b2017-12-28 12:23:20 -0800150 "${config.SoongZipCmd}",
151 "${config.MergeZipsCmd}",
152 },
Kousik Kumar366afc52020-05-20 11:27:16 -0700153 }, map[string]*remoteexec.REParams{
154 "$r8Template": &remoteexec.REParams{
155 Labels: map[string]string{"type": "compile", "compiler": "r8"},
156 Inputs: []string{"$implicits", "${config.R8Jar}"},
Colin Crosse00c0e72020-09-17 11:54:30 -0700157 OutputFiles: []string{"${outUsage}"},
Kousik Kumar366afc52020-05-20 11:27:16 -0700158 ExecStrategy: "${config.RER8ExecStrategy}",
159 ToolchainInputs: []string{"${config.JavaCmd}"},
160 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
161 },
162 "$zipTemplate": &remoteexec.REParams{
163 Labels: map[string]string{"type": "tool", "name": "soong_zip"},
164 Inputs: []string{"${config.SoongZipCmd}", "$outDir"},
165 OutputFiles: []string{"$outDir/classes.dex.jar"},
166 ExecStrategy: "${config.RER8ExecStrategy}",
167 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
168 },
Colin Crosscb6143a2020-08-14 17:39:29 -0700169 "$zipUsageTemplate": &remoteexec.REParams{
170 Labels: map[string]string{"type": "tool", "name": "soong_zip"},
171 Inputs: []string{"${config.SoongZipCmd}", "${outUsage}"},
172 OutputFiles: []string{"${outUsageZip}"},
173 ExecStrategy: "${config.RER8ExecStrategy}",
174 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
175 },
176 }, []string{"outDir", "outDict", "outUsage", "outUsageZip", "outUsageDir",
Wei Li1e73c652021-12-06 13:35:11 -0800177 "r8Flags", "zipFlags", "tmpJar", "mergeZipsFlags"}, []string{"implicits"})
Colin Cross66dbc0b2017-12-28 12:23:20 -0800178
Colin Cross5ea963e2021-09-16 19:13:43 -0700179func (d *dexer) dexCommonFlags(ctx android.ModuleContext,
180 minSdkVersion android.SdkSpec) (flags []string, deps android.Paths) {
181
182 flags = d.dexProperties.Dxflags
Colin Crossbafb8972018-06-06 21:46:32 +0000183 // Translate all the DX flags to D8 ones until all the build files have been migrated
184 // to D8 flags. See: b/69377755
185 flags = android.RemoveListFromList(flags,
186 []string{"--core-library", "--dex", "--multi-dex"})
Colin Crossf0056cb2017-12-22 15:56:08 -0800187
Colin Cross5ea963e2021-09-16 19:13:43 -0700188 for _, f := range android.PathsForModuleSrc(ctx, d.dexProperties.Main_dex_rules) {
189 flags = append(flags, "--main-dex-rules", f.String())
190 deps = append(deps, f)
191 }
192
Colin Crossf0056cb2017-12-22 15:56:08 -0800193 if ctx.Config().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Crossbafb8972018-06-06 21:46:32 +0000194 flags = append(flags, "--debug")
Colin Crossf0056cb2017-12-22 15:56:08 -0800195 }
196
197 if ctx.Config().Getenv("GENERATE_DEX_DEBUG") != "" {
198 flags = append(flags,
199 "--debug",
200 "--verbose")
Colin Crossf0056cb2017-12-22 15:56:08 -0800201 }
202
Jared Duke40d731a2022-09-20 15:32:14 -0700203 // Supplying the platform build flag disables various features like API modeling and desugaring.
204 // For targets with a stable min SDK version (i.e., when the min SDK is both explicitly specified
205 // and managed+versioned), we suppress this flag to ensure portability.
206 // Note: Targets with a min SDK kind of core_platform (e.g., framework.jar) or unspecified (e.g.,
207 // services.jar), are not classified as stable, which is WAI.
208 // TODO(b/232073181): Expand to additional min SDK cases after validation.
209 if !minSdkVersion.Stable() {
210 flags = append(flags, "--android-platform-build")
211 }
212
Jiyong Parkf1691d22021-03-29 20:11:58 +0900213 effectiveVersion, err := minSdkVersion.EffectiveVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -0700214 if err != nil {
215 ctx.PropertyErrorf("min_sdk_version", "%s", err)
216 }
217
Jiyong Park54105c42021-03-31 18:17:53 +0900218 flags = append(flags, "--min-api "+strconv.Itoa(effectiveVersion.FinalOrFutureInt()))
Colin Cross5ea963e2021-09-16 19:13:43 -0700219 return flags, deps
Colin Crossf0056cb2017-12-22 15:56:08 -0800220}
221
Liz Kammera7a64f32020-07-09 15:16:41 -0700222func d8Flags(flags javaBuilderFlags) (d8Flags []string, d8Deps android.Paths) {
Colin Crossc2557d12019-10-31 15:22:57 -0700223 d8Flags = append(d8Flags, flags.bootClasspath.FormRepeatedClassPath("--lib ")...)
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700224 d8Flags = append(d8Flags, flags.dexClasspath.FormRepeatedClassPath("--lib ")...)
Colin Crossffb657e2018-09-21 12:29:22 -0700225
Colin Cross6dab9bd2018-09-28 08:06:24 -0700226 d8Deps = append(d8Deps, flags.bootClasspath...)
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700227 d8Deps = append(d8Deps, flags.dexClasspath...)
Colin Cross6dab9bd2018-09-28 08:06:24 -0700228
229 return d8Flags, d8Deps
Colin Crossffb657e2018-09-21 12:29:22 -0700230}
231
Liz Kammera7a64f32020-07-09 15:16:41 -0700232func (d *dexer) r8Flags(ctx android.ModuleContext, flags javaBuilderFlags) (r8Flags []string, r8Deps android.Paths) {
233 opt := d.dexProperties.Optimize
Colin Cross66dbc0b2017-12-28 12:23:20 -0800234
235 // When an app contains references to APIs that are not in the SDK specified by
236 // its LOCAL_SDK_VERSION for example added by support library or by runtime
Colin Crossbafb8972018-06-06 21:46:32 +0000237 // classes added by desugaring, we artifically raise the "SDK version" "linked" by
Colin Cross66dbc0b2017-12-28 12:23:20 -0800238 // ProGuard, to
239 // - suppress ProGuard warnings of referencing symbols unknown to the lower SDK version.
240 // - prevent ProGuard stripping subclass in the support library that extends class added in the higher SDK version.
241 // See b/20667396
242 var proguardRaiseDeps classpath
Colin Crossdcf71b22021-02-01 13:59:03 -0800243 ctx.VisitDirectDepsWithTag(proguardRaiseTag, func(m android.Module) {
244 dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo)
245 proguardRaiseDeps = append(proguardRaiseDeps, dep.HeaderJars...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800246 })
247
248 r8Flags = append(r8Flags, proguardRaiseDeps.FormJavaClassPath("-libraryjars"))
249 r8Flags = append(r8Flags, flags.bootClasspath.FormJavaClassPath("-libraryjars"))
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700250 r8Flags = append(r8Flags, flags.dexClasspath.FormJavaClassPath("-libraryjars"))
Colin Cross66dbc0b2017-12-28 12:23:20 -0800251
Colin Cross6dab9bd2018-09-28 08:06:24 -0700252 r8Deps = append(r8Deps, proguardRaiseDeps...)
253 r8Deps = append(r8Deps, flags.bootClasspath...)
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700254 r8Deps = append(r8Deps, flags.dexClasspath...)
Colin Cross6dab9bd2018-09-28 08:06:24 -0700255
Colin Cross66dbc0b2017-12-28 12:23:20 -0800256 flagFiles := android.Paths{
257 android.PathForSource(ctx, "build/make/core/proguard.flags"),
258 }
259
Liz Kammera7a64f32020-07-09 15:16:41 -0700260 flagFiles = append(flagFiles, d.extraProguardFlagFiles...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800261 // TODO(ccross): static android library proguard files
262
Liz Kammera7a64f32020-07-09 15:16:41 -0700263 flagFiles = append(flagFiles, android.PathsForModuleSrc(ctx, opt.Proguard_flags_files)...)
Colin Crossbd1cef52018-08-13 10:28:18 -0700264
Colin Cross66dbc0b2017-12-28 12:23:20 -0800265 r8Flags = append(r8Flags, android.JoinWithPrefix(flagFiles.Strings(), "-include "))
266 r8Deps = append(r8Deps, flagFiles...)
267
268 // TODO(b/70942988): This is included from build/make/core/proguard.flags
269 r8Deps = append(r8Deps, android.PathForSource(ctx,
270 "build/make/core/proguard_basic_keeps.flags"))
271
Liz Kammera7a64f32020-07-09 15:16:41 -0700272 r8Flags = append(r8Flags, opt.Proguard_flags...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800273
Christoffer Quist Adamsenf2d7b162020-08-24 15:56:16 +0200274 if BoolDefault(opt.Proguard_compatibility, true) {
275 r8Flags = append(r8Flags, "--force-proguard-compatibility")
Ian Zernyfc7df612021-11-02 15:37:06 +0100276 } else {
277 // TODO(b/213833843): Allow configuration of the prefix via a build variable.
278 var sourceFilePrefix = "go/retraceme "
279 var sourceFileTemplate = "\"" + sourceFilePrefix + "%MAP_ID\""
280 // TODO(b/200967150): Also tag the source file in compat builds.
281 if Bool(opt.Optimize) || Bool(opt.Obfuscate) {
282 r8Flags = append(r8Flags, "--map-id-template", "%MAP_HASH")
283 r8Flags = append(r8Flags, "--source-file-template", sourceFileTemplate)
284 }
Christoffer Quist Adamsenf2d7b162020-08-24 15:56:16 +0200285 }
286
Colin Cross66dbc0b2017-12-28 12:23:20 -0800287 // TODO(ccross): Don't shrink app instrumentation tests by default.
288 if !Bool(opt.Shrink) {
289 r8Flags = append(r8Flags, "-dontshrink")
290 }
291
292 if !Bool(opt.Optimize) {
293 r8Flags = append(r8Flags, "-dontoptimize")
294 }
295
296 // TODO(ccross): error if obufscation + app instrumentation test.
297 if !Bool(opt.Obfuscate) {
298 r8Flags = append(r8Flags, "-dontobfuscate")
299 }
Colin Cross4b964c02018-10-15 16:18:06 -0700300 // TODO(ccross): if this is an instrumentation test of an obfuscated app, use the
301 // dictionary of the app and move the app from libraryjars to injars.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800302
Jaewoong Jung1d6eb682018-11-29 15:08:44 -0800303 // Don't strip out debug information for eng builds.
304 if ctx.Config().Eng() {
305 r8Flags = append(r8Flags, "--debug")
306 }
307
Christoffer Quist Adamsene8507372021-02-22 09:44:09 +0100308 // TODO(b/180878971): missing classes should be added to the relevant builds.
Remi NGUYEN VANbdad3142022-08-04 13:19:03 +0900309 // TODO(b/229727645): do not use true as default for Android platform builds.
310 if proptools.BoolDefault(opt.Ignore_warnings, true) {
311 r8Flags = append(r8Flags, "-ignorewarnings")
312 }
Christoffer Quist Adamsene8507372021-02-22 09:44:09 +0100313
Colin Cross66dbc0b2017-12-28 12:23:20 -0800314 return r8Flags, r8Deps
315}
316
Jiyong Parkf1691d22021-03-29 20:11:58 +0900317func (d *dexer) compileDex(ctx android.ModuleContext, flags javaBuilderFlags, minSdkVersion android.SdkSpec,
Paul Duffin612e6102021-02-02 13:38:13 +0000318 classesJar android.Path, jarName string) android.OutputPath {
Colin Crossf0056cb2017-12-22 15:56:08 -0800319
Colin Crossf0056cb2017-12-22 15:56:08 -0800320 // Compile classes.jar into classes.dex and then javalib.jar
Paul Duffin612e6102021-02-02 13:38:13 +0000321 javalibJar := android.PathForModuleOut(ctx, "dex", jarName).OutputPath
Colin Crossf0056cb2017-12-22 15:56:08 -0800322 outDir := android.PathForModuleOut(ctx, "dex")
Colin Crossa79a52c2021-08-04 10:52:44 -0700323 tmpJar := android.PathForModuleOut(ctx, "withres-withoutdex", jarName)
Colin Crossf0056cb2017-12-22 15:56:08 -0800324
Sasha Smundakd3cf4ee2019-02-15 10:14:23 -0800325 zipFlags := "--ignore_missing_files"
Liz Kammera7a64f32020-07-09 15:16:41 -0700326 if proptools.Bool(d.dexProperties.Uncompress_dex) {
Sasha Smundakd3cf4ee2019-02-15 10:14:23 -0800327 zipFlags += " -L 0"
Colin Cross5a0dcd52018-10-05 14:20:06 -0700328 }
329
Colin Cross5ea963e2021-09-16 19:13:43 -0700330 commonFlags, commonDeps := d.dexCommonFlags(ctx, minSdkVersion)
Liz Kammera7a64f32020-07-09 15:16:41 -0700331
Wei Li1e73c652021-12-06 13:35:11 -0800332 // Exclude kotlinc generated files when "exclude_kotlinc_generated_files" is set to true.
333 mergeZipsFlags := ""
334 if proptools.BoolDefault(d.dexProperties.Exclude_kotlinc_generated_files, false) {
335 mergeZipsFlags = "-stripFile META-INF/*.kotlin_module -stripFile **/*.kotlin_builtins"
336 }
337
Liz Kammera7a64f32020-07-09 15:16:41 -0700338 useR8 := d.effectiveOptimizeEnabled()
Colin Cross66dbc0b2017-12-28 12:23:20 -0800339 if useR8 {
Colin Crossc0c664c2018-05-16 16:47:21 -0700340 proguardDictionary := android.PathForModuleOut(ctx, "proguard_dictionary")
Liz Kammera7a64f32020-07-09 15:16:41 -0700341 d.proguardDictionary = android.OptionalPathForPath(proguardDictionary)
Colin Crosscb6143a2020-08-14 17:39:29 -0700342 proguardUsageDir := android.PathForModuleOut(ctx, "proguard_usage")
343 proguardUsage := proguardUsageDir.Join(ctx, ctx.Namespace().Path,
344 android.ModuleNameWithPossibleOverride(ctx), "unused.txt")
345 proguardUsageZip := android.PathForModuleOut(ctx, "proguard_usage.zip")
346 d.proguardUsageZip = android.OptionalPathForPath(proguardUsageZip)
Liz Kammera7a64f32020-07-09 15:16:41 -0700347 r8Flags, r8Deps := d.r8Flags(ctx, flags)
Colin Cross5ea963e2021-09-16 19:13:43 -0700348 r8Deps = append(r8Deps, commonDeps...)
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400349 rule := r8
350 args := map[string]string{
Wei Li1e73c652021-12-06 13:35:11 -0800351 "r8Flags": strings.Join(append(commonFlags, r8Flags...), " "),
352 "zipFlags": zipFlags,
353 "outDict": proguardDictionary.String(),
354 "outUsageDir": proguardUsageDir.String(),
355 "outUsage": proguardUsage.String(),
356 "outUsageZip": proguardUsageZip.String(),
357 "outDir": outDir.String(),
358 "tmpJar": tmpJar.String(),
359 "mergeZipsFlags": mergeZipsFlags,
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400360 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400361 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_R8") {
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400362 rule = r8RE
363 args["implicits"] = strings.Join(r8Deps.Strings(), ",")
364 }
Colin Cross66dbc0b2017-12-28 12:23:20 -0800365 ctx.Build(pctx, android.BuildParams{
Colin Crosscb6143a2020-08-14 17:39:29 -0700366 Rule: rule,
367 Description: "r8",
368 Output: javalibJar,
369 ImplicitOutputs: android.WritablePaths{proguardDictionary, proguardUsageZip},
370 Input: classesJar,
371 Implicits: r8Deps,
372 Args: args,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800373 })
374 } else {
Liz Kammera7a64f32020-07-09 15:16:41 -0700375 d8Flags, d8Deps := d8Flags(flags)
Colin Cross5ea963e2021-09-16 19:13:43 -0700376 d8Deps = append(d8Deps, commonDeps...)
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400377 rule := d8
Ramy Medhat16f23a42020-09-03 01:29:49 -0400378 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_D8") {
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400379 rule = d8RE
380 }
Colin Cross66dbc0b2017-12-28 12:23:20 -0800381 ctx.Build(pctx, android.BuildParams{
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400382 Rule: rule,
Colin Crossbafb8972018-06-06 21:46:32 +0000383 Description: "d8",
Colin Cross66dbc0b2017-12-28 12:23:20 -0800384 Output: javalibJar,
385 Input: classesJar,
Colin Cross6dab9bd2018-09-28 08:06:24 -0700386 Implicits: d8Deps,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800387 Args: map[string]string{
Wei Li1e73c652021-12-06 13:35:11 -0800388 "d8Flags": strings.Join(append(commonFlags, d8Flags...), " "),
389 "zipFlags": zipFlags,
390 "outDir": outDir.String(),
391 "tmpJar": tmpJar.String(),
392 "mergeZipsFlags": mergeZipsFlags,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800393 },
394 })
Colin Crossf0056cb2017-12-22 15:56:08 -0800395 }
Liz Kammera7a64f32020-07-09 15:16:41 -0700396 if proptools.Bool(d.dexProperties.Uncompress_dex) {
Paul Duffin612e6102021-02-02 13:38:13 +0000397 alignedJavalibJar := android.PathForModuleOut(ctx, "aligned", jarName).OutputPath
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000398 TransformZipAlign(ctx, alignedJavalibJar, javalibJar)
399 javalibJar = alignedJavalibJar
400 }
Colin Crossf0056cb2017-12-22 15:56:08 -0800401
Colin Crossf0056cb2017-12-22 15:56:08 -0800402 return javalibJar
403}