blob: 8af06d5303b8d5bc97164ba77da9294832f4a15d [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
Ajinkya Chalkeddad41b2023-02-09 14:09:58 +000045 // Whether to continue building even if warnings are emitted. Defaults to true.
Remi NGUYEN VANbdad3142022-08-04 13:19:03 +090046 Ignore_warnings *bool
47
Jared Dukeaa88b3d2023-08-29 17:07:20 +000048 // If true, runs R8 in Proguard compatibility mode, otherwise runs R8 in full mode.
49 // Defaults to false for apps, true for libraries and tests.
Christoffer Quist Adamsenf2d7b162020-08-24 15:56:16 +020050 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"`
Sam Delmerico95d70942023-08-02 18:00:35 -040074
75 // If true, transitive reverse dependencies of this module will have this
76 // module's proguard spec appended to their optimization action
77 Export_proguard_flags_files *bool
Liz Kammera7a64f32020-07-09 15:16:41 -070078 }
79
80 // Keep the data uncompressed. We always need uncompressed dex for execution,
81 // so this might actually save space by avoiding storing the same data twice.
82 // This defaults to reasonable value based on module and should not be set.
83 // It exists only to support ART tests.
84 Uncompress_dex *bool
Wei Li1e73c652021-12-06 13:35:11 -080085
Wei Li92cd54b2022-01-12 13:22:55 -080086 // Exclude kotlinc generate files: *.kotlin_module, *.kotlin_builtins. Defaults to false.
Wei Li1e73c652021-12-06 13:35:11 -080087 Exclude_kotlinc_generated_files *bool
Liz Kammera7a64f32020-07-09 15:16:41 -070088}
89
90type dexer struct {
91 dexProperties DexProperties
92
93 // list of extra proguard flag files
94 extraProguardFlagFiles android.Paths
95 proguardDictionary android.OptionalPath
Ian Zerny82044f12023-01-25 12:11:27 +010096 proguardConfiguration android.OptionalPath
Colin Crosscb6143a2020-08-14 17:39:29 -070097 proguardUsageZip android.OptionalPath
Sam Delmerico9f9c0a22022-11-29 11:19:37 -050098
99 providesTransitiveHeaderJars
Liz Kammera7a64f32020-07-09 15:16:41 -0700100}
101
102func (d *dexer) effectiveOptimizeEnabled() bool {
103 return BoolDefault(d.dexProperties.Optimize.Enabled, d.dexProperties.Optimize.EnabledByDefault)
104}
105
Colin Cross77cdcfd2021-03-12 11:28:25 -0800106var d8, d8RE = pctx.MultiCommandRemoteStaticRules("d8",
Colin Crossf0056cb2017-12-22 15:56:08 -0800107 blueprint.RuleParams{
108 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
David Srbeckybda964c2023-11-24 15:57:54 +0000109 `$d8Template${config.D8Cmd} ${config.D8Flags} $d8Flags --output $outDir --no-dex-input-jar $in && ` +
Kousik Kumar366afc52020-05-20 11:27:16 -0700110 `$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` +
Colin Cross56e28402023-09-28 14:38:06 -0700111 `${config.MergeZipsCmd} -D -stripFile "**/*.class" $mergeZipsFlags $out $outDir/classes.dex.jar $in && ` +
Ian Zernydb2d35b2023-10-10 12:22:39 +0200112 `rm -f "$outDir/classes*.dex" "$outDir/classes.dex.jar"`,
Colin Crossf0056cb2017-12-22 15:56:08 -0800113 CommandDeps: []string{
114 "${config.D8Cmd}",
115 "${config.SoongZipCmd}",
116 "${config.MergeZipsCmd}",
117 },
Kousik Kumar366afc52020-05-20 11:27:16 -0700118 }, map[string]*remoteexec.REParams{
119 "$d8Template": &remoteexec.REParams{
120 Labels: map[string]string{"type": "compile", "compiler": "d8"},
121 Inputs: []string{"${config.D8Jar}"},
122 ExecStrategy: "${config.RED8ExecStrategy}",
123 ToolchainInputs: []string{"${config.JavaCmd}"},
124 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
125 },
126 "$zipTemplate": &remoteexec.REParams{
127 Labels: map[string]string{"type": "tool", "name": "soong_zip"},
128 Inputs: []string{"${config.SoongZipCmd}", "$outDir"},
129 OutputFiles: []string{"$outDir/classes.dex.jar"},
130 ExecStrategy: "${config.RED8ExecStrategy}",
131 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
132 },
Ian Zernydb2d35b2023-10-10 12:22:39 +0200133 }, []string{"outDir", "d8Flags", "zipFlags", "mergeZipsFlags"}, nil)
Colin Crossf0056cb2017-12-22 15:56:08 -0800134
Colin Cross77cdcfd2021-03-12 11:28:25 -0800135var r8, r8RE = pctx.MultiCommandRemoteStaticRules("r8",
Colin Cross66dbc0b2017-12-28 12:23:20 -0800136 blueprint.RuleParams{
137 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Ian Zerny82044f12023-01-25 12:11:27 +0100138 `rm -f "$outDict" && rm -f "$outConfig" && rm -rf "${outUsageDir}" && ` +
Colin Crosscb6143a2020-08-14 17:39:29 -0700139 `mkdir -p $$(dirname ${outUsage}) && ` +
David Srbeckybda964c2023-11-24 15:57:54 +0000140 `$r8Template${config.R8Cmd} ${config.R8Flags} $r8Flags -injars $in --output $outDir ` +
Søren Gjesse24f17022018-09-14 15:20:42 +0200141 `--no-data-resources ` +
Colin Crosscb6143a2020-08-14 17:39:29 -0700142 `-printmapping ${outDict} ` +
Jared Duke34c6d7d2023-05-05 20:40:20 +0000143 `-printconfiguration ${outConfig} ` +
Colin Crosscb6143a2020-08-14 17:39:29 -0700144 `-printusage ${outUsage} ` +
David Srbeckybda964c2023-11-24 15:57:54 +0000145 `--deps-file ${out}.d && ` +
Ian Zerny82044f12023-01-25 12:11:27 +0100146 `touch "${outDict}" "${outConfig}" "${outUsage}" && ` +
Colin Crosscb6143a2020-08-14 17:39:29 -0700147 `${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" && ` +
Colin Cross56e28402023-09-28 14:38:06 -0700150 `${config.MergeZipsCmd} -D -stripFile "**/*.class" $mergeZipsFlags $out $outDir/classes.dex.jar $in && ` +
Colin Crossb716ceb2023-10-03 09:40:06 -0700151 `rm -f "$outDir/classes*.dex" "$outDir/classes.dex.jar"`,
Colin Cross22e6a6f2022-03-21 12:19:44 -0700152 Depfile: "${out}.d",
153 Deps: blueprint.DepsGCC,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800154 CommandDeps: []string{
Colin Crossa832a042021-08-05 16:56:18 -0700155 "${config.R8Cmd}",
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}"},
Rico Winda9fd59a2023-11-03 10:26:38 +0000163 OutputFiles: []string{"${outUsage}", "${outConfig}", "${outDict}"},
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 },
Ian Zerny82044f12023-01-25 12:11:27 +0100182 }, []string{"outDir", "outDict", "outConfig", "outUsage", "outUsageZip", "outUsageDir",
Rico Winda9fd59a2023-11-03 10:26:38 +0000183 "r8Flags", "zipFlags", "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 Dasc404cc72023-02-23 18:05:05 +0000186 dexParams *compileDexParams) (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.
Ian Zernyc26029b2023-08-25 13:43:44 +0200215 var addAndroidPlatformBuildFlag = false
Spandan Dasc404cc72023-02-23 18:05:05 +0000216 if !dexParams.sdkVersion.Stable() {
Ian Zernyc26029b2023-08-25 13:43:44 +0200217 addAndroidPlatformBuildFlag = true
Jared Duke40d731a2022-09-20 15:32:14 -0700218 }
219
Spandan Dasc404cc72023-02-23 18:05:05 +0000220 effectiveVersion, err := dexParams.minSdkVersion.EffectiveVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -0700221 if err != nil {
222 ctx.PropertyErrorf("min_sdk_version", "%s", err)
223 }
Søren Gjesseb82a2a22022-10-03 14:15:52 +0200224 // V is 35, but we have not bumped the SDK version yet, so check for both.
225 if ctx.Config().PlatformSdkVersion().FinalInt() >= 35 ||
226 ctx.Config().PlatformSdkCodename() == "VanillaIceCream" {
227 flags = append([]string{"-JDcom.android.tools.r8.dexContainerExperiment"}, flags...)
228 }
Colin Cross83bb3162018-06-25 15:48:06 -0700229
Ian Zernyc26029b2023-08-25 13:43:44 +0200230 // If the specified SDK level is 10000, then configure the compiler to use the
231 // current platform SDK level and to compile the build as a platform build.
232 var minApiFlagValue = effectiveVersion.FinalOrFutureInt()
233 if minApiFlagValue == 10000 {
234 minApiFlagValue = ctx.Config().PlatformSdkVersion().FinalInt()
235 addAndroidPlatformBuildFlag = true
236 }
237 flags = append(flags, "--min-api "+strconv.Itoa(minApiFlagValue))
238
239 if addAndroidPlatformBuildFlag {
240 flags = append(flags, "--android-platform-build")
241 }
Colin Cross5ea963e2021-09-16 19:13:43 -0700242 return flags, deps
Colin Crossf0056cb2017-12-22 15:56:08 -0800243}
244
Liz Kammera7a64f32020-07-09 15:16:41 -0700245func d8Flags(flags javaBuilderFlags) (d8Flags []string, d8Deps android.Paths) {
Colin Crossc2557d12019-10-31 15:22:57 -0700246 d8Flags = append(d8Flags, flags.bootClasspath.FormRepeatedClassPath("--lib ")...)
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700247 d8Flags = append(d8Flags, flags.dexClasspath.FormRepeatedClassPath("--lib ")...)
Colin Crossffb657e2018-09-21 12:29:22 -0700248
Colin Cross6dab9bd2018-09-28 08:06:24 -0700249 d8Deps = append(d8Deps, flags.bootClasspath...)
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700250 d8Deps = append(d8Deps, flags.dexClasspath...)
Colin Cross6dab9bd2018-09-28 08:06:24 -0700251
252 return d8Flags, d8Deps
Colin Crossffb657e2018-09-21 12:29:22 -0700253}
254
Liz Kammera7a64f32020-07-09 15:16:41 -0700255func (d *dexer) r8Flags(ctx android.ModuleContext, flags javaBuilderFlags) (r8Flags []string, r8Deps android.Paths) {
256 opt := d.dexProperties.Optimize
Colin Cross66dbc0b2017-12-28 12:23:20 -0800257
258 // When an app contains references to APIs that are not in the SDK specified by
259 // its LOCAL_SDK_VERSION for example added by support library or by runtime
Colin Crossbafb8972018-06-06 21:46:32 +0000260 // classes added by desugaring, we artifically raise the "SDK version" "linked" by
Colin Cross66dbc0b2017-12-28 12:23:20 -0800261 // ProGuard, to
262 // - suppress ProGuard warnings of referencing symbols unknown to the lower SDK version.
263 // - prevent ProGuard stripping subclass in the support library that extends class added in the higher SDK version.
264 // See b/20667396
265 var proguardRaiseDeps classpath
Colin Crossdcf71b22021-02-01 13:59:03 -0800266 ctx.VisitDirectDepsWithTag(proguardRaiseTag, func(m android.Module) {
267 dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo)
268 proguardRaiseDeps = append(proguardRaiseDeps, dep.HeaderJars...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800269 })
270
271 r8Flags = append(r8Flags, proguardRaiseDeps.FormJavaClassPath("-libraryjars"))
Colin Cross6dab9bd2018-09-28 08:06:24 -0700272 r8Deps = append(r8Deps, proguardRaiseDeps...)
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500273 r8Flags = append(r8Flags, flags.bootClasspath.FormJavaClassPath("-libraryjars"))
Colin Cross6dab9bd2018-09-28 08:06:24 -0700274 r8Deps = append(r8Deps, flags.bootClasspath...)
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500275 r8Flags = append(r8Flags, flags.dexClasspath.FormJavaClassPath("-libraryjars"))
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700276 r8Deps = append(r8Deps, flags.dexClasspath...)
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500277
278 transitiveStaticLibsLookupMap := map[android.Path]bool{}
279 if d.transitiveStaticLibsHeaderJars != nil {
280 for _, jar := range d.transitiveStaticLibsHeaderJars.ToList() {
281 transitiveStaticLibsLookupMap[jar] = true
282 }
283 }
284 transitiveHeaderJars := android.Paths{}
285 if d.transitiveLibsHeaderJars != nil {
286 for _, jar := range d.transitiveLibsHeaderJars.ToList() {
287 if _, ok := transitiveStaticLibsLookupMap[jar]; ok {
288 // don't include a lib if it is already packaged in the current JAR as a static lib
289 continue
290 }
291 transitiveHeaderJars = append(transitiveHeaderJars, jar)
292 }
293 }
294 transitiveClasspath := classpath(transitiveHeaderJars)
295 r8Flags = append(r8Flags, transitiveClasspath.FormJavaClassPath("-libraryjars"))
296 r8Deps = append(r8Deps, transitiveClasspath...)
Colin Cross6dab9bd2018-09-28 08:06:24 -0700297
Colin Cross66dbc0b2017-12-28 12:23:20 -0800298 flagFiles := android.Paths{
299 android.PathForSource(ctx, "build/make/core/proguard.flags"),
300 }
301
Liz Kammera7a64f32020-07-09 15:16:41 -0700302 flagFiles = append(flagFiles, d.extraProguardFlagFiles...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800303 // TODO(ccross): static android library proguard files
304
Liz Kammera7a64f32020-07-09 15:16:41 -0700305 flagFiles = append(flagFiles, android.PathsForModuleSrc(ctx, opt.Proguard_flags_files)...)
Colin Crossbd1cef52018-08-13 10:28:18 -0700306
Sam Delmericoc8e040c2023-10-31 17:27:02 +0000307 flagFiles = android.FirstUniquePaths(flagFiles)
308
Colin Cross66dbc0b2017-12-28 12:23:20 -0800309 r8Flags = append(r8Flags, android.JoinWithPrefix(flagFiles.Strings(), "-include "))
310 r8Deps = append(r8Deps, flagFiles...)
311
312 // TODO(b/70942988): This is included from build/make/core/proguard.flags
313 r8Deps = append(r8Deps, android.PathForSource(ctx,
314 "build/make/core/proguard_basic_keeps.flags"))
315
Liz Kammera7a64f32020-07-09 15:16:41 -0700316 r8Flags = append(r8Flags, opt.Proguard_flags...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800317
Christoffer Quist Adamsenf2d7b162020-08-24 15:56:16 +0200318 if BoolDefault(opt.Proguard_compatibility, true) {
319 r8Flags = append(r8Flags, "--force-proguard-compatibility")
Jared Dukeb832fbb2023-09-15 17:16:36 +0000320 }
321
322 if Bool(opt.Optimize) || Bool(opt.Obfuscate) {
Ian Zernyfc7df612021-11-02 15:37:06 +0100323 // TODO(b/213833843): Allow configuration of the prefix via a build variable.
324 var sourceFilePrefix = "go/retraceme "
325 var sourceFileTemplate = "\"" + sourceFilePrefix + "%MAP_ID\""
Jared Dukeb832fbb2023-09-15 17:16:36 +0000326 r8Flags = append(r8Flags, "--map-id-template", "%MAP_HASH")
327 r8Flags = append(r8Flags, "--source-file-template", sourceFileTemplate)
Christoffer Quist Adamsenf2d7b162020-08-24 15:56:16 +0200328 }
329
Colin Cross66dbc0b2017-12-28 12:23:20 -0800330 // TODO(ccross): Don't shrink app instrumentation tests by default.
331 if !Bool(opt.Shrink) {
332 r8Flags = append(r8Flags, "-dontshrink")
333 }
334
335 if !Bool(opt.Optimize) {
336 r8Flags = append(r8Flags, "-dontoptimize")
337 }
338
339 // TODO(ccross): error if obufscation + app instrumentation test.
340 if !Bool(opt.Obfuscate) {
341 r8Flags = append(r8Flags, "-dontobfuscate")
342 }
Colin Cross4b964c02018-10-15 16:18:06 -0700343 // TODO(ccross): if this is an instrumentation test of an obfuscated app, use the
344 // dictionary of the app and move the app from libraryjars to injars.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800345
Jaewoong Jung1d6eb682018-11-29 15:08:44 -0800346 // Don't strip out debug information for eng builds.
347 if ctx.Config().Eng() {
348 r8Flags = append(r8Flags, "--debug")
349 }
350
Christoffer Quist Adamsene8507372021-02-22 09:44:09 +0100351 // TODO(b/180878971): missing classes should be added to the relevant builds.
Ajinkya Chalkeddad41b2023-02-09 14:09:58 +0000352 // TODO(b/229727645): do not use true as default for Android platform builds.
353 if proptools.BoolDefault(opt.Ignore_warnings, true) {
Remi NGUYEN VANbdad3142022-08-04 13:19:03 +0900354 r8Flags = append(r8Flags, "-ignorewarnings")
355 }
Christoffer Quist Adamsene8507372021-02-22 09:44:09 +0100356
Colin Cross66dbc0b2017-12-28 12:23:20 -0800357 return r8Flags, r8Deps
358}
359
Spandan Dasc404cc72023-02-23 18:05:05 +0000360type compileDexParams struct {
361 flags javaBuilderFlags
362 sdkVersion android.SdkSpec
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000363 minSdkVersion android.ApiLevel
Spandan Dasc404cc72023-02-23 18:05:05 +0000364 classesJar android.Path
365 jarName string
366}
367
368func (d *dexer) compileDex(ctx android.ModuleContext, dexParams *compileDexParams) android.OutputPath {
Colin Crossf0056cb2017-12-22 15:56:08 -0800369
Colin Crossf0056cb2017-12-22 15:56:08 -0800370 // Compile classes.jar into classes.dex and then javalib.jar
Spandan Dasc404cc72023-02-23 18:05:05 +0000371 javalibJar := android.PathForModuleOut(ctx, "dex", dexParams.jarName).OutputPath
Colin Crossf0056cb2017-12-22 15:56:08 -0800372 outDir := android.PathForModuleOut(ctx, "dex")
373
Sasha Smundakd3cf4ee2019-02-15 10:14:23 -0800374 zipFlags := "--ignore_missing_files"
Liz Kammera7a64f32020-07-09 15:16:41 -0700375 if proptools.Bool(d.dexProperties.Uncompress_dex) {
Sasha Smundakd3cf4ee2019-02-15 10:14:23 -0800376 zipFlags += " -L 0"
Colin Cross5a0dcd52018-10-05 14:20:06 -0700377 }
378
Spandan Dasc404cc72023-02-23 18:05:05 +0000379 commonFlags, commonDeps := d.dexCommonFlags(ctx, dexParams)
Liz Kammera7a64f32020-07-09 15:16:41 -0700380
Wei Li1e73c652021-12-06 13:35:11 -0800381 // Exclude kotlinc generated files when "exclude_kotlinc_generated_files" is set to true.
382 mergeZipsFlags := ""
383 if proptools.BoolDefault(d.dexProperties.Exclude_kotlinc_generated_files, false) {
384 mergeZipsFlags = "-stripFile META-INF/*.kotlin_module -stripFile **/*.kotlin_builtins"
385 }
386
Liz Kammera7a64f32020-07-09 15:16:41 -0700387 useR8 := d.effectiveOptimizeEnabled()
Colin Cross66dbc0b2017-12-28 12:23:20 -0800388 if useR8 {
Colin Crossc0c664c2018-05-16 16:47:21 -0700389 proguardDictionary := android.PathForModuleOut(ctx, "proguard_dictionary")
Liz Kammera7a64f32020-07-09 15:16:41 -0700390 d.proguardDictionary = android.OptionalPathForPath(proguardDictionary)
Ian Zerny82044f12023-01-25 12:11:27 +0100391 proguardConfiguration := android.PathForModuleOut(ctx, "proguard_configuration")
392 d.proguardConfiguration = android.OptionalPathForPath(proguardConfiguration)
Colin Crosscb6143a2020-08-14 17:39:29 -0700393 proguardUsageDir := android.PathForModuleOut(ctx, "proguard_usage")
394 proguardUsage := proguardUsageDir.Join(ctx, ctx.Namespace().Path,
395 android.ModuleNameWithPossibleOverride(ctx), "unused.txt")
396 proguardUsageZip := android.PathForModuleOut(ctx, "proguard_usage.zip")
397 d.proguardUsageZip = android.OptionalPathForPath(proguardUsageZip)
Spandan Dasc404cc72023-02-23 18:05:05 +0000398 r8Flags, r8Deps := d.r8Flags(ctx, dexParams.flags)
Colin Cross5ea963e2021-09-16 19:13:43 -0700399 r8Deps = append(r8Deps, commonDeps...)
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400400 rule := r8
401 args := map[string]string{
Wei Li1e73c652021-12-06 13:35:11 -0800402 "r8Flags": strings.Join(append(commonFlags, r8Flags...), " "),
403 "zipFlags": zipFlags,
404 "outDict": proguardDictionary.String(),
Ian Zerny82044f12023-01-25 12:11:27 +0100405 "outConfig": proguardConfiguration.String(),
Wei Li1e73c652021-12-06 13:35:11 -0800406 "outUsageDir": proguardUsageDir.String(),
407 "outUsage": proguardUsage.String(),
408 "outUsageZip": proguardUsageZip.String(),
409 "outDir": outDir.String(),
Wei Li1e73c652021-12-06 13:35:11 -0800410 "mergeZipsFlags": mergeZipsFlags,
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400411 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400412 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_R8") {
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400413 rule = r8RE
414 args["implicits"] = strings.Join(r8Deps.Strings(), ",")
415 }
Colin Cross66dbc0b2017-12-28 12:23:20 -0800416 ctx.Build(pctx, android.BuildParams{
Rico Winda9fd59a2023-11-03 10:26:38 +0000417 Rule: rule,
418 Description: "r8",
419 Output: javalibJar,
420 ImplicitOutputs: android.WritablePaths{
421 proguardDictionary,
422 proguardUsageZip,
423 proguardConfiguration},
424 Input: dexParams.classesJar,
425 Implicits: r8Deps,
426 Args: args,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800427 })
428 } else {
Spandan Dasc404cc72023-02-23 18:05:05 +0000429 d8Flags, d8Deps := d8Flags(dexParams.flags)
Colin Cross5ea963e2021-09-16 19:13:43 -0700430 d8Deps = append(d8Deps, commonDeps...)
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400431 rule := d8
Ramy Medhat16f23a42020-09-03 01:29:49 -0400432 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_D8") {
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400433 rule = d8RE
434 }
Colin Cross66dbc0b2017-12-28 12:23:20 -0800435 ctx.Build(pctx, android.BuildParams{
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400436 Rule: rule,
Colin Crossbafb8972018-06-06 21:46:32 +0000437 Description: "d8",
Colin Cross66dbc0b2017-12-28 12:23:20 -0800438 Output: javalibJar,
Spandan Dasc404cc72023-02-23 18:05:05 +0000439 Input: dexParams.classesJar,
Colin Cross6dab9bd2018-09-28 08:06:24 -0700440 Implicits: d8Deps,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800441 Args: map[string]string{
Wei Li1e73c652021-12-06 13:35:11 -0800442 "d8Flags": strings.Join(append(commonFlags, d8Flags...), " "),
443 "zipFlags": zipFlags,
444 "outDir": outDir.String(),
Wei Li1e73c652021-12-06 13:35:11 -0800445 "mergeZipsFlags": mergeZipsFlags,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800446 },
447 })
Colin Crossf0056cb2017-12-22 15:56:08 -0800448 }
Liz Kammera7a64f32020-07-09 15:16:41 -0700449 if proptools.Bool(d.dexProperties.Uncompress_dex) {
Spandan Dasc404cc72023-02-23 18:05:05 +0000450 alignedJavalibJar := android.PathForModuleOut(ctx, "aligned", dexParams.jarName).OutputPath
Cole Faust51d7bfd2023-09-07 05:31:32 +0000451 TransformZipAlign(ctx, alignedJavalibJar, javalibJar, nil)
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000452 javalibJar = alignedJavalibJar
453 }
Colin Crossf0056cb2017-12-22 15:56:08 -0800454
Colin Crossf0056cb2017-12-22 15:56:08 -0800455 return javalibJar
456}