blob: ed1f07b26887bd04f50d2bf1c472b9b3d87d1ac6 [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
Jared Duke51b0a102022-09-27 16:53:11 -070066 // If true, optimize for size by removing unused resources. Defaults to false.
Rico Wind351bac92022-09-22 10:41:42 +020067 Shrink_resources *bool
68
Liz Kammera7a64f32020-07-09 15:16:41 -070069 // Flags to pass to proguard.
70 Proguard_flags []string
71
72 // Specifies the locations of files containing proguard flags.
73 Proguard_flags_files []string `android:"path"`
74 }
75
76 // Keep the data uncompressed. We always need uncompressed dex for execution,
77 // so this might actually save space by avoiding storing the same data twice.
78 // This defaults to reasonable value based on module and should not be set.
79 // It exists only to support ART tests.
80 Uncompress_dex *bool
Wei Li1e73c652021-12-06 13:35:11 -080081
Wei Li92cd54b2022-01-12 13:22:55 -080082 // Exclude kotlinc generate files: *.kotlin_module, *.kotlin_builtins. Defaults to false.
Wei Li1e73c652021-12-06 13:35:11 -080083 Exclude_kotlinc_generated_files *bool
Liz Kammera7a64f32020-07-09 15:16:41 -070084}
85
86type dexer struct {
87 dexProperties DexProperties
88
89 // list of extra proguard flag files
90 extraProguardFlagFiles android.Paths
91 proguardDictionary android.OptionalPath
Colin Crosscb6143a2020-08-14 17:39:29 -070092 proguardUsageZip android.OptionalPath
Liz Kammera7a64f32020-07-09 15:16:41 -070093}
94
95func (d *dexer) effectiveOptimizeEnabled() bool {
96 return BoolDefault(d.dexProperties.Optimize.Enabled, d.dexProperties.Optimize.EnabledByDefault)
97}
98
Colin Cross77cdcfd2021-03-12 11:28:25 -080099var d8, d8RE = pctx.MultiCommandRemoteStaticRules("d8",
Colin Crossf0056cb2017-12-22 15:56:08 -0800100 blueprint.RuleParams{
101 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Colin Crossa79a52c2021-08-04 10:52:44 -0700102 `mkdir -p $$(dirname $tmpJar) && ` +
103 `${config.Zip2ZipCmd} -i $in -o $tmpJar -x '**/*.dex' && ` +
Jared Duke0cf7c962022-04-20 20:28:33 +0000104 `$d8Template${config.D8Cmd} ${config.D8Flags} --output $outDir $d8Flags $tmpJar && ` +
Kousik Kumar366afc52020-05-20 11:27:16 -0700105 `$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` +
Wei Li1e73c652021-12-06 13:35:11 -0800106 `${config.MergeZipsCmd} -D -stripFile "**/*.class" $mergeZipsFlags $out $outDir/classes.dex.jar $in`,
Colin Crossf0056cb2017-12-22 15:56:08 -0800107 CommandDeps: []string{
108 "${config.D8Cmd}",
Colin Crossa79a52c2021-08-04 10:52:44 -0700109 "${config.Zip2ZipCmd}",
Colin Crossf0056cb2017-12-22 15:56:08 -0800110 "${config.SoongZipCmd}",
111 "${config.MergeZipsCmd}",
112 },
Kousik Kumar366afc52020-05-20 11:27:16 -0700113 }, map[string]*remoteexec.REParams{
114 "$d8Template": &remoteexec.REParams{
115 Labels: map[string]string{"type": "compile", "compiler": "d8"},
116 Inputs: []string{"${config.D8Jar}"},
117 ExecStrategy: "${config.RED8ExecStrategy}",
118 ToolchainInputs: []string{"${config.JavaCmd}"},
119 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
120 },
121 "$zipTemplate": &remoteexec.REParams{
122 Labels: map[string]string{"type": "tool", "name": "soong_zip"},
123 Inputs: []string{"${config.SoongZipCmd}", "$outDir"},
124 OutputFiles: []string{"$outDir/classes.dex.jar"},
125 ExecStrategy: "${config.RED8ExecStrategy}",
126 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
127 },
Wei Li1e73c652021-12-06 13:35:11 -0800128 }, []string{"outDir", "d8Flags", "zipFlags", "tmpJar", "mergeZipsFlags"}, nil)
Colin Crossf0056cb2017-12-22 15:56:08 -0800129
Colin Cross77cdcfd2021-03-12 11:28:25 -0800130var r8, r8RE = pctx.MultiCommandRemoteStaticRules("r8",
Colin Cross66dbc0b2017-12-28 12:23:20 -0800131 blueprint.RuleParams{
132 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Colin Crosscb6143a2020-08-14 17:39:29 -0700133 `rm -f "$outDict" && rm -rf "${outUsageDir}" && ` +
134 `mkdir -p $$(dirname ${outUsage}) && ` +
Colin Crossa79a52c2021-08-04 10:52:44 -0700135 `mkdir -p $$(dirname $tmpJar) && ` +
136 `${config.Zip2ZipCmd} -i $in -o $tmpJar -x '**/*.dex' && ` +
Jared Duke0cf7c962022-04-20 20:28:33 +0000137 `$r8Template${config.R8Cmd} ${config.R8Flags} -injars $tmpJar --output $outDir ` +
Søren Gjesse24f17022018-09-14 15:20:42 +0200138 `--no-data-resources ` +
Colin Crosscb6143a2020-08-14 17:39:29 -0700139 `-printmapping ${outDict} ` +
140 `-printusage ${outUsage} ` +
Colin Cross22e6a6f2022-03-21 12:19:44 -0700141 `--deps-file ${out}.d ` +
Colin Crossffb657e2018-09-21 12:29:22 -0700142 `$r8Flags && ` +
Colin Crosscb6143a2020-08-14 17:39:29 -0700143 `touch "${outDict}" "${outUsage}" && ` +
144 `${config.SoongZipCmd} -o ${outUsageZip} -C ${outUsageDir} -f ${outUsage} && ` +
145 `rm -rf ${outUsageDir} && ` +
Kousik Kumar366afc52020-05-20 11:27:16 -0700146 `$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` +
Wei Li1e73c652021-12-06 13:35:11 -0800147 `${config.MergeZipsCmd} -D -stripFile "**/*.class" $mergeZipsFlags $out $outDir/classes.dex.jar $in`,
Colin Cross22e6a6f2022-03-21 12:19:44 -0700148 Depfile: "${out}.d",
149 Deps: blueprint.DepsGCC,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800150 CommandDeps: []string{
Colin Crossa832a042021-08-05 16:56:18 -0700151 "${config.R8Cmd}",
Colin Crossa79a52c2021-08-04 10:52:44 -0700152 "${config.Zip2ZipCmd}",
Colin Cross66dbc0b2017-12-28 12:23:20 -0800153 "${config.SoongZipCmd}",
154 "${config.MergeZipsCmd}",
155 },
Kousik Kumar366afc52020-05-20 11:27:16 -0700156 }, map[string]*remoteexec.REParams{
157 "$r8Template": &remoteexec.REParams{
158 Labels: map[string]string{"type": "compile", "compiler": "r8"},
159 Inputs: []string{"$implicits", "${config.R8Jar}"},
Colin Crosse00c0e72020-09-17 11:54:30 -0700160 OutputFiles: []string{"${outUsage}"},
Kousik Kumar366afc52020-05-20 11:27:16 -0700161 ExecStrategy: "${config.RER8ExecStrategy}",
162 ToolchainInputs: []string{"${config.JavaCmd}"},
163 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
164 },
165 "$zipTemplate": &remoteexec.REParams{
166 Labels: map[string]string{"type": "tool", "name": "soong_zip"},
167 Inputs: []string{"${config.SoongZipCmd}", "$outDir"},
168 OutputFiles: []string{"$outDir/classes.dex.jar"},
169 ExecStrategy: "${config.RER8ExecStrategy}",
170 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
171 },
Colin Crosscb6143a2020-08-14 17:39:29 -0700172 "$zipUsageTemplate": &remoteexec.REParams{
173 Labels: map[string]string{"type": "tool", "name": "soong_zip"},
174 Inputs: []string{"${config.SoongZipCmd}", "${outUsage}"},
175 OutputFiles: []string{"${outUsageZip}"},
176 ExecStrategy: "${config.RER8ExecStrategy}",
177 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
178 },
179 }, []string{"outDir", "outDict", "outUsage", "outUsageZip", "outUsageDir",
Wei Li1e73c652021-12-06 13:35:11 -0800180 "r8Flags", "zipFlags", "tmpJar", "mergeZipsFlags"}, []string{"implicits"})
Colin Cross66dbc0b2017-12-28 12:23:20 -0800181
Colin Cross5ea963e2021-09-16 19:13:43 -0700182func (d *dexer) dexCommonFlags(ctx android.ModuleContext,
Spandan Dasc404cc72023-02-23 18:05:05 +0000183 dexParams *compileDexParams) (flags []string, deps android.Paths) {
Colin Cross5ea963e2021-09-16 19:13:43 -0700184
185 flags = d.dexProperties.Dxflags
Colin Crossbafb8972018-06-06 21:46:32 +0000186 // Translate all the DX flags to D8 ones until all the build files have been migrated
187 // to D8 flags. See: b/69377755
188 flags = android.RemoveListFromList(flags,
189 []string{"--core-library", "--dex", "--multi-dex"})
Colin Crossf0056cb2017-12-22 15:56:08 -0800190
Colin Cross5ea963e2021-09-16 19:13:43 -0700191 for _, f := range android.PathsForModuleSrc(ctx, d.dexProperties.Main_dex_rules) {
192 flags = append(flags, "--main-dex-rules", f.String())
193 deps = append(deps, f)
194 }
195
Colin Crossf0056cb2017-12-22 15:56:08 -0800196 if ctx.Config().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Crossbafb8972018-06-06 21:46:32 +0000197 flags = append(flags, "--debug")
Colin Crossf0056cb2017-12-22 15:56:08 -0800198 }
199
200 if ctx.Config().Getenv("GENERATE_DEX_DEBUG") != "" {
201 flags = append(flags,
202 "--debug",
203 "--verbose")
Colin Crossf0056cb2017-12-22 15:56:08 -0800204 }
205
Jared Duke40d731a2022-09-20 15:32:14 -0700206 // Supplying the platform build flag disables various features like API modeling and desugaring.
207 // For targets with a stable min SDK version (i.e., when the min SDK is both explicitly specified
208 // and managed+versioned), we suppress this flag to ensure portability.
209 // Note: Targets with a min SDK kind of core_platform (e.g., framework.jar) or unspecified (e.g.,
210 // services.jar), are not classified as stable, which is WAI.
211 // TODO(b/232073181): Expand to additional min SDK cases after validation.
Spandan Dasc404cc72023-02-23 18:05:05 +0000212 if !dexParams.sdkVersion.Stable() {
Jared Duke40d731a2022-09-20 15:32:14 -0700213 flags = append(flags, "--android-platform-build")
214 }
215
Spandan Dasc404cc72023-02-23 18:05:05 +0000216 effectiveVersion, err := dexParams.minSdkVersion.EffectiveVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -0700217 if err != nil {
218 ctx.PropertyErrorf("min_sdk_version", "%s", err)
219 }
220
Jiyong Park54105c42021-03-31 18:17:53 +0900221 flags = append(flags, "--min-api "+strconv.Itoa(effectiveVersion.FinalOrFutureInt()))
Colin Cross5ea963e2021-09-16 19:13:43 -0700222 return flags, deps
Colin Crossf0056cb2017-12-22 15:56:08 -0800223}
224
Liz Kammera7a64f32020-07-09 15:16:41 -0700225func d8Flags(flags javaBuilderFlags) (d8Flags []string, d8Deps android.Paths) {
Colin Crossc2557d12019-10-31 15:22:57 -0700226 d8Flags = append(d8Flags, flags.bootClasspath.FormRepeatedClassPath("--lib ")...)
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700227 d8Flags = append(d8Flags, flags.dexClasspath.FormRepeatedClassPath("--lib ")...)
Colin Crossffb657e2018-09-21 12:29:22 -0700228
Colin Cross6dab9bd2018-09-28 08:06:24 -0700229 d8Deps = append(d8Deps, flags.bootClasspath...)
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700230 d8Deps = append(d8Deps, flags.dexClasspath...)
Colin Cross6dab9bd2018-09-28 08:06:24 -0700231
232 return d8Flags, d8Deps
Colin Crossffb657e2018-09-21 12:29:22 -0700233}
234
Liz Kammera7a64f32020-07-09 15:16:41 -0700235func (d *dexer) r8Flags(ctx android.ModuleContext, flags javaBuilderFlags) (r8Flags []string, r8Deps android.Paths) {
236 opt := d.dexProperties.Optimize
Colin Cross66dbc0b2017-12-28 12:23:20 -0800237
238 // When an app contains references to APIs that are not in the SDK specified by
239 // its LOCAL_SDK_VERSION for example added by support library or by runtime
Colin Crossbafb8972018-06-06 21:46:32 +0000240 // classes added by desugaring, we artifically raise the "SDK version" "linked" by
Colin Cross66dbc0b2017-12-28 12:23:20 -0800241 // ProGuard, to
242 // - suppress ProGuard warnings of referencing symbols unknown to the lower SDK version.
243 // - prevent ProGuard stripping subclass in the support library that extends class added in the higher SDK version.
244 // See b/20667396
245 var proguardRaiseDeps classpath
Colin Crossdcf71b22021-02-01 13:59:03 -0800246 ctx.VisitDirectDepsWithTag(proguardRaiseTag, func(m android.Module) {
247 dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo)
248 proguardRaiseDeps = append(proguardRaiseDeps, dep.HeaderJars...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800249 })
250
251 r8Flags = append(r8Flags, proguardRaiseDeps.FormJavaClassPath("-libraryjars"))
252 r8Flags = append(r8Flags, flags.bootClasspath.FormJavaClassPath("-libraryjars"))
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700253 r8Flags = append(r8Flags, flags.dexClasspath.FormJavaClassPath("-libraryjars"))
Colin Cross66dbc0b2017-12-28 12:23:20 -0800254
Colin Cross6dab9bd2018-09-28 08:06:24 -0700255 r8Deps = append(r8Deps, proguardRaiseDeps...)
256 r8Deps = append(r8Deps, flags.bootClasspath...)
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700257 r8Deps = append(r8Deps, flags.dexClasspath...)
Colin Cross6dab9bd2018-09-28 08:06:24 -0700258
Colin Cross66dbc0b2017-12-28 12:23:20 -0800259 flagFiles := android.Paths{
260 android.PathForSource(ctx, "build/make/core/proguard.flags"),
261 }
262
Liz Kammera7a64f32020-07-09 15:16:41 -0700263 flagFiles = append(flagFiles, d.extraProguardFlagFiles...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800264 // TODO(ccross): static android library proguard files
265
Liz Kammera7a64f32020-07-09 15:16:41 -0700266 flagFiles = append(flagFiles, android.PathsForModuleSrc(ctx, opt.Proguard_flags_files)...)
Colin Crossbd1cef52018-08-13 10:28:18 -0700267
Colin Cross66dbc0b2017-12-28 12:23:20 -0800268 r8Flags = append(r8Flags, android.JoinWithPrefix(flagFiles.Strings(), "-include "))
269 r8Deps = append(r8Deps, flagFiles...)
270
271 // TODO(b/70942988): This is included from build/make/core/proguard.flags
272 r8Deps = append(r8Deps, android.PathForSource(ctx,
273 "build/make/core/proguard_basic_keeps.flags"))
274
Liz Kammera7a64f32020-07-09 15:16:41 -0700275 r8Flags = append(r8Flags, opt.Proguard_flags...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800276
Christoffer Quist Adamsenf2d7b162020-08-24 15:56:16 +0200277 if BoolDefault(opt.Proguard_compatibility, true) {
278 r8Flags = append(r8Flags, "--force-proguard-compatibility")
Ian Zernyfc7df612021-11-02 15:37:06 +0100279 } else {
280 // TODO(b/213833843): Allow configuration of the prefix via a build variable.
281 var sourceFilePrefix = "go/retraceme "
282 var sourceFileTemplate = "\"" + sourceFilePrefix + "%MAP_ID\""
283 // TODO(b/200967150): Also tag the source file in compat builds.
284 if Bool(opt.Optimize) || Bool(opt.Obfuscate) {
285 r8Flags = append(r8Flags, "--map-id-template", "%MAP_HASH")
286 r8Flags = append(r8Flags, "--source-file-template", sourceFileTemplate)
287 }
Christoffer Quist Adamsenf2d7b162020-08-24 15:56:16 +0200288 }
289
Colin Cross66dbc0b2017-12-28 12:23:20 -0800290 // TODO(ccross): Don't shrink app instrumentation tests by default.
291 if !Bool(opt.Shrink) {
292 r8Flags = append(r8Flags, "-dontshrink")
293 }
294
295 if !Bool(opt.Optimize) {
296 r8Flags = append(r8Flags, "-dontoptimize")
297 }
298
299 // TODO(ccross): error if obufscation + app instrumentation test.
300 if !Bool(opt.Obfuscate) {
301 r8Flags = append(r8Flags, "-dontobfuscate")
302 }
Colin Cross4b964c02018-10-15 16:18:06 -0700303 // TODO(ccross): if this is an instrumentation test of an obfuscated app, use the
304 // dictionary of the app and move the app from libraryjars to injars.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800305
Jaewoong Jung1d6eb682018-11-29 15:08:44 -0800306 // Don't strip out debug information for eng builds.
307 if ctx.Config().Eng() {
308 r8Flags = append(r8Flags, "--debug")
309 }
310
Christoffer Quist Adamsene8507372021-02-22 09:44:09 +0100311 // TODO(b/180878971): missing classes should be added to the relevant builds.
Remi NGUYEN VANbdad3142022-08-04 13:19:03 +0900312 // TODO(b/229727645): do not use true as default for Android platform builds.
313 if proptools.BoolDefault(opt.Ignore_warnings, true) {
314 r8Flags = append(r8Flags, "-ignorewarnings")
315 }
Christoffer Quist Adamsene8507372021-02-22 09:44:09 +0100316
Colin Cross66dbc0b2017-12-28 12:23:20 -0800317 return r8Flags, r8Deps
318}
319
Spandan Dasc404cc72023-02-23 18:05:05 +0000320type compileDexParams struct {
321 flags javaBuilderFlags
322 sdkVersion android.SdkSpec
323 minSdkVersion android.SdkSpec
324 classesJar android.Path
325 jarName string
326}
327
328func (d *dexer) compileDex(ctx android.ModuleContext, dexParams *compileDexParams) android.OutputPath {
Colin Crossf0056cb2017-12-22 15:56:08 -0800329
Colin Crossf0056cb2017-12-22 15:56:08 -0800330 // Compile classes.jar into classes.dex and then javalib.jar
Spandan Dasc404cc72023-02-23 18:05:05 +0000331 javalibJar := android.PathForModuleOut(ctx, "dex", dexParams.jarName).OutputPath
Colin Crossf0056cb2017-12-22 15:56:08 -0800332 outDir := android.PathForModuleOut(ctx, "dex")
Spandan Dasc404cc72023-02-23 18:05:05 +0000333 tmpJar := android.PathForModuleOut(ctx, "withres-withoutdex", dexParams.jarName)
Colin Crossf0056cb2017-12-22 15:56:08 -0800334
Sasha Smundakd3cf4ee2019-02-15 10:14:23 -0800335 zipFlags := "--ignore_missing_files"
Liz Kammera7a64f32020-07-09 15:16:41 -0700336 if proptools.Bool(d.dexProperties.Uncompress_dex) {
Sasha Smundakd3cf4ee2019-02-15 10:14:23 -0800337 zipFlags += " -L 0"
Colin Cross5a0dcd52018-10-05 14:20:06 -0700338 }
339
Spandan Dasc404cc72023-02-23 18:05:05 +0000340 commonFlags, commonDeps := d.dexCommonFlags(ctx, dexParams)
Liz Kammera7a64f32020-07-09 15:16:41 -0700341
Wei Li1e73c652021-12-06 13:35:11 -0800342 // Exclude kotlinc generated files when "exclude_kotlinc_generated_files" is set to true.
343 mergeZipsFlags := ""
344 if proptools.BoolDefault(d.dexProperties.Exclude_kotlinc_generated_files, false) {
345 mergeZipsFlags = "-stripFile META-INF/*.kotlin_module -stripFile **/*.kotlin_builtins"
346 }
347
Liz Kammera7a64f32020-07-09 15:16:41 -0700348 useR8 := d.effectiveOptimizeEnabled()
Colin Cross66dbc0b2017-12-28 12:23:20 -0800349 if useR8 {
Colin Crossc0c664c2018-05-16 16:47:21 -0700350 proguardDictionary := android.PathForModuleOut(ctx, "proguard_dictionary")
Liz Kammera7a64f32020-07-09 15:16:41 -0700351 d.proguardDictionary = android.OptionalPathForPath(proguardDictionary)
Colin Crosscb6143a2020-08-14 17:39:29 -0700352 proguardUsageDir := android.PathForModuleOut(ctx, "proguard_usage")
353 proguardUsage := proguardUsageDir.Join(ctx, ctx.Namespace().Path,
354 android.ModuleNameWithPossibleOverride(ctx), "unused.txt")
355 proguardUsageZip := android.PathForModuleOut(ctx, "proguard_usage.zip")
356 d.proguardUsageZip = android.OptionalPathForPath(proguardUsageZip)
Spandan Dasc404cc72023-02-23 18:05:05 +0000357 r8Flags, r8Deps := d.r8Flags(ctx, dexParams.flags)
Colin Cross5ea963e2021-09-16 19:13:43 -0700358 r8Deps = append(r8Deps, commonDeps...)
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400359 rule := r8
360 args := map[string]string{
Wei Li1e73c652021-12-06 13:35:11 -0800361 "r8Flags": strings.Join(append(commonFlags, r8Flags...), " "),
362 "zipFlags": zipFlags,
363 "outDict": proguardDictionary.String(),
364 "outUsageDir": proguardUsageDir.String(),
365 "outUsage": proguardUsage.String(),
366 "outUsageZip": proguardUsageZip.String(),
367 "outDir": outDir.String(),
368 "tmpJar": tmpJar.String(),
369 "mergeZipsFlags": mergeZipsFlags,
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400370 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400371 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_R8") {
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400372 rule = r8RE
373 args["implicits"] = strings.Join(r8Deps.Strings(), ",")
374 }
Colin Cross66dbc0b2017-12-28 12:23:20 -0800375 ctx.Build(pctx, android.BuildParams{
Colin Crosscb6143a2020-08-14 17:39:29 -0700376 Rule: rule,
377 Description: "r8",
378 Output: javalibJar,
379 ImplicitOutputs: android.WritablePaths{proguardDictionary, proguardUsageZip},
Spandan Dasc404cc72023-02-23 18:05:05 +0000380 Input: dexParams.classesJar,
Colin Crosscb6143a2020-08-14 17:39:29 -0700381 Implicits: r8Deps,
382 Args: args,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800383 })
384 } else {
Spandan Dasc404cc72023-02-23 18:05:05 +0000385 d8Flags, d8Deps := d8Flags(dexParams.flags)
Colin Cross5ea963e2021-09-16 19:13:43 -0700386 d8Deps = append(d8Deps, commonDeps...)
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400387 rule := d8
Ramy Medhat16f23a42020-09-03 01:29:49 -0400388 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_D8") {
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400389 rule = d8RE
390 }
Colin Cross66dbc0b2017-12-28 12:23:20 -0800391 ctx.Build(pctx, android.BuildParams{
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400392 Rule: rule,
Colin Crossbafb8972018-06-06 21:46:32 +0000393 Description: "d8",
Colin Cross66dbc0b2017-12-28 12:23:20 -0800394 Output: javalibJar,
Spandan Dasc404cc72023-02-23 18:05:05 +0000395 Input: dexParams.classesJar,
Colin Cross6dab9bd2018-09-28 08:06:24 -0700396 Implicits: d8Deps,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800397 Args: map[string]string{
Wei Li1e73c652021-12-06 13:35:11 -0800398 "d8Flags": strings.Join(append(commonFlags, d8Flags...), " "),
399 "zipFlags": zipFlags,
400 "outDir": outDir.String(),
401 "tmpJar": tmpJar.String(),
402 "mergeZipsFlags": mergeZipsFlags,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800403 },
404 })
Colin Crossf0056cb2017-12-22 15:56:08 -0800405 }
Liz Kammera7a64f32020-07-09 15:16:41 -0700406 if proptools.Bool(d.dexProperties.Uncompress_dex) {
Spandan Dasc404cc72023-02-23 18:05:05 +0000407 alignedJavalibJar := android.PathForModuleOut(ctx, "aligned", dexParams.jarName).OutputPath
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000408 TransformZipAlign(ctx, alignedJavalibJar, javalibJar)
409 javalibJar = alignedJavalibJar
410 }
Colin Crossf0056cb2017-12-22 15:56:08 -0800411
Colin Crossf0056cb2017-12-22 15:56:08 -0800412 return javalibJar
413}