blob: 3df03a889c02282dfa9c81b6c9bbfb94f563f095 [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
Christoffer Adamsenc8fd1f12025-01-06 19:04:03 +010045 // Whether to allow that library classes inherit from program classes.
46 // Defaults to false.
47 Ignore_library_extends_program *bool
48
Ajinkya Chalkeddad41b2023-02-09 14:09:58 +000049 // Whether to continue building even if warnings are emitted. Defaults to true.
Remi NGUYEN VANbdad3142022-08-04 13:19:03 +090050 Ignore_warnings *bool
51
Christoffer Adamsen8ec5f612025-01-09 11:05:11 +010052 // Whether runtime invisible annotations should be kept by R8. Defaults to false.
53 // This is equivalent to:
54 // -keepattributes RuntimeInvisibleAnnotations,
55 // RuntimeInvisibleParameterAnnotations,
56 // RuntimeInvisibleTypeAnnotations
57 // This is only applicable when RELEASE_R8_ONLY_RUNTIME_VISIBLE_ANNOTATIONS is
58 // enabled and will be used to migrate away from keeping runtime invisible
59 // annotations (b/387958004).
60 Keep_runtime_invisible_annotations *bool
61
Jared Dukeaa88b3d2023-08-29 17:07:20 +000062 // If true, runs R8 in Proguard compatibility mode, otherwise runs R8 in full mode.
Christoffer Adamsenc8fd1f12025-01-06 19:04:03 +010063 // Defaults to false for apps and tests, true for libraries.
Christoffer Quist Adamsenf2d7b162020-08-24 15:56:16 +020064 Proguard_compatibility *bool
65
Christoffer Adamsen18c1da52025-02-07 03:36:47 -080066 // If true, R8 will not add public or protected members (fields or methods) to
67 // the API surface of the compilation unit, i.e., classes that are kept or
68 // have kept subclasses will not expose any members added by R8 for internal
69 // use. That includes renamed members if obfuscation is enabled.
70 // This should only be used for building targets that go on the bootclasspath.
71 // Defaults to false.
72 Protect_api_surface *bool
73
Liz Kammera7a64f32020-07-09 15:16:41 -070074 // If true, optimize for size by removing unused code. Defaults to true for apps,
75 // false for libraries and tests.
76 Shrink *bool
77
78 // If true, optimize bytecode. Defaults to false.
79 Optimize *bool
80
81 // If true, obfuscate bytecode. Defaults to false.
82 Obfuscate *bool
83
84 // If true, do not use the flag files generated by aapt that automatically keep
85 // classes referenced by the app manifest. Defaults to false.
86 No_aapt_flags *bool
87
Jared Duke51b0a102022-09-27 16:53:11 -070088 // If true, optimize for size by removing unused resources. Defaults to false.
Rico Wind351bac92022-09-22 10:41:42 +020089 Shrink_resources *bool
90
Rico Winda2fa2632024-03-13 13:09:17 +010091 // If true, use optimized resource shrinking in R8, overriding the
92 // Shrink_resources setting. Defaults to false.
93 // Optimized shrinking means that R8 will trace and treeshake resources together with code
94 // and apply additional optimizations. This implies non final fields in the R classes.
95 Optimized_shrink_resources *bool
96
Liz Kammera7a64f32020-07-09 15:16:41 -070097 // Flags to pass to proguard.
98 Proguard_flags []string
99
100 // Specifies the locations of files containing proguard flags.
101 Proguard_flags_files []string `android:"path"`
Sam Delmerico95d70942023-08-02 18:00:35 -0400102
103 // If true, transitive reverse dependencies of this module will have this
104 // module's proguard spec appended to their optimization action
105 Export_proguard_flags_files *bool
Liz Kammera7a64f32020-07-09 15:16:41 -0700106 }
107
108 // Keep the data uncompressed. We always need uncompressed dex for execution,
109 // so this might actually save space by avoiding storing the same data twice.
110 // This defaults to reasonable value based on module and should not be set.
111 // It exists only to support ART tests.
112 Uncompress_dex *bool
Wei Li1e73c652021-12-06 13:35:11 -0800113
Wei Li92cd54b2022-01-12 13:22:55 -0800114 // Exclude kotlinc generate files: *.kotlin_module, *.kotlin_builtins. Defaults to false.
Wei Li1e73c652021-12-06 13:35:11 -0800115 Exclude_kotlinc_generated_files *bool
Ulya Trafimovichc8160582024-07-10 08:32:45 +0100116
117 // Disable dex container (also known as "multi-dex").
118 // This may be necessary as a temporary workaround to mask toolchain bugs (see b/341652226).
119 No_dex_container *bool
Liz Kammera7a64f32020-07-09 15:16:41 -0700120}
121
122type dexer struct {
123 dexProperties DexProperties
124
125 // list of extra proguard flag files
Colin Cross312634e2023-11-21 15:13:56 -0800126 extraProguardFlagsFiles android.Paths
127 proguardDictionary android.OptionalPath
128 proguardConfiguration android.OptionalPath
129 proguardUsageZip android.OptionalPath
130 resourcesInput android.OptionalPath
131 resourcesOutput android.OptionalPath
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500132
Colin Cross9ffaf282024-08-12 13:50:09 -0700133 providesTransitiveHeaderJarsForR8
Liz Kammera7a64f32020-07-09 15:16:41 -0700134}
135
136func (d *dexer) effectiveOptimizeEnabled() bool {
137 return BoolDefault(d.dexProperties.Optimize.Enabled, d.dexProperties.Optimize.EnabledByDefault)
138}
139
Rico Wind936754c2024-05-07 09:08:31 +0200140func (d *DexProperties) resourceShrinkingEnabled(ctx android.ModuleContext) bool {
141 return !ctx.Config().Eng() && BoolDefault(d.Optimize.Optimized_shrink_resources, Bool(d.Optimize.Shrink_resources))
142}
143
144func (d *DexProperties) optimizedResourceShrinkingEnabled(ctx android.ModuleContext) bool {
Rico Winda7b38592024-08-26 12:10:44 +0200145 return d.resourceShrinkingEnabled(ctx) && BoolDefault(d.Optimize.Optimized_shrink_resources, ctx.Config().UseOptimizedResourceShrinkingByDefault())
Rico Winda2fa2632024-03-13 13:09:17 +0100146}
147
Spandan Das15a67112024-05-30 00:07:40 +0000148func (d *dexer) optimizeOrObfuscateEnabled() bool {
149 return d.effectiveOptimizeEnabled() && (proptools.Bool(d.dexProperties.Optimize.Optimize) || proptools.Bool(d.dexProperties.Optimize.Obfuscate))
150}
151
Colin Cross77cdcfd2021-03-12 11:28:25 -0800152var d8, d8RE = pctx.MultiCommandRemoteStaticRules("d8",
Colin Crossf0056cb2017-12-22 15:56:08 -0800153 blueprint.RuleParams{
154 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
David Srbeckybda964c2023-11-24 15:57:54 +0000155 `$d8Template${config.D8Cmd} ${config.D8Flags} $d8Flags --output $outDir --no-dex-input-jar $in && ` +
Kousik Kumar366afc52020-05-20 11:27:16 -0700156 `$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` +
Colin Cross56e28402023-09-28 14:38:06 -0700157 `${config.MergeZipsCmd} -D -stripFile "**/*.class" $mergeZipsFlags $out $outDir/classes.dex.jar $in && ` +
Haamed Gheibie90a6712024-10-04 12:37:21 -0700158 `rm -f "$outDir"/classes*.dex "$outDir/classes.dex.jar"`,
Colin Crossf0056cb2017-12-22 15:56:08 -0800159 CommandDeps: []string{
160 "${config.D8Cmd}",
161 "${config.SoongZipCmd}",
162 "${config.MergeZipsCmd}",
163 },
Kousik Kumar366afc52020-05-20 11:27:16 -0700164 }, map[string]*remoteexec.REParams{
165 "$d8Template": &remoteexec.REParams{
166 Labels: map[string]string{"type": "compile", "compiler": "d8"},
167 Inputs: []string{"${config.D8Jar}"},
168 ExecStrategy: "${config.RED8ExecStrategy}",
169 ToolchainInputs: []string{"${config.JavaCmd}"},
170 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
171 },
172 "$zipTemplate": &remoteexec.REParams{
173 Labels: map[string]string{"type": "tool", "name": "soong_zip"},
174 Inputs: []string{"${config.SoongZipCmd}", "$outDir"},
175 OutputFiles: []string{"$outDir/classes.dex.jar"},
176 ExecStrategy: "${config.RED8ExecStrategy}",
177 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
178 },
Ian Zernydb2d35b2023-10-10 12:22:39 +0200179 }, []string{"outDir", "d8Flags", "zipFlags", "mergeZipsFlags"}, nil)
Colin Crossf0056cb2017-12-22 15:56:08 -0800180
Colin Cross77cdcfd2021-03-12 11:28:25 -0800181var r8, r8RE = pctx.MultiCommandRemoteStaticRules("r8",
Colin Cross66dbc0b2017-12-28 12:23:20 -0800182 blueprint.RuleParams{
183 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Ian Zerny82044f12023-01-25 12:11:27 +0100184 `rm -f "$outDict" && rm -f "$outConfig" && rm -rf "${outUsageDir}" && ` +
Colin Crosscb6143a2020-08-14 17:39:29 -0700185 `mkdir -p $$(dirname ${outUsage}) && ` +
David Srbeckybda964c2023-11-24 15:57:54 +0000186 `$r8Template${config.R8Cmd} ${config.R8Flags} $r8Flags -injars $in --output $outDir ` +
Søren Gjesse24f17022018-09-14 15:20:42 +0200187 `--no-data-resources ` +
Colin Crosscb6143a2020-08-14 17:39:29 -0700188 `-printmapping ${outDict} ` +
Jared Duke34c6d7d2023-05-05 20:40:20 +0000189 `-printconfiguration ${outConfig} ` +
Colin Crosscb6143a2020-08-14 17:39:29 -0700190 `-printusage ${outUsage} ` +
David Srbeckybda964c2023-11-24 15:57:54 +0000191 `--deps-file ${out}.d && ` +
Ian Zerny82044f12023-01-25 12:11:27 +0100192 `touch "${outDict}" "${outConfig}" "${outUsage}" && ` +
Colin Crosscb6143a2020-08-14 17:39:29 -0700193 `${config.SoongZipCmd} -o ${outUsageZip} -C ${outUsageDir} -f ${outUsage} && ` +
194 `rm -rf ${outUsageDir} && ` +
Kousik Kumar366afc52020-05-20 11:27:16 -0700195 `$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` +
Colin Cross56e28402023-09-28 14:38:06 -0700196 `${config.MergeZipsCmd} -D -stripFile "**/*.class" $mergeZipsFlags $out $outDir/classes.dex.jar $in && ` +
Haamed Gheibie90a6712024-10-04 12:37:21 -0700197 `rm -f "$outDir"/classes*.dex "$outDir/classes.dex.jar"`,
Colin Cross22e6a6f2022-03-21 12:19:44 -0700198 Depfile: "${out}.d",
199 Deps: blueprint.DepsGCC,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800200 CommandDeps: []string{
Colin Crossa832a042021-08-05 16:56:18 -0700201 "${config.R8Cmd}",
Colin Cross66dbc0b2017-12-28 12:23:20 -0800202 "${config.SoongZipCmd}",
203 "${config.MergeZipsCmd}",
204 },
Kousik Kumar366afc52020-05-20 11:27:16 -0700205 }, map[string]*remoteexec.REParams{
206 "$r8Template": &remoteexec.REParams{
207 Labels: map[string]string{"type": "compile", "compiler": "r8"},
208 Inputs: []string{"$implicits", "${config.R8Jar}"},
Spandan Dasd447bbb2024-06-24 17:13:30 +0000209 OutputFiles: []string{"${outUsage}", "${outConfig}", "${outDict}", "${resourcesOutput}", "${outR8ArtProfile}"},
Kousik Kumar366afc52020-05-20 11:27:16 -0700210 ExecStrategy: "${config.RER8ExecStrategy}",
211 ToolchainInputs: []string{"${config.JavaCmd}"},
212 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
213 },
214 "$zipTemplate": &remoteexec.REParams{
215 Labels: map[string]string{"type": "tool", "name": "soong_zip"},
216 Inputs: []string{"${config.SoongZipCmd}", "$outDir"},
217 OutputFiles: []string{"$outDir/classes.dex.jar"},
218 ExecStrategy: "${config.RER8ExecStrategy}",
219 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
220 },
Colin Crosscb6143a2020-08-14 17:39:29 -0700221 "$zipUsageTemplate": &remoteexec.REParams{
222 Labels: map[string]string{"type": "tool", "name": "soong_zip"},
223 Inputs: []string{"${config.SoongZipCmd}", "${outUsage}"},
224 OutputFiles: []string{"${outUsageZip}"},
225 ExecStrategy: "${config.RER8ExecStrategy}",
226 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
227 },
Ian Zerny82044f12023-01-25 12:11:27 +0100228 }, []string{"outDir", "outDict", "outConfig", "outUsage", "outUsageZip", "outUsageDir",
Spandan Dasd447bbb2024-06-24 17:13:30 +0000229 "r8Flags", "zipFlags", "mergeZipsFlags", "resourcesOutput", "outR8ArtProfile"}, []string{"implicits"})
Colin Cross66dbc0b2017-12-28 12:23:20 -0800230
Colin Cross5ea963e2021-09-16 19:13:43 -0700231func (d *dexer) dexCommonFlags(ctx android.ModuleContext,
Spandan Dasc404cc72023-02-23 18:05:05 +0000232 dexParams *compileDexParams) (flags []string, deps android.Paths) {
Colin Cross5ea963e2021-09-16 19:13:43 -0700233
234 flags = d.dexProperties.Dxflags
Colin Crossbafb8972018-06-06 21:46:32 +0000235 // Translate all the DX flags to D8 ones until all the build files have been migrated
236 // to D8 flags. See: b/69377755
237 flags = android.RemoveListFromList(flags,
238 []string{"--core-library", "--dex", "--multi-dex"})
Colin Crossf0056cb2017-12-22 15:56:08 -0800239
Colin Cross5ea963e2021-09-16 19:13:43 -0700240 for _, f := range android.PathsForModuleSrc(ctx, d.dexProperties.Main_dex_rules) {
241 flags = append(flags, "--main-dex-rules", f.String())
242 deps = append(deps, f)
243 }
244
Jared Duke70b85422025-01-28 19:13:39 +0000245 var requestReleaseMode, requestDebugMode bool
Jared Duked32e85f2024-09-25 23:54:30 +0000246 requestReleaseMode, flags = android.RemoveFromList("--release", flags)
Jared Duke70b85422025-01-28 19:13:39 +0000247 requestDebugMode, flags = android.RemoveFromList("--debug", flags)
Jared Duked32e85f2024-09-25 23:54:30 +0000248
Abdelrahman Daim1cde8fc2024-10-01 06:55:58 -0700249 if ctx.Config().Getenv("NO_OPTIMIZE_DX") != "" || ctx.Config().Getenv("GENERATE_DEX_DEBUG") != "" {
Jared Duke70b85422025-01-28 19:13:39 +0000250 requestDebugMode = true
Jared Duked32e85f2024-09-25 23:54:30 +0000251 requestReleaseMode = false
Colin Crossf0056cb2017-12-22 15:56:08 -0800252 }
253
Jared Duked32e85f2024-09-25 23:54:30 +0000254 // Don't strip out debug information for eng builds, unless the target
255 // explicitly provided the `--release` build flag. This allows certain
256 // test targets to remain optimized as part of eng test_suites builds.
Jared Duke70b85422025-01-28 19:13:39 +0000257 if requestDebugMode {
258 flags = append(flags, "--debug")
259 } else if requestReleaseMode {
Jared Duked32e85f2024-09-25 23:54:30 +0000260 flags = append(flags, "--release")
261 } else if ctx.Config().Eng() {
262 flags = append(flags, "--debug")
Jared Duke70b85422025-01-28 19:13:39 +0000263 } else if !d.effectiveOptimizeEnabled() && d.dexProperties.Optimize.EnabledByDefault {
264 // D8 uses --debug by default, whereas R8 uses --release by default.
265 // For targets that default to R8 usage (e.g., apps), but override this default, we still
266 // want D8 to run in release mode, preserving semantics as much as possible between the two.
267 flags = append(flags, "--release")
Colin Crossf0056cb2017-12-22 15:56:08 -0800268 }
269
Jared Duke40d731a2022-09-20 15:32:14 -0700270 // Supplying the platform build flag disables various features like API modeling and desugaring.
271 // For targets with a stable min SDK version (i.e., when the min SDK is both explicitly specified
272 // and managed+versioned), we suppress this flag to ensure portability.
273 // Note: Targets with a min SDK kind of core_platform (e.g., framework.jar) or unspecified (e.g.,
274 // services.jar), are not classified as stable, which is WAI.
275 // TODO(b/232073181): Expand to additional min SDK cases after validation.
Ian Zernyc26029b2023-08-25 13:43:44 +0200276 var addAndroidPlatformBuildFlag = false
Spandan Dasc404cc72023-02-23 18:05:05 +0000277 if !dexParams.sdkVersion.Stable() {
Ian Zernyc26029b2023-08-25 13:43:44 +0200278 addAndroidPlatformBuildFlag = true
Jared Duke40d731a2022-09-20 15:32:14 -0700279 }
280
Spandan Dasc404cc72023-02-23 18:05:05 +0000281 effectiveVersion, err := dexParams.minSdkVersion.EffectiveVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -0700282 if err != nil {
283 ctx.PropertyErrorf("min_sdk_version", "%s", err)
284 }
David Srbecky55b46b02024-11-26 17:16:16 +0000285 if !Bool(d.dexProperties.No_dex_container) && effectiveVersion.FinalOrFutureInt() >= 36 && ctx.Config().UseDexV41() {
Ulya Trofimovich5bb46812024-08-07 19:25:58 +0000286 // W is 36, but we have not bumped the SDK version yet, so check for both.
287 if ctx.Config().PlatformSdkVersion().FinalInt() >= 36 ||
David Srbecky55b46b02024-11-26 17:16:16 +0000288 ctx.Config().PlatformSdkCodename() == "Baklava" {
289 flags = append([]string{"-JDcom.android.tools.r8.dexContainerExperiment"}, flags...)
Ulya Trofimovich5bb46812024-08-07 19:25:58 +0000290 }
291 }
Colin Cross83bb3162018-06-25 15:48:06 -0700292
Ian Zernyc26029b2023-08-25 13:43:44 +0200293 // If the specified SDK level is 10000, then configure the compiler to use the
294 // current platform SDK level and to compile the build as a platform build.
295 var minApiFlagValue = effectiveVersion.FinalOrFutureInt()
296 if minApiFlagValue == 10000 {
297 minApiFlagValue = ctx.Config().PlatformSdkVersion().FinalInt()
298 addAndroidPlatformBuildFlag = true
299 }
300 flags = append(flags, "--min-api "+strconv.Itoa(minApiFlagValue))
301
302 if addAndroidPlatformBuildFlag {
303 flags = append(flags, "--android-platform-build")
304 }
Colin Cross5ea963e2021-09-16 19:13:43 -0700305 return flags, deps
Colin Crossf0056cb2017-12-22 15:56:08 -0800306}
307
Spandan Das3dbda182024-05-20 22:23:10 +0000308func (d *dexer) d8Flags(ctx android.ModuleContext, dexParams *compileDexParams) (d8Flags []string, d8Deps android.Paths, artProfileOutput *android.OutputPath) {
309 flags := dexParams.flags
Colin Crossc2557d12019-10-31 15:22:57 -0700310 d8Flags = append(d8Flags, flags.bootClasspath.FormRepeatedClassPath("--lib ")...)
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700311 d8Flags = append(d8Flags, flags.dexClasspath.FormRepeatedClassPath("--lib ")...)
Colin Crossffb657e2018-09-21 12:29:22 -0700312
Colin Cross6dab9bd2018-09-28 08:06:24 -0700313 d8Deps = append(d8Deps, flags.bootClasspath...)
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700314 d8Deps = append(d8Deps, flags.dexClasspath...)
Colin Cross6dab9bd2018-09-28 08:06:24 -0700315
Spandan Das3dbda182024-05-20 22:23:10 +0000316 if flags, deps, profileOutput := d.addArtProfile(ctx, dexParams); profileOutput != nil {
317 d8Flags = append(d8Flags, flags...)
318 d8Deps = append(d8Deps, deps...)
319 artProfileOutput = profileOutput
320 }
321
322 return d8Flags, d8Deps, artProfileOutput
Colin Crossffb657e2018-09-21 12:29:22 -0700323}
324
Jared Duke91d0bee2024-11-14 23:10:09 +0000325func (d *dexer) r8Flags(ctx android.ModuleContext, dexParams *compileDexParams, debugMode bool) (r8Flags []string, r8Deps android.Paths, artProfileOutput *android.OutputPath) {
Spandan Das3dbda182024-05-20 22:23:10 +0000326 flags := dexParams.flags
Liz Kammera7a64f32020-07-09 15:16:41 -0700327 opt := d.dexProperties.Optimize
Colin Cross66dbc0b2017-12-28 12:23:20 -0800328
329 // When an app contains references to APIs that are not in the SDK specified by
330 // its LOCAL_SDK_VERSION for example added by support library or by runtime
Colin Crossbafb8972018-06-06 21:46:32 +0000331 // classes added by desugaring, we artifically raise the "SDK version" "linked" by
Colin Cross66dbc0b2017-12-28 12:23:20 -0800332 // ProGuard, to
333 // - suppress ProGuard warnings of referencing symbols unknown to the lower SDK version.
334 // - prevent ProGuard stripping subclass in the support library that extends class added in the higher SDK version.
335 // See b/20667396
Jared Duke22468442024-08-20 20:45:35 +0000336 // TODO(b/360905238): Remove SdkSystemServer exception after resolving missing class references.
337 if !dexParams.sdkVersion.Stable() || dexParams.sdkVersion.Kind == android.SdkSystemServer {
338 var proguardRaiseDeps classpath
Yu Liuc41eae52025-01-14 01:03:08 +0000339 ctx.VisitDirectDepsProxyWithTag(proguardRaiseTag, func(m android.ModuleProxy) {
Jared Duke22468442024-08-20 20:45:35 +0000340 if dep, ok := android.OtherModuleProvider(ctx, m, JavaInfoProvider); ok {
341 proguardRaiseDeps = append(proguardRaiseDeps, dep.RepackagedHeaderJars...)
342 }
343 })
344 r8Flags = append(r8Flags, proguardRaiseDeps.FormJavaClassPath("-libraryjars"))
345 r8Deps = append(r8Deps, proguardRaiseDeps...)
346 }
Colin Cross66dbc0b2017-12-28 12:23:20 -0800347
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500348 r8Flags = append(r8Flags, flags.bootClasspath.FormJavaClassPath("-libraryjars"))
Colin Cross6dab9bd2018-09-28 08:06:24 -0700349 r8Deps = append(r8Deps, flags.bootClasspath...)
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500350 r8Flags = append(r8Flags, flags.dexClasspath.FormJavaClassPath("-libraryjars"))
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700351 r8Deps = append(r8Deps, flags.dexClasspath...)
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500352
353 transitiveStaticLibsLookupMap := map[android.Path]bool{}
Colin Crossa14fb6a2024-10-23 16:57:06 -0700354 for _, jar := range d.transitiveStaticLibsHeaderJarsForR8.ToList() {
355 transitiveStaticLibsLookupMap[jar] = true
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500356 }
357 transitiveHeaderJars := android.Paths{}
Colin Crossa14fb6a2024-10-23 16:57:06 -0700358 for _, jar := range d.transitiveLibsHeaderJarsForR8.ToList() {
359 if _, ok := transitiveStaticLibsLookupMap[jar]; ok {
360 // don't include a lib if it is already packaged in the current JAR as a static lib
361 continue
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500362 }
Colin Crossa14fb6a2024-10-23 16:57:06 -0700363 transitiveHeaderJars = append(transitiveHeaderJars, jar)
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500364 }
365 transitiveClasspath := classpath(transitiveHeaderJars)
366 r8Flags = append(r8Flags, transitiveClasspath.FormJavaClassPath("-libraryjars"))
367 r8Deps = append(r8Deps, transitiveClasspath...)
Colin Cross6dab9bd2018-09-28 08:06:24 -0700368
Colin Cross66dbc0b2017-12-28 12:23:20 -0800369 flagFiles := android.Paths{
370 android.PathForSource(ctx, "build/make/core/proguard.flags"),
371 }
372
Colin Cross312634e2023-11-21 15:13:56 -0800373 flagFiles = append(flagFiles, d.extraProguardFlagsFiles...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800374 // TODO(ccross): static android library proguard files
375
Liz Kammera7a64f32020-07-09 15:16:41 -0700376 flagFiles = append(flagFiles, android.PathsForModuleSrc(ctx, opt.Proguard_flags_files)...)
Colin Crossbd1cef52018-08-13 10:28:18 -0700377
Sam Delmericoc8e040c2023-10-31 17:27:02 +0000378 flagFiles = android.FirstUniquePaths(flagFiles)
379
Colin Cross66dbc0b2017-12-28 12:23:20 -0800380 r8Flags = append(r8Flags, android.JoinWithPrefix(flagFiles.Strings(), "-include "))
381 r8Deps = append(r8Deps, flagFiles...)
382
383 // TODO(b/70942988): This is included from build/make/core/proguard.flags
384 r8Deps = append(r8Deps, android.PathForSource(ctx,
385 "build/make/core/proguard_basic_keeps.flags"))
386
Liz Kammera7a64f32020-07-09 15:16:41 -0700387 r8Flags = append(r8Flags, opt.Proguard_flags...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800388
Christoffer Adamsenc8fd1f12025-01-06 19:04:03 +0100389 if BoolDefault(opt.Ignore_library_extends_program, false) {
390 r8Flags = append(r8Flags, "--ignore-library-extends-program")
391 }
392
Christoffer Adamsen8ec5f612025-01-09 11:05:11 +0100393 if BoolDefault(opt.Keep_runtime_invisible_annotations, false) {
394 r8Flags = append(r8Flags, "--keep-runtime-invisible-annotations")
395 }
396
Christoffer Adamsenf4f4aa62025-01-14 09:15:11 +0100397 if BoolDefault(opt.Proguard_compatibility, !ctx.Config().UseR8FullModeByDefault()) {
Christoffer Quist Adamsenf2d7b162020-08-24 15:56:16 +0200398 r8Flags = append(r8Flags, "--force-proguard-compatibility")
Jared Dukeb832fbb2023-09-15 17:16:36 +0000399 }
400
Christoffer Adamsen18c1da52025-02-07 03:36:47 -0800401 if BoolDefault(opt.Protect_api_surface, false) {
402 r8Flags = append(r8Flags, "--protect-api-surface")
403 }
404
Jared Duke91d0bee2024-11-14 23:10:09 +0000405 // Avoid unnecessary stack frame noise by only injecting source map ids for non-debug
406 // optimized or obfuscated targets.
407 if (Bool(opt.Optimize) || Bool(opt.Obfuscate)) && !debugMode {
Ian Zernyfc7df612021-11-02 15:37:06 +0100408 // TODO(b/213833843): Allow configuration of the prefix via a build variable.
409 var sourceFilePrefix = "go/retraceme "
410 var sourceFileTemplate = "\"" + sourceFilePrefix + "%MAP_ID\""
Jared Dukeb832fbb2023-09-15 17:16:36 +0000411 r8Flags = append(r8Flags, "--map-id-template", "%MAP_HASH")
412 r8Flags = append(r8Flags, "--source-file-template", sourceFileTemplate)
Christoffer Quist Adamsenf2d7b162020-08-24 15:56:16 +0200413 }
414
Colin Cross66dbc0b2017-12-28 12:23:20 -0800415 // TODO(ccross): Don't shrink app instrumentation tests by default.
416 if !Bool(opt.Shrink) {
417 r8Flags = append(r8Flags, "-dontshrink")
418 }
419
420 if !Bool(opt.Optimize) {
421 r8Flags = append(r8Flags, "-dontoptimize")
422 }
423
424 // TODO(ccross): error if obufscation + app instrumentation test.
425 if !Bool(opt.Obfuscate) {
426 r8Flags = append(r8Flags, "-dontobfuscate")
427 }
Colin Cross4b964c02018-10-15 16:18:06 -0700428 // TODO(ccross): if this is an instrumentation test of an obfuscated app, use the
429 // dictionary of the app and move the app from libraryjars to injars.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800430
Christoffer Quist Adamsene8507372021-02-22 09:44:09 +0100431 // TODO(b/180878971): missing classes should be added to the relevant builds.
Ajinkya Chalkeddad41b2023-02-09 14:09:58 +0000432 // TODO(b/229727645): do not use true as default for Android platform builds.
433 if proptools.BoolDefault(opt.Ignore_warnings, true) {
Remi NGUYEN VANbdad3142022-08-04 13:19:03 +0900434 r8Flags = append(r8Flags, "-ignorewarnings")
435 }
Christoffer Quist Adamsene8507372021-02-22 09:44:09 +0100436
Rico Winda2fa2632024-03-13 13:09:17 +0100437 // resourcesInput is empty when we don't use resource shrinking, if on, pass these to R8
Rico Wind98e7fa82023-11-27 09:44:03 +0100438 if d.resourcesInput.Valid() {
439 r8Flags = append(r8Flags, "--resource-input", d.resourcesInput.Path().String())
440 r8Deps = append(r8Deps, d.resourcesInput.Path())
441 r8Flags = append(r8Flags, "--resource-output", d.resourcesOutput.Path().String())
Rico Winda7b38592024-08-26 12:10:44 +0200442 if d.dexProperties.optimizedResourceShrinkingEnabled(ctx) {
Rico Winda2fa2632024-03-13 13:09:17 +0100443 r8Flags = append(r8Flags, "--optimized-resource-shrinking")
Rico Wind619ed5c2024-10-22 14:00:23 +0200444 if Bool(d.dexProperties.Optimize.Optimized_shrink_resources) {
445 // Explicitly opted into optimized shrinking, no need for keeping R$id entries
446 r8Flags = append(r8Flags, "--force-optimized-resource-shrinking")
447 }
Rico Winda2fa2632024-03-13 13:09:17 +0100448 }
Rico Wind98e7fa82023-11-27 09:44:03 +0100449 }
450
Spandan Das3dbda182024-05-20 22:23:10 +0000451 if flags, deps, profileOutput := d.addArtProfile(ctx, dexParams); profileOutput != nil {
452 r8Flags = append(r8Flags, flags...)
453 r8Deps = append(r8Deps, deps...)
454 artProfileOutput = profileOutput
455 }
456
Christoffer Adamsenb81fc912024-12-17 14:30:47 +0100457 if ctx.Config().UseR8StoreStoreFenceConstructorInlining() {
458 r8Flags = append(r8Flags, "--store-store-fence-constructor-inlining")
459 }
460
Spandan Das3dbda182024-05-20 22:23:10 +0000461 return r8Flags, r8Deps, artProfileOutput
Colin Cross66dbc0b2017-12-28 12:23:20 -0800462}
463
Spandan Dasc404cc72023-02-23 18:05:05 +0000464type compileDexParams struct {
Spandan Das3dbda182024-05-20 22:23:10 +0000465 flags javaBuilderFlags
466 sdkVersion android.SdkSpec
467 minSdkVersion android.ApiLevel
468 classesJar android.Path
469 jarName string
470 artProfileInput *string
Spandan Dasc404cc72023-02-23 18:05:05 +0000471}
472
Spandan Das3dbda182024-05-20 22:23:10 +0000473// Adds --art-profile to r8/d8 command.
474// r8/d8 will output a generated profile file to match the optimized dex code.
475func (d *dexer) addArtProfile(ctx android.ModuleContext, dexParams *compileDexParams) (flags []string, deps android.Paths, artProfileOutputPath *android.OutputPath) {
Cole Faust4e9f5922024-11-13 16:09:23 -0800476 if dexParams.artProfileInput == nil {
477 return nil, nil, nil
Spandan Das3dbda182024-05-20 22:23:10 +0000478 }
Cole Faust4e9f5922024-11-13 16:09:23 -0800479 artProfileInputPath := android.PathForModuleSrc(ctx, *dexParams.artProfileInput)
480 artProfileOutputPathValue := android.PathForModuleOut(ctx, "profile.prof.txt").OutputPath
481 artProfileOutputPath = &artProfileOutputPathValue
482 flags = []string{
483 "--art-profile",
484 artProfileInputPath.String(),
485 artProfileOutputPath.String(),
486 }
487 deps = append(deps, artProfileInputPath)
Spandan Das3dbda182024-05-20 22:23:10 +0000488 return flags, deps, artProfileOutputPath
489
490}
491
492// Return the compiled dex jar and (optional) profile _after_ r8 optimization
Colin Cross7707b242024-07-26 12:02:36 -0700493func (d *dexer) compileDex(ctx android.ModuleContext, dexParams *compileDexParams) (android.Path, android.Path) {
Colin Crossf0056cb2017-12-22 15:56:08 -0800494
Colin Crossf0056cb2017-12-22 15:56:08 -0800495 // Compile classes.jar into classes.dex and then javalib.jar
Spandan Dasc404cc72023-02-23 18:05:05 +0000496 javalibJar := android.PathForModuleOut(ctx, "dex", dexParams.jarName).OutputPath
Colin Crossf0056cb2017-12-22 15:56:08 -0800497 outDir := android.PathForModuleOut(ctx, "dex")
498
Sasha Smundakd3cf4ee2019-02-15 10:14:23 -0800499 zipFlags := "--ignore_missing_files"
Liz Kammera7a64f32020-07-09 15:16:41 -0700500 if proptools.Bool(d.dexProperties.Uncompress_dex) {
Sasha Smundakd3cf4ee2019-02-15 10:14:23 -0800501 zipFlags += " -L 0"
Colin Cross5a0dcd52018-10-05 14:20:06 -0700502 }
503
Spandan Dasc404cc72023-02-23 18:05:05 +0000504 commonFlags, commonDeps := d.dexCommonFlags(ctx, dexParams)
Liz Kammera7a64f32020-07-09 15:16:41 -0700505
Wei Li1e73c652021-12-06 13:35:11 -0800506 // Exclude kotlinc generated files when "exclude_kotlinc_generated_files" is set to true.
507 mergeZipsFlags := ""
508 if proptools.BoolDefault(d.dexProperties.Exclude_kotlinc_generated_files, false) {
509 mergeZipsFlags = "-stripFile META-INF/*.kotlin_module -stripFile **/*.kotlin_builtins"
510 }
511
Liz Kammera7a64f32020-07-09 15:16:41 -0700512 useR8 := d.effectiveOptimizeEnabled()
Spandan Das3dbda182024-05-20 22:23:10 +0000513 var artProfileOutputPath *android.OutputPath
Colin Cross66dbc0b2017-12-28 12:23:20 -0800514 if useR8 {
Colin Crossc0c664c2018-05-16 16:47:21 -0700515 proguardDictionary := android.PathForModuleOut(ctx, "proguard_dictionary")
Liz Kammera7a64f32020-07-09 15:16:41 -0700516 d.proguardDictionary = android.OptionalPathForPath(proguardDictionary)
Ian Zerny82044f12023-01-25 12:11:27 +0100517 proguardConfiguration := android.PathForModuleOut(ctx, "proguard_configuration")
518 d.proguardConfiguration = android.OptionalPathForPath(proguardConfiguration)
Colin Crosscb6143a2020-08-14 17:39:29 -0700519 proguardUsageDir := android.PathForModuleOut(ctx, "proguard_usage")
520 proguardUsage := proguardUsageDir.Join(ctx, ctx.Namespace().Path,
521 android.ModuleNameWithPossibleOverride(ctx), "unused.txt")
522 proguardUsageZip := android.PathForModuleOut(ctx, "proguard_usage.zip")
523 d.proguardUsageZip = android.OptionalPathForPath(proguardUsageZip)
Rico Wind98e7fa82023-11-27 09:44:03 +0100524 resourcesOutput := android.PathForModuleOut(ctx, "package-res-shrunken.apk")
525 d.resourcesOutput = android.OptionalPathForPath(resourcesOutput)
Spandan Das3dbda182024-05-20 22:23:10 +0000526 implicitOutputs := android.WritablePaths{
527 proguardDictionary,
528 proguardUsageZip,
529 proguardConfiguration,
530 }
Jared Duke91d0bee2024-11-14 23:10:09 +0000531 debugMode := android.InList("--debug", commonFlags)
532 r8Flags, r8Deps, r8ArtProfileOutputPath := d.r8Flags(ctx, dexParams, debugMode)
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400533 rule := r8
534 args := map[string]string{
Wei Li1e73c652021-12-06 13:35:11 -0800535 "r8Flags": strings.Join(append(commonFlags, r8Flags...), " "),
536 "zipFlags": zipFlags,
537 "outDict": proguardDictionary.String(),
Ian Zerny82044f12023-01-25 12:11:27 +0100538 "outConfig": proguardConfiguration.String(),
Wei Li1e73c652021-12-06 13:35:11 -0800539 "outUsageDir": proguardUsageDir.String(),
540 "outUsage": proguardUsage.String(),
541 "outUsageZip": proguardUsageZip.String(),
542 "outDir": outDir.String(),
Wei Li1e73c652021-12-06 13:35:11 -0800543 "mergeZipsFlags": mergeZipsFlags,
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400544 }
Spandan Dasd447bbb2024-06-24 17:13:30 +0000545 if r8ArtProfileOutputPath != nil {
546 artProfileOutputPath = r8ArtProfileOutputPath
547 implicitOutputs = append(
548 implicitOutputs,
549 artProfileOutputPath,
550 )
551 // Add the implicit r8 Art profile output to args so that r8RE knows
552 // about this implicit output
553 args["outR8ArtProfile"] = artProfileOutputPath.String()
554 }
555
Ramy Medhat16f23a42020-09-03 01:29:49 -0400556 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_R8") {
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400557 rule = r8RE
558 args["implicits"] = strings.Join(r8Deps.Strings(), ",")
559 }
Rico Wind98e7fa82023-11-27 09:44:03 +0100560 if d.resourcesInput.Valid() {
561 implicitOutputs = append(implicitOutputs, resourcesOutput)
562 args["resourcesOutput"] = resourcesOutput.String()
563 }
Colin Cross66dbc0b2017-12-28 12:23:20 -0800564 ctx.Build(pctx, android.BuildParams{
Rico Wind98e7fa82023-11-27 09:44:03 +0100565 Rule: rule,
566 Description: "r8",
567 Output: javalibJar,
568 ImplicitOutputs: implicitOutputs,
569 Input: dexParams.classesJar,
570 Implicits: r8Deps,
571 Args: args,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800572 })
573 } else {
Spandan Das3dbda182024-05-20 22:23:10 +0000574 implicitOutputs := android.WritablePaths{}
575 d8Flags, d8Deps, d8ArtProfileOutputPath := d.d8Flags(ctx, dexParams)
576 if d8ArtProfileOutputPath != nil {
577 artProfileOutputPath = d8ArtProfileOutputPath
578 implicitOutputs = append(
579 implicitOutputs,
580 artProfileOutputPath,
581 )
582 }
Colin Cross5ea963e2021-09-16 19:13:43 -0700583 d8Deps = append(d8Deps, commonDeps...)
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400584 rule := d8
Ramy Medhat16f23a42020-09-03 01:29:49 -0400585 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_D8") {
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400586 rule = d8RE
587 }
Colin Cross66dbc0b2017-12-28 12:23:20 -0800588 ctx.Build(pctx, android.BuildParams{
Spandan Das3dbda182024-05-20 22:23:10 +0000589 Rule: rule,
590 Description: "d8",
591 Output: javalibJar,
592 Input: dexParams.classesJar,
593 ImplicitOutputs: implicitOutputs,
594 Implicits: d8Deps,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800595 Args: map[string]string{
Wei Li1e73c652021-12-06 13:35:11 -0800596 "d8Flags": strings.Join(append(commonFlags, d8Flags...), " "),
597 "zipFlags": zipFlags,
598 "outDir": outDir.String(),
Wei Li1e73c652021-12-06 13:35:11 -0800599 "mergeZipsFlags": mergeZipsFlags,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800600 },
601 })
Colin Crossf0056cb2017-12-22 15:56:08 -0800602 }
Liz Kammera7a64f32020-07-09 15:16:41 -0700603 if proptools.Bool(d.dexProperties.Uncompress_dex) {
Spandan Dasc404cc72023-02-23 18:05:05 +0000604 alignedJavalibJar := android.PathForModuleOut(ctx, "aligned", dexParams.jarName).OutputPath
Cole Faust51d7bfd2023-09-07 05:31:32 +0000605 TransformZipAlign(ctx, alignedJavalibJar, javalibJar, nil)
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000606 javalibJar = alignedJavalibJar
607 }
Colin Crossf0056cb2017-12-22 15:56:08 -0800608
Spandan Das3dbda182024-05-20 22:23:10 +0000609 return javalibJar, artProfileOutputPath
Colin Crossf0056cb2017-12-22 15:56:08 -0800610}