blob: 971da925ec0645e05276035b13717c3a3a2885e9 [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"
Sam Delmerico9f9c0a22022-11-29 11:19:37 -050025 "android/soong/java/config"
Ramy Medhat1dcc27e2020-04-21 21:36:23 -040026 "android/soong/remoteexec"
Colin Crossf0056cb2017-12-22 15:56:08 -080027)
28
Liz Kammera7a64f32020-07-09 15:16:41 -070029type DexProperties struct {
30 // If set to true, compile dex regardless of installable. Defaults to false.
31 Compile_dex *bool
32
33 // list of module-specific flags that will be used for dex compiles
34 Dxflags []string `android:"arch_variant"`
35
Colin Cross5ea963e2021-09-16 19:13:43 -070036 // A list of files containing rules that specify the classes to keep in the main dex file.
37 Main_dex_rules []string `android:"path"`
38
Liz Kammera7a64f32020-07-09 15:16:41 -070039 Optimize struct {
Jared Duke63a3da92022-06-02 19:11:14 +000040 // If false, disable all optimization. Defaults to true for android_app and
41 // android_test_helper_app modules, false for android_test, java_library, and java_test modules.
Liz Kammera7a64f32020-07-09 15:16:41 -070042 Enabled *bool
43 // True if the module containing this has it set by default.
44 EnabledByDefault bool `blueprint:"mutated"`
45
Remi NGUYEN VANbdad3142022-08-04 13:19:03 +090046 // Whether to continue building even if warnings are emitted. Defaults to true.
47 Ignore_warnings *bool
48
Christoffer Quist Adamsenf2d7b162020-08-24 15:56:16 +020049 // If true, runs R8 in Proguard compatibility mode (default).
50 // Otherwise, runs R8 in full mode.
51 Proguard_compatibility *bool
52
Liz Kammera7a64f32020-07-09 15:16:41 -070053 // If true, optimize for size by removing unused code. Defaults to true for apps,
54 // false for libraries and tests.
55 Shrink *bool
56
57 // If true, optimize bytecode. Defaults to false.
58 Optimize *bool
59
60 // If true, obfuscate bytecode. Defaults to false.
61 Obfuscate *bool
62
63 // If true, do not use the flag files generated by aapt that automatically keep
64 // classes referenced by the app manifest. Defaults to false.
65 No_aapt_flags *bool
66
Jared Duke51b0a102022-09-27 16:53:11 -070067 // If true, optimize for size by removing unused resources. Defaults to false.
Rico Wind351bac92022-09-22 10:41:42 +020068 Shrink_resources *bool
69
Liz Kammera7a64f32020-07-09 15:16:41 -070070 // Flags to pass to proguard.
71 Proguard_flags []string
72
73 // Specifies the locations of files containing proguard flags.
74 Proguard_flags_files []string `android:"path"`
75 }
76
77 // Keep the data uncompressed. We always need uncompressed dex for execution,
78 // so this might actually save space by avoiding storing the same data twice.
79 // This defaults to reasonable value based on module and should not be set.
80 // It exists only to support ART tests.
81 Uncompress_dex *bool
Wei Li1e73c652021-12-06 13:35:11 -080082
Wei Li92cd54b2022-01-12 13:22:55 -080083 // Exclude kotlinc generate files: *.kotlin_module, *.kotlin_builtins. Defaults to false.
Wei Li1e73c652021-12-06 13:35:11 -080084 Exclude_kotlinc_generated_files *bool
Liz Kammera7a64f32020-07-09 15:16:41 -070085}
86
87type dexer struct {
88 dexProperties DexProperties
89
90 // list of extra proguard flag files
91 extraProguardFlagFiles android.Paths
92 proguardDictionary android.OptionalPath
Colin Crosscb6143a2020-08-14 17:39:29 -070093 proguardUsageZip android.OptionalPath
Sam Delmerico9f9c0a22022-11-29 11:19:37 -050094
95 providesTransitiveHeaderJars
Liz Kammera7a64f32020-07-09 15:16:41 -070096}
97
98func (d *dexer) effectiveOptimizeEnabled() bool {
99 return BoolDefault(d.dexProperties.Optimize.Enabled, d.dexProperties.Optimize.EnabledByDefault)
100}
101
Colin Cross77cdcfd2021-03-12 11:28:25 -0800102var d8, d8RE = pctx.MultiCommandRemoteStaticRules("d8",
Colin Crossf0056cb2017-12-22 15:56:08 -0800103 blueprint.RuleParams{
104 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Colin Crossa79a52c2021-08-04 10:52:44 -0700105 `mkdir -p $$(dirname $tmpJar) && ` +
106 `${config.Zip2ZipCmd} -i $in -o $tmpJar -x '**/*.dex' && ` +
Jared Duke0cf7c962022-04-20 20:28:33 +0000107 `$d8Template${config.D8Cmd} ${config.D8Flags} --output $outDir $d8Flags $tmpJar && ` +
Kousik Kumar366afc52020-05-20 11:27:16 -0700108 `$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` +
Wei Li1e73c652021-12-06 13:35:11 -0800109 `${config.MergeZipsCmd} -D -stripFile "**/*.class" $mergeZipsFlags $out $outDir/classes.dex.jar $in`,
Colin Crossf0056cb2017-12-22 15:56:08 -0800110 CommandDeps: []string{
111 "${config.D8Cmd}",
Colin Crossa79a52c2021-08-04 10:52:44 -0700112 "${config.Zip2ZipCmd}",
Colin Crossf0056cb2017-12-22 15:56:08 -0800113 "${config.SoongZipCmd}",
114 "${config.MergeZipsCmd}",
115 },
Kousik Kumar366afc52020-05-20 11:27:16 -0700116 }, map[string]*remoteexec.REParams{
117 "$d8Template": &remoteexec.REParams{
118 Labels: map[string]string{"type": "compile", "compiler": "d8"},
119 Inputs: []string{"${config.D8Jar}"},
120 ExecStrategy: "${config.RED8ExecStrategy}",
121 ToolchainInputs: []string{"${config.JavaCmd}"},
122 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
123 },
124 "$zipTemplate": &remoteexec.REParams{
125 Labels: map[string]string{"type": "tool", "name": "soong_zip"},
126 Inputs: []string{"${config.SoongZipCmd}", "$outDir"},
127 OutputFiles: []string{"$outDir/classes.dex.jar"},
128 ExecStrategy: "${config.RED8ExecStrategy}",
129 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
130 },
Wei Li1e73c652021-12-06 13:35:11 -0800131 }, []string{"outDir", "d8Flags", "zipFlags", "tmpJar", "mergeZipsFlags"}, nil)
Colin Crossf0056cb2017-12-22 15:56:08 -0800132
Colin Cross77cdcfd2021-03-12 11:28:25 -0800133var r8, r8RE = pctx.MultiCommandRemoteStaticRules("r8",
Colin Cross66dbc0b2017-12-28 12:23:20 -0800134 blueprint.RuleParams{
135 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Colin Crosscb6143a2020-08-14 17:39:29 -0700136 `rm -f "$outDict" && rm -rf "${outUsageDir}" && ` +
137 `mkdir -p $$(dirname ${outUsage}) && ` +
Colin Crossa79a52c2021-08-04 10:52:44 -0700138 `mkdir -p $$(dirname $tmpJar) && ` +
139 `${config.Zip2ZipCmd} -i $in -o $tmpJar -x '**/*.dex' && ` +
Jared Duke0cf7c962022-04-20 20:28:33 +0000140 `$r8Template${config.R8Cmd} ${config.R8Flags} -injars $tmpJar --output $outDir ` +
Søren Gjesse24f17022018-09-14 15:20:42 +0200141 `--no-data-resources ` +
Colin Crosscb6143a2020-08-14 17:39:29 -0700142 `-printmapping ${outDict} ` +
143 `-printusage ${outUsage} ` +
Colin Cross22e6a6f2022-03-21 12:19:44 -0700144 `--deps-file ${out}.d ` +
Colin Crossffb657e2018-09-21 12:29:22 -0700145 `$r8Flags && ` +
Colin Crosscb6143a2020-08-14 17:39:29 -0700146 `touch "${outDict}" "${outUsage}" && ` +
147 `${config.SoongZipCmd} -o ${outUsageZip} -C ${outUsageDir} -f ${outUsage} && ` +
148 `rm -rf ${outUsageDir} && ` +
Kousik Kumar366afc52020-05-20 11:27:16 -0700149 `$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` +
Wei Li1e73c652021-12-06 13:35:11 -0800150 `${config.MergeZipsCmd} -D -stripFile "**/*.class" $mergeZipsFlags $out $outDir/classes.dex.jar $in`,
Colin Cross22e6a6f2022-03-21 12:19:44 -0700151 Depfile: "${out}.d",
152 Deps: blueprint.DepsGCC,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800153 CommandDeps: []string{
Colin Crossa832a042021-08-05 16:56:18 -0700154 "${config.R8Cmd}",
Colin Crossa79a52c2021-08-04 10:52:44 -0700155 "${config.Zip2ZipCmd}",
Colin Cross66dbc0b2017-12-28 12:23:20 -0800156 "${config.SoongZipCmd}",
157 "${config.MergeZipsCmd}",
158 },
Kousik Kumar366afc52020-05-20 11:27:16 -0700159 }, map[string]*remoteexec.REParams{
160 "$r8Template": &remoteexec.REParams{
161 Labels: map[string]string{"type": "compile", "compiler": "r8"},
162 Inputs: []string{"$implicits", "${config.R8Jar}"},
Colin Crosse00c0e72020-09-17 11:54:30 -0700163 OutputFiles: []string{"${outUsage}"},
Kousik Kumar366afc52020-05-20 11:27:16 -0700164 ExecStrategy: "${config.RER8ExecStrategy}",
165 ToolchainInputs: []string{"${config.JavaCmd}"},
166 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
167 },
168 "$zipTemplate": &remoteexec.REParams{
169 Labels: map[string]string{"type": "tool", "name": "soong_zip"},
170 Inputs: []string{"${config.SoongZipCmd}", "$outDir"},
171 OutputFiles: []string{"$outDir/classes.dex.jar"},
172 ExecStrategy: "${config.RER8ExecStrategy}",
173 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
174 },
Colin Crosscb6143a2020-08-14 17:39:29 -0700175 "$zipUsageTemplate": &remoteexec.REParams{
176 Labels: map[string]string{"type": "tool", "name": "soong_zip"},
177 Inputs: []string{"${config.SoongZipCmd}", "${outUsage}"},
178 OutputFiles: []string{"${outUsageZip}"},
179 ExecStrategy: "${config.RER8ExecStrategy}",
180 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
181 },
182 }, []string{"outDir", "outDict", "outUsage", "outUsageZip", "outUsageDir",
Wei Li1e73c652021-12-06 13:35:11 -0800183 "r8Flags", "zipFlags", "tmpJar", "mergeZipsFlags"}, []string{"implicits"})
Colin Cross66dbc0b2017-12-28 12:23:20 -0800184
Colin Cross5ea963e2021-09-16 19:13:43 -0700185func (d *dexer) dexCommonFlags(ctx android.ModuleContext,
Spandan Das7eb92432023-01-04 22:14:59 +0000186 minSdkVersion android.SdkSpec) (flags []string, deps android.Paths) {
Colin Cross5ea963e2021-09-16 19:13:43 -0700187
188 flags = d.dexProperties.Dxflags
Colin Crossbafb8972018-06-06 21:46:32 +0000189 // Translate all the DX flags to D8 ones until all the build files have been migrated
190 // to D8 flags. See: b/69377755
191 flags = android.RemoveListFromList(flags,
192 []string{"--core-library", "--dex", "--multi-dex"})
Colin Crossf0056cb2017-12-22 15:56:08 -0800193
Colin Cross5ea963e2021-09-16 19:13:43 -0700194 for _, f := range android.PathsForModuleSrc(ctx, d.dexProperties.Main_dex_rules) {
195 flags = append(flags, "--main-dex-rules", f.String())
196 deps = append(deps, f)
197 }
198
Colin Crossf0056cb2017-12-22 15:56:08 -0800199 if ctx.Config().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Crossbafb8972018-06-06 21:46:32 +0000200 flags = append(flags, "--debug")
Colin Crossf0056cb2017-12-22 15:56:08 -0800201 }
202
203 if ctx.Config().Getenv("GENERATE_DEX_DEBUG") != "" {
204 flags = append(flags,
205 "--debug",
206 "--verbose")
Colin Crossf0056cb2017-12-22 15:56:08 -0800207 }
208
Jared Duke40d731a2022-09-20 15:32:14 -0700209 // Supplying the platform build flag disables various features like API modeling and desugaring.
210 // For targets with a stable min SDK version (i.e., when the min SDK is both explicitly specified
211 // and managed+versioned), we suppress this flag to ensure portability.
212 // Note: Targets with a min SDK kind of core_platform (e.g., framework.jar) or unspecified (e.g.,
213 // services.jar), are not classified as stable, which is WAI.
214 // TODO(b/232073181): Expand to additional min SDK cases after validation.
Spandan Das7eb92432023-01-04 22:14:59 +0000215 if !minSdkVersion.Stable() {
Jared Duke40d731a2022-09-20 15:32:14 -0700216 flags = append(flags, "--android-platform-build")
217 }
218
Spandan Das7eb92432023-01-04 22:14:59 +0000219 effectiveVersion, err := minSdkVersion.EffectiveVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -0700220 if err != nil {
221 ctx.PropertyErrorf("min_sdk_version", "%s", err)
222 }
223
Jiyong Park54105c42021-03-31 18:17:53 +0900224 flags = append(flags, "--min-api "+strconv.Itoa(effectiveVersion.FinalOrFutureInt()))
Colin Cross5ea963e2021-09-16 19:13:43 -0700225 return flags, deps
Colin Crossf0056cb2017-12-22 15:56:08 -0800226}
227
Liz Kammera7a64f32020-07-09 15:16:41 -0700228func d8Flags(flags javaBuilderFlags) (d8Flags []string, d8Deps android.Paths) {
Colin Crossc2557d12019-10-31 15:22:57 -0700229 d8Flags = append(d8Flags, flags.bootClasspath.FormRepeatedClassPath("--lib ")...)
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700230 d8Flags = append(d8Flags, flags.dexClasspath.FormRepeatedClassPath("--lib ")...)
Colin Crossffb657e2018-09-21 12:29:22 -0700231
Colin Cross6dab9bd2018-09-28 08:06:24 -0700232 d8Deps = append(d8Deps, flags.bootClasspath...)
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700233 d8Deps = append(d8Deps, flags.dexClasspath...)
Colin Cross6dab9bd2018-09-28 08:06:24 -0700234
235 return d8Flags, d8Deps
Colin Crossffb657e2018-09-21 12:29:22 -0700236}
237
Liz Kammera7a64f32020-07-09 15:16:41 -0700238func (d *dexer) r8Flags(ctx android.ModuleContext, flags javaBuilderFlags) (r8Flags []string, r8Deps android.Paths) {
239 opt := d.dexProperties.Optimize
Colin Cross66dbc0b2017-12-28 12:23:20 -0800240
241 // When an app contains references to APIs that are not in the SDK specified by
242 // its LOCAL_SDK_VERSION for example added by support library or by runtime
Colin Crossbafb8972018-06-06 21:46:32 +0000243 // classes added by desugaring, we artifically raise the "SDK version" "linked" by
Colin Cross66dbc0b2017-12-28 12:23:20 -0800244 // ProGuard, to
245 // - suppress ProGuard warnings of referencing symbols unknown to the lower SDK version.
246 // - prevent ProGuard stripping subclass in the support library that extends class added in the higher SDK version.
247 // See b/20667396
248 var proguardRaiseDeps classpath
Colin Crossdcf71b22021-02-01 13:59:03 -0800249 ctx.VisitDirectDepsWithTag(proguardRaiseTag, func(m android.Module) {
250 dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo)
251 proguardRaiseDeps = append(proguardRaiseDeps, dep.HeaderJars...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800252 })
253
254 r8Flags = append(r8Flags, proguardRaiseDeps.FormJavaClassPath("-libraryjars"))
Colin Cross6dab9bd2018-09-28 08:06:24 -0700255 r8Deps = append(r8Deps, proguardRaiseDeps...)
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500256 r8Flags = append(r8Flags, flags.bootClasspath.FormJavaClassPath("-libraryjars"))
Colin Cross6dab9bd2018-09-28 08:06:24 -0700257 r8Deps = append(r8Deps, flags.bootClasspath...)
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500258 r8Flags = append(r8Flags, flags.dexClasspath.FormJavaClassPath("-libraryjars"))
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700259 r8Deps = append(r8Deps, flags.dexClasspath...)
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500260 r8Flags = append(r8Flags, flags.processorPath.FormJavaClassPath("-libraryjars"))
261 r8Deps = append(r8Deps, flags.processorPath...)
262
263 errorProneClasspath := classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
264 r8Flags = append(r8Flags, errorProneClasspath.FormJavaClassPath("-libraryjars"))
265 r8Deps = append(r8Deps, errorProneClasspath...)
266
267 transitiveStaticLibsLookupMap := map[android.Path]bool{}
268 if d.transitiveStaticLibsHeaderJars != nil {
269 for _, jar := range d.transitiveStaticLibsHeaderJars.ToList() {
270 transitiveStaticLibsLookupMap[jar] = true
271 }
272 }
273 transitiveHeaderJars := android.Paths{}
274 if d.transitiveLibsHeaderJars != nil {
275 for _, jar := range d.transitiveLibsHeaderJars.ToList() {
276 if _, ok := transitiveStaticLibsLookupMap[jar]; ok {
277 // don't include a lib if it is already packaged in the current JAR as a static lib
278 continue
279 }
280 transitiveHeaderJars = append(transitiveHeaderJars, jar)
281 }
282 }
283 transitiveClasspath := classpath(transitiveHeaderJars)
284 r8Flags = append(r8Flags, transitiveClasspath.FormJavaClassPath("-libraryjars"))
285 r8Deps = append(r8Deps, transitiveClasspath...)
Colin Cross6dab9bd2018-09-28 08:06:24 -0700286
Colin Cross66dbc0b2017-12-28 12:23:20 -0800287 flagFiles := android.Paths{
288 android.PathForSource(ctx, "build/make/core/proguard.flags"),
289 }
290
Liz Kammera7a64f32020-07-09 15:16:41 -0700291 flagFiles = append(flagFiles, d.extraProguardFlagFiles...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800292 // TODO(ccross): static android library proguard files
293
Liz Kammera7a64f32020-07-09 15:16:41 -0700294 flagFiles = append(flagFiles, android.PathsForModuleSrc(ctx, opt.Proguard_flags_files)...)
Colin Crossbd1cef52018-08-13 10:28:18 -0700295
Colin Cross66dbc0b2017-12-28 12:23:20 -0800296 r8Flags = append(r8Flags, android.JoinWithPrefix(flagFiles.Strings(), "-include "))
297 r8Deps = append(r8Deps, flagFiles...)
298
299 // TODO(b/70942988): This is included from build/make/core/proguard.flags
300 r8Deps = append(r8Deps, android.PathForSource(ctx,
301 "build/make/core/proguard_basic_keeps.flags"))
302
Liz Kammera7a64f32020-07-09 15:16:41 -0700303 r8Flags = append(r8Flags, opt.Proguard_flags...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800304
Christoffer Quist Adamsenf2d7b162020-08-24 15:56:16 +0200305 if BoolDefault(opt.Proguard_compatibility, true) {
306 r8Flags = append(r8Flags, "--force-proguard-compatibility")
Ian Zernyfc7df612021-11-02 15:37:06 +0100307 } else {
308 // TODO(b/213833843): Allow configuration of the prefix via a build variable.
309 var sourceFilePrefix = "go/retraceme "
310 var sourceFileTemplate = "\"" + sourceFilePrefix + "%MAP_ID\""
311 // TODO(b/200967150): Also tag the source file in compat builds.
312 if Bool(opt.Optimize) || Bool(opt.Obfuscate) {
313 r8Flags = append(r8Flags, "--map-id-template", "%MAP_HASH")
314 r8Flags = append(r8Flags, "--source-file-template", sourceFileTemplate)
315 }
Christoffer Quist Adamsenf2d7b162020-08-24 15:56:16 +0200316 }
317
Colin Cross66dbc0b2017-12-28 12:23:20 -0800318 // TODO(ccross): Don't shrink app instrumentation tests by default.
319 if !Bool(opt.Shrink) {
320 r8Flags = append(r8Flags, "-dontshrink")
321 }
322
323 if !Bool(opt.Optimize) {
324 r8Flags = append(r8Flags, "-dontoptimize")
325 }
326
327 // TODO(ccross): error if obufscation + app instrumentation test.
328 if !Bool(opt.Obfuscate) {
329 r8Flags = append(r8Flags, "-dontobfuscate")
330 }
Colin Cross4b964c02018-10-15 16:18:06 -0700331 // TODO(ccross): if this is an instrumentation test of an obfuscated app, use the
332 // dictionary of the app and move the app from libraryjars to injars.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800333
Jaewoong Jung1d6eb682018-11-29 15:08:44 -0800334 // Don't strip out debug information for eng builds.
335 if ctx.Config().Eng() {
336 r8Flags = append(r8Flags, "--debug")
337 }
338
Christoffer Quist Adamsene8507372021-02-22 09:44:09 +0100339 // TODO(b/180878971): missing classes should be added to the relevant builds.
Remi NGUYEN VANbdad3142022-08-04 13:19:03 +0900340 // TODO(b/229727645): do not use true as default for Android platform builds.
341 if proptools.BoolDefault(opt.Ignore_warnings, true) {
342 r8Flags = append(r8Flags, "-ignorewarnings")
343 }
Christoffer Quist Adamsene8507372021-02-22 09:44:09 +0100344
Colin Cross66dbc0b2017-12-28 12:23:20 -0800345 return r8Flags, r8Deps
346}
347
Spandan Das7eb92432023-01-04 22:14:59 +0000348func (d *dexer) compileDex(ctx android.ModuleContext, flags javaBuilderFlags, minSdkVersion android.SdkSpec,
349 classesJar android.Path, jarName string) android.OutputPath {
Colin Crossf0056cb2017-12-22 15:56:08 -0800350
Colin Crossf0056cb2017-12-22 15:56:08 -0800351 // Compile classes.jar into classes.dex and then javalib.jar
Spandan Das7eb92432023-01-04 22:14:59 +0000352 javalibJar := android.PathForModuleOut(ctx, "dex", jarName).OutputPath
Colin Crossf0056cb2017-12-22 15:56:08 -0800353 outDir := android.PathForModuleOut(ctx, "dex")
Spandan Das7eb92432023-01-04 22:14:59 +0000354 tmpJar := android.PathForModuleOut(ctx, "withres-withoutdex", jarName)
Colin Crossf0056cb2017-12-22 15:56:08 -0800355
Sasha Smundakd3cf4ee2019-02-15 10:14:23 -0800356 zipFlags := "--ignore_missing_files"
Liz Kammera7a64f32020-07-09 15:16:41 -0700357 if proptools.Bool(d.dexProperties.Uncompress_dex) {
Sasha Smundakd3cf4ee2019-02-15 10:14:23 -0800358 zipFlags += " -L 0"
Colin Cross5a0dcd52018-10-05 14:20:06 -0700359 }
360
Spandan Das7eb92432023-01-04 22:14:59 +0000361 commonFlags, commonDeps := d.dexCommonFlags(ctx, minSdkVersion)
Liz Kammera7a64f32020-07-09 15:16:41 -0700362
Wei Li1e73c652021-12-06 13:35:11 -0800363 // Exclude kotlinc generated files when "exclude_kotlinc_generated_files" is set to true.
364 mergeZipsFlags := ""
365 if proptools.BoolDefault(d.dexProperties.Exclude_kotlinc_generated_files, false) {
366 mergeZipsFlags = "-stripFile META-INF/*.kotlin_module -stripFile **/*.kotlin_builtins"
367 }
368
Liz Kammera7a64f32020-07-09 15:16:41 -0700369 useR8 := d.effectiveOptimizeEnabled()
Colin Cross66dbc0b2017-12-28 12:23:20 -0800370 if useR8 {
Colin Crossc0c664c2018-05-16 16:47:21 -0700371 proguardDictionary := android.PathForModuleOut(ctx, "proguard_dictionary")
Liz Kammera7a64f32020-07-09 15:16:41 -0700372 d.proguardDictionary = android.OptionalPathForPath(proguardDictionary)
Colin Crosscb6143a2020-08-14 17:39:29 -0700373 proguardUsageDir := android.PathForModuleOut(ctx, "proguard_usage")
374 proguardUsage := proguardUsageDir.Join(ctx, ctx.Namespace().Path,
375 android.ModuleNameWithPossibleOverride(ctx), "unused.txt")
376 proguardUsageZip := android.PathForModuleOut(ctx, "proguard_usage.zip")
377 d.proguardUsageZip = android.OptionalPathForPath(proguardUsageZip)
Spandan Das7eb92432023-01-04 22:14:59 +0000378 r8Flags, r8Deps := d.r8Flags(ctx, flags)
Colin Cross5ea963e2021-09-16 19:13:43 -0700379 r8Deps = append(r8Deps, commonDeps...)
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400380 rule := r8
381 args := map[string]string{
Wei Li1e73c652021-12-06 13:35:11 -0800382 "r8Flags": strings.Join(append(commonFlags, r8Flags...), " "),
383 "zipFlags": zipFlags,
384 "outDict": proguardDictionary.String(),
385 "outUsageDir": proguardUsageDir.String(),
386 "outUsage": proguardUsage.String(),
387 "outUsageZip": proguardUsageZip.String(),
388 "outDir": outDir.String(),
389 "tmpJar": tmpJar.String(),
390 "mergeZipsFlags": mergeZipsFlags,
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400391 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400392 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_R8") {
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400393 rule = r8RE
394 args["implicits"] = strings.Join(r8Deps.Strings(), ",")
395 }
Colin Cross66dbc0b2017-12-28 12:23:20 -0800396 ctx.Build(pctx, android.BuildParams{
Colin Crosscb6143a2020-08-14 17:39:29 -0700397 Rule: rule,
398 Description: "r8",
399 Output: javalibJar,
400 ImplicitOutputs: android.WritablePaths{proguardDictionary, proguardUsageZip},
Spandan Das7eb92432023-01-04 22:14:59 +0000401 Input: classesJar,
Colin Crosscb6143a2020-08-14 17:39:29 -0700402 Implicits: r8Deps,
403 Args: args,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800404 })
405 } else {
Spandan Das7eb92432023-01-04 22:14:59 +0000406 d8Flags, d8Deps := d8Flags(flags)
Colin Cross5ea963e2021-09-16 19:13:43 -0700407 d8Deps = append(d8Deps, commonDeps...)
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400408 rule := d8
Ramy Medhat16f23a42020-09-03 01:29:49 -0400409 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_D8") {
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400410 rule = d8RE
411 }
Colin Cross66dbc0b2017-12-28 12:23:20 -0800412 ctx.Build(pctx, android.BuildParams{
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400413 Rule: rule,
Colin Crossbafb8972018-06-06 21:46:32 +0000414 Description: "d8",
Colin Cross66dbc0b2017-12-28 12:23:20 -0800415 Output: javalibJar,
Spandan Das7eb92432023-01-04 22:14:59 +0000416 Input: classesJar,
Colin Cross6dab9bd2018-09-28 08:06:24 -0700417 Implicits: d8Deps,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800418 Args: map[string]string{
Wei Li1e73c652021-12-06 13:35:11 -0800419 "d8Flags": strings.Join(append(commonFlags, d8Flags...), " "),
420 "zipFlags": zipFlags,
421 "outDir": outDir.String(),
422 "tmpJar": tmpJar.String(),
423 "mergeZipsFlags": mergeZipsFlags,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800424 },
425 })
Colin Crossf0056cb2017-12-22 15:56:08 -0800426 }
Liz Kammera7a64f32020-07-09 15:16:41 -0700427 if proptools.Bool(d.dexProperties.Uncompress_dex) {
Spandan Das7eb92432023-01-04 22:14:59 +0000428 alignedJavalibJar := android.PathForModuleOut(ctx, "aligned", jarName).OutputPath
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000429 TransformZipAlign(ctx, alignedJavalibJar, javalibJar)
430 javalibJar = alignedJavalibJar
431 }
Colin Crossf0056cb2017-12-22 15:56:08 -0800432
Colin Crossf0056cb2017-12-22 15:56:08 -0800433 return javalibJar
434}