Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 18 | "strconv" |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 19 | "strings" |
| 20 | |
| 21 | "github.com/google/blueprint" |
David Srbecky | e033cba | 2020-05-20 22:20:28 +0100 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 23 | |
| 24 | "android/soong/android" |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 25 | "android/soong/remoteexec" |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 26 | ) |
| 27 | |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 28 | type 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 Cross | 5ea963e | 2021-09-16 19:13:43 -0700 | [diff] [blame] | 35 | // 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 Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 38 | Optimize struct { |
Jared Duke | 63a3da9 | 2022-06-02 19:11:14 +0000 | [diff] [blame] | 39 | // 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 Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 41 | Enabled *bool |
| 42 | // True if the module containing this has it set by default. |
| 43 | EnabledByDefault bool `blueprint:"mutated"` |
| 44 | |
LaMont Jones | 1abd20d | 2025-03-24 13:07:01 -0700 | [diff] [blame^] | 45 | // If true, then `d8` will be used on eng builds instead of `r8`, even though |
| 46 | // optimize.enabled is true. |
| 47 | D8_on_eng *bool |
| 48 | |
Christoffer Adamsen | c8fd1f1 | 2025-01-06 19:04:03 +0100 | [diff] [blame] | 49 | // Whether to allow that library classes inherit from program classes. |
| 50 | // Defaults to false. |
| 51 | Ignore_library_extends_program *bool |
| 52 | |
Ajinkya Chalke | ddad41b | 2023-02-09 14:09:58 +0000 | [diff] [blame] | 53 | // Whether to continue building even if warnings are emitted. Defaults to true. |
Remi NGUYEN VAN | bdad314 | 2022-08-04 13:19:03 +0900 | [diff] [blame] | 54 | Ignore_warnings *bool |
| 55 | |
Christoffer Adamsen | 8ec5f61 | 2025-01-09 11:05:11 +0100 | [diff] [blame] | 56 | // Whether runtime invisible annotations should be kept by R8. Defaults to false. |
| 57 | // This is equivalent to: |
| 58 | // -keepattributes RuntimeInvisibleAnnotations, |
| 59 | // RuntimeInvisibleParameterAnnotations, |
| 60 | // RuntimeInvisibleTypeAnnotations |
| 61 | // This is only applicable when RELEASE_R8_ONLY_RUNTIME_VISIBLE_ANNOTATIONS is |
| 62 | // enabled and will be used to migrate away from keeping runtime invisible |
| 63 | // annotations (b/387958004). |
| 64 | Keep_runtime_invisible_annotations *bool |
| 65 | |
Jared Duke | aa88b3d | 2023-08-29 17:07:20 +0000 | [diff] [blame] | 66 | // If true, runs R8 in Proguard compatibility mode, otherwise runs R8 in full mode. |
Christoffer Adamsen | c8fd1f1 | 2025-01-06 19:04:03 +0100 | [diff] [blame] | 67 | // Defaults to false for apps and tests, true for libraries. |
Christoffer Quist Adamsen | f2d7b16 | 2020-08-24 15:56:16 +0200 | [diff] [blame] | 68 | Proguard_compatibility *bool |
| 69 | |
Christoffer Adamsen | 18c1da5 | 2025-02-07 03:36:47 -0800 | [diff] [blame] | 70 | // If true, R8 will not add public or protected members (fields or methods) to |
| 71 | // the API surface of the compilation unit, i.e., classes that are kept or |
| 72 | // have kept subclasses will not expose any members added by R8 for internal |
| 73 | // use. That includes renamed members if obfuscation is enabled. |
| 74 | // This should only be used for building targets that go on the bootclasspath. |
| 75 | // Defaults to false. |
| 76 | Protect_api_surface *bool |
| 77 | |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 78 | // If true, optimize for size by removing unused code. Defaults to true for apps, |
| 79 | // false for libraries and tests. |
| 80 | Shrink *bool |
| 81 | |
| 82 | // If true, optimize bytecode. Defaults to false. |
| 83 | Optimize *bool |
| 84 | |
| 85 | // If true, obfuscate bytecode. Defaults to false. |
| 86 | Obfuscate *bool |
| 87 | |
| 88 | // If true, do not use the flag files generated by aapt that automatically keep |
| 89 | // classes referenced by the app manifest. Defaults to false. |
| 90 | No_aapt_flags *bool |
| 91 | |
Jared Duke | 51b0a10 | 2022-09-27 16:53:11 -0700 | [diff] [blame] | 92 | // If true, optimize for size by removing unused resources. Defaults to false. |
Rico Wind | 351bac9 | 2022-09-22 10:41:42 +0200 | [diff] [blame] | 93 | Shrink_resources *bool |
| 94 | |
Rico Wind | a2fa263 | 2024-03-13 13:09:17 +0100 | [diff] [blame] | 95 | // If true, use optimized resource shrinking in R8, overriding the |
| 96 | // Shrink_resources setting. Defaults to false. |
| 97 | // Optimized shrinking means that R8 will trace and treeshake resources together with code |
| 98 | // and apply additional optimizations. This implies non final fields in the R classes. |
| 99 | Optimized_shrink_resources *bool |
| 100 | |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 101 | // Flags to pass to proguard. |
| 102 | Proguard_flags []string |
| 103 | |
| 104 | // Specifies the locations of files containing proguard flags. |
| 105 | Proguard_flags_files []string `android:"path"` |
Sam Delmerico | 95d7094 | 2023-08-02 18:00:35 -0400 | [diff] [blame] | 106 | |
| 107 | // If true, transitive reverse dependencies of this module will have this |
| 108 | // module's proguard spec appended to their optimization action |
| 109 | Export_proguard_flags_files *bool |
Christoffer Adamsen | 914fe68 | 2025-02-27 08:23:49 +0100 | [diff] [blame] | 110 | |
| 111 | // Path to a file containing a list of class names that should not be compiled using R8. |
| 112 | // These classes will be compiled by D8 similar to when Optimize.Enabled is false. |
| 113 | // |
| 114 | // Example: |
| 115 | // |
| 116 | // r8.exclude: |
| 117 | // com.example.Foo |
| 118 | // com.example.Bar |
| 119 | // com.example.Bar$Baz |
| 120 | // |
| 121 | // By default all classes are compiled using R8 when Optimize.Enabled is set. |
| 122 | Exclude *string `android:"path"` |
Jared Duke | 0ac6ae7 | 2025-03-10 23:03:00 +0000 | [diff] [blame] | 123 | |
| 124 | // Optional list of downstream (Java) libraries from which to trace and preserve references |
| 125 | // when optimizing. Note that this requires that the source reference does *not* have |
| 126 | // a strict lib dependency on this target; dependencies should be on intermediate targets |
| 127 | // statically linked into this target, e.g., if A references B, and we want to trace and |
| 128 | // keep references from A when optimizing B, you would create an intermediate B.impl ( |
| 129 | // containing all static code), have A depend on `B.impl` via libs, and set |
| 130 | // `trace_references_from: ["A"]` on B. |
| 131 | // |
| 132 | // Also note that these are *not* inherited across targets, they must be specified at the |
| 133 | // top-level target that is optimized. |
| 134 | // |
| 135 | // TODO(b/212737576): Handle this implicitly using bottom-up deps mutation and implicit |
| 136 | // creation of a proxy `.impl` library. |
| 137 | Trace_references_from proptools.Configurable[[]string] `android:"arch_variant"` |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // Keep the data uncompressed. We always need uncompressed dex for execution, |
| 141 | // so this might actually save space by avoiding storing the same data twice. |
| 142 | // This defaults to reasonable value based on module and should not be set. |
| 143 | // It exists only to support ART tests. |
| 144 | Uncompress_dex *bool |
Wei Li | 1e73c65 | 2021-12-06 13:35:11 -0800 | [diff] [blame] | 145 | |
Wei Li | 92cd54b | 2022-01-12 13:22:55 -0800 | [diff] [blame] | 146 | // Exclude kotlinc generate files: *.kotlin_module, *.kotlin_builtins. Defaults to false. |
Wei Li | 1e73c65 | 2021-12-06 13:35:11 -0800 | [diff] [blame] | 147 | Exclude_kotlinc_generated_files *bool |
Ulya Trafimovich | c816058 | 2024-07-10 08:32:45 +0100 | [diff] [blame] | 148 | |
| 149 | // Disable dex container (also known as "multi-dex"). |
| 150 | // This may be necessary as a temporary workaround to mask toolchain bugs (see b/341652226). |
| 151 | No_dex_container *bool |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | type dexer struct { |
| 155 | dexProperties DexProperties |
| 156 | |
| 157 | // list of extra proguard flag files |
Colin Cross | 312634e | 2023-11-21 15:13:56 -0800 | [diff] [blame] | 158 | extraProguardFlagsFiles android.Paths |
| 159 | proguardDictionary android.OptionalPath |
| 160 | proguardConfiguration android.OptionalPath |
| 161 | proguardUsageZip android.OptionalPath |
| 162 | resourcesInput android.OptionalPath |
| 163 | resourcesOutput android.OptionalPath |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 164 | |
Colin Cross | 9ffaf28 | 2024-08-12 13:50:09 -0700 | [diff] [blame] | 165 | providesTransitiveHeaderJarsForR8 |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 166 | } |
| 167 | |
LaMont Jones | b97f1d1 | 2025-03-18 15:18:58 -0700 | [diff] [blame] | 168 | func (d *dexer) effectiveOptimizeEnabled(ctx android.EarlyModuleContext) bool { |
LaMont Jones | 1abd20d | 2025-03-24 13:07:01 -0700 | [diff] [blame^] | 169 | // For eng builds, if Optimize.D8_on_eng is true, then disable optimization. |
| 170 | if ctx.Config().Eng() && proptools.Bool(d.dexProperties.Optimize.D8_on_eng) { |
| 171 | return false |
| 172 | } |
| 173 | // Otherwise, use the legacy logic of a default value which can be explicitly overridden by the module. |
| 174 | return BoolDefault(d.dexProperties.Optimize.Enabled, d.dexProperties.Optimize.EnabledByDefault) |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Rico Wind | 936754c | 2024-05-07 09:08:31 +0200 | [diff] [blame] | 177 | func (d *DexProperties) resourceShrinkingEnabled(ctx android.ModuleContext) bool { |
| 178 | return !ctx.Config().Eng() && BoolDefault(d.Optimize.Optimized_shrink_resources, Bool(d.Optimize.Shrink_resources)) |
| 179 | } |
| 180 | |
| 181 | func (d *DexProperties) optimizedResourceShrinkingEnabled(ctx android.ModuleContext) bool { |
Rico Wind | a7b3859 | 2024-08-26 12:10:44 +0200 | [diff] [blame] | 182 | return d.resourceShrinkingEnabled(ctx) && BoolDefault(d.Optimize.Optimized_shrink_resources, ctx.Config().UseOptimizedResourceShrinkingByDefault()) |
Rico Wind | a2fa263 | 2024-03-13 13:09:17 +0100 | [diff] [blame] | 183 | } |
| 184 | |
LaMont Jones | b97f1d1 | 2025-03-18 15:18:58 -0700 | [diff] [blame] | 185 | func (d *dexer) optimizeOrObfuscateEnabled(ctx android.EarlyModuleContext) bool { |
| 186 | return d.effectiveOptimizeEnabled(ctx) && (proptools.Bool(d.dexProperties.Optimize.Optimize) || proptools.Bool(d.dexProperties.Optimize.Obfuscate)) |
Spandan Das | 15a6711 | 2024-05-30 00:07:40 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Colin Cross | 77cdcfd | 2021-03-12 11:28:25 -0800 | [diff] [blame] | 189 | var d8, d8RE = pctx.MultiCommandRemoteStaticRules("d8", |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 190 | blueprint.RuleParams{ |
| 191 | Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` + |
David Srbecky | bda964c | 2023-11-24 15:57:54 +0000 | [diff] [blame] | 192 | `$d8Template${config.D8Cmd} ${config.D8Flags} $d8Flags --output $outDir --no-dex-input-jar $in && ` + |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 193 | `$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` + |
Colin Cross | 56e2840 | 2023-09-28 14:38:06 -0700 | [diff] [blame] | 194 | `${config.MergeZipsCmd} -D -stripFile "**/*.class" $mergeZipsFlags $out $outDir/classes.dex.jar $in && ` + |
Haamed Gheibi | e90a671 | 2024-10-04 12:37:21 -0700 | [diff] [blame] | 195 | `rm -f "$outDir"/classes*.dex "$outDir/classes.dex.jar"`, |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 196 | CommandDeps: []string{ |
| 197 | "${config.D8Cmd}", |
| 198 | "${config.SoongZipCmd}", |
| 199 | "${config.MergeZipsCmd}", |
| 200 | }, |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 201 | }, map[string]*remoteexec.REParams{ |
| 202 | "$d8Template": &remoteexec.REParams{ |
| 203 | Labels: map[string]string{"type": "compile", "compiler": "d8"}, |
| 204 | Inputs: []string{"${config.D8Jar}"}, |
| 205 | ExecStrategy: "${config.RED8ExecStrategy}", |
| 206 | ToolchainInputs: []string{"${config.JavaCmd}"}, |
| 207 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 208 | }, |
| 209 | "$zipTemplate": &remoteexec.REParams{ |
| 210 | Labels: map[string]string{"type": "tool", "name": "soong_zip"}, |
| 211 | Inputs: []string{"${config.SoongZipCmd}", "$outDir"}, |
| 212 | OutputFiles: []string{"$outDir/classes.dex.jar"}, |
| 213 | ExecStrategy: "${config.RED8ExecStrategy}", |
| 214 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 215 | }, |
Ian Zerny | db2d35b | 2023-10-10 12:22:39 +0200 | [diff] [blame] | 216 | }, []string{"outDir", "d8Flags", "zipFlags", "mergeZipsFlags"}, nil) |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 217 | |
LaMont Jones | ed6f2b2 | 2025-02-10 16:24:40 -0800 | [diff] [blame] | 218 | // Include all of the args for d8r8, so that we can generate the partialcompileclean target's build using the same list. |
| 219 | var d8r8Clean = pctx.AndroidStaticRule("d8r8-partialcompileclean", |
| 220 | blueprint.RuleParams{ |
| 221 | Command: `rm -rf "${outDir}" "${outDict}" "${outConfig}" "${outUsage}" "${outUsageZip}" "${outUsageDir}" ` + |
| 222 | `"${resourcesOutput}" "${outR8ArtProfile}" ${builtOut}`, |
| 223 | }, "outDir", "outDict", "outConfig", "outUsage", "outUsageZip", "outUsageDir", "builtOut", |
| 224 | "d8Flags", "r8Flags", "zipFlags", "mergeZipsFlags", "resourcesOutput", "outR8ArtProfile", "implicits", |
| 225 | ) |
| 226 | |
| 227 | var d8r8, d8r8RE = pctx.MultiCommandRemoteStaticRules("d8r8", |
| 228 | blueprint.RuleParams{ |
| 229 | Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` + |
| 230 | `rm -f "$outDict" && rm -f "$outConfig" && rm -rf "${outUsageDir}" && ` + |
| 231 | `mkdir -p $$(dirname ${outUsage}) && ` + |
| 232 | `if [ -n "$${SOONG_USE_PARTIAL_COMPILE}" ]; then ` + |
| 233 | ` for f in "${outConfig}" "${outDict}" "${outUsage}" "${resourcesOutput}"; do ` + |
| 234 | ` test -n "$${f}" && test ! -f "$${f}" && mkdir -p "$$(dirname "$${f}")" && touch "$${f}" || true; ` + |
| 235 | ` done && ` + |
| 236 | ` $d8Template${config.D8Cmd} ${config.D8Flags} $d8Flags --output $outDir --no-dex-input-jar $in; ` + |
| 237 | `else ` + |
| 238 | ` $r8Template${config.R8Cmd} ${config.R8Flags} $r8Flags -injars $in --output $outDir ` + |
| 239 | ` --no-data-resources ` + |
| 240 | ` -printmapping ${outDict} ` + |
| 241 | ` -printconfiguration ${outConfig} ` + |
| 242 | ` -printusage ${outUsage} ` + |
| 243 | ` --deps-file ${out}.d && ` + |
| 244 | ` touch "${outDict}" "${outConfig}" "${outUsage}"; ` + |
| 245 | `fi && ` + |
| 246 | `${config.SoongZipCmd} -o ${outUsageZip} -C ${outUsageDir} -f ${outUsage} && ` + |
| 247 | `rm -rf ${outUsageDir} && ` + |
| 248 | `$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` + |
| 249 | `${config.MergeZipsCmd} -D -stripFile "**/*.class" $mergeZipsFlags $out $outDir/classes.dex.jar $in && ` + |
| 250 | `rm -f "$outDir"/classes*.dex "$outDir/classes.dex.jar" `, |
| 251 | CommandDeps: []string{ |
| 252 | "${config.D8Cmd}", |
| 253 | "${config.R8Cmd}", |
| 254 | "${config.SoongZipCmd}", |
| 255 | "${config.MergeZipsCmd}", |
| 256 | }, |
| 257 | }, map[string]*remoteexec.REParams{ |
| 258 | "$d8Template": &remoteexec.REParams{ |
| 259 | Labels: map[string]string{"type": "compile", "compiler": "d8"}, |
| 260 | Inputs: []string{"${config.D8Jar}"}, |
| 261 | ExecStrategy: "${config.RED8ExecStrategy}", |
| 262 | ToolchainInputs: []string{"${config.JavaCmd}"}, |
| 263 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 264 | }, |
| 265 | "$r8Template": &remoteexec.REParams{ |
| 266 | Labels: map[string]string{"type": "compile", "compiler": "r8"}, |
| 267 | Inputs: []string{"$implicits", "${config.R8Jar}"}, |
| 268 | OutputFiles: []string{"${outUsage}", "${outConfig}", "${outDict}", "${resourcesOutput}", "${outR8ArtProfile}"}, |
| 269 | ExecStrategy: "${config.RER8ExecStrategy}", |
| 270 | ToolchainInputs: []string{"${config.JavaCmd}"}, |
| 271 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 272 | }, |
| 273 | "$zipTemplate": &remoteexec.REParams{ |
| 274 | Labels: map[string]string{"type": "tool", "name": "soong_zip"}, |
| 275 | Inputs: []string{"${config.SoongZipCmd}", "$outDir"}, |
| 276 | OutputFiles: []string{"$outDir/classes.dex.jar"}, |
| 277 | ExecStrategy: "${config.RED8ExecStrategy}", |
| 278 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 279 | }, |
| 280 | }, []string{"outDir", "outDict", "outConfig", "outUsage", "outUsageZip", "outUsageDir", |
| 281 | "d8Flags", "r8Flags", "zipFlags", "mergeZipsFlags", "resourcesOutput", "outR8ArtProfile"}, []string{"implicits"}) |
| 282 | |
Colin Cross | 77cdcfd | 2021-03-12 11:28:25 -0800 | [diff] [blame] | 283 | var r8, r8RE = pctx.MultiCommandRemoteStaticRules("r8", |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 284 | blueprint.RuleParams{ |
| 285 | Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` + |
Ian Zerny | 82044f1 | 2023-01-25 12:11:27 +0100 | [diff] [blame] | 286 | `rm -f "$outDict" && rm -f "$outConfig" && rm -rf "${outUsageDir}" && ` + |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 287 | `mkdir -p $$(dirname ${outUsage}) && ` + |
David Srbecky | bda964c | 2023-11-24 15:57:54 +0000 | [diff] [blame] | 288 | `$r8Template${config.R8Cmd} ${config.R8Flags} $r8Flags -injars $in --output $outDir ` + |
Søren Gjesse | 24f1702 | 2018-09-14 15:20:42 +0200 | [diff] [blame] | 289 | `--no-data-resources ` + |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 290 | `-printmapping ${outDict} ` + |
Jared Duke | 34c6d7d | 2023-05-05 20:40:20 +0000 | [diff] [blame] | 291 | `-printconfiguration ${outConfig} ` + |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 292 | `-printusage ${outUsage} ` + |
David Srbecky | bda964c | 2023-11-24 15:57:54 +0000 | [diff] [blame] | 293 | `--deps-file ${out}.d && ` + |
Ian Zerny | 82044f1 | 2023-01-25 12:11:27 +0100 | [diff] [blame] | 294 | `touch "${outDict}" "${outConfig}" "${outUsage}" && ` + |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 295 | `${config.SoongZipCmd} -o ${outUsageZip} -C ${outUsageDir} -f ${outUsage} && ` + |
| 296 | `rm -rf ${outUsageDir} && ` + |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 297 | `$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` + |
Colin Cross | 56e2840 | 2023-09-28 14:38:06 -0700 | [diff] [blame] | 298 | `${config.MergeZipsCmd} -D -stripFile "**/*.class" $mergeZipsFlags $out $outDir/classes.dex.jar $in && ` + |
Haamed Gheibi | e90a671 | 2024-10-04 12:37:21 -0700 | [diff] [blame] | 299 | `rm -f "$outDir"/classes*.dex "$outDir/classes.dex.jar"`, |
Colin Cross | 22e6a6f | 2022-03-21 12:19:44 -0700 | [diff] [blame] | 300 | Depfile: "${out}.d", |
| 301 | Deps: blueprint.DepsGCC, |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 302 | CommandDeps: []string{ |
Colin Cross | a832a04 | 2021-08-05 16:56:18 -0700 | [diff] [blame] | 303 | "${config.R8Cmd}", |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 304 | "${config.SoongZipCmd}", |
| 305 | "${config.MergeZipsCmd}", |
| 306 | }, |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 307 | }, map[string]*remoteexec.REParams{ |
| 308 | "$r8Template": &remoteexec.REParams{ |
| 309 | Labels: map[string]string{"type": "compile", "compiler": "r8"}, |
| 310 | Inputs: []string{"$implicits", "${config.R8Jar}"}, |
Spandan Das | d447bbb | 2024-06-24 17:13:30 +0000 | [diff] [blame] | 311 | OutputFiles: []string{"${outUsage}", "${outConfig}", "${outDict}", "${resourcesOutput}", "${outR8ArtProfile}"}, |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 312 | ExecStrategy: "${config.RER8ExecStrategy}", |
| 313 | ToolchainInputs: []string{"${config.JavaCmd}"}, |
| 314 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 315 | }, |
| 316 | "$zipTemplate": &remoteexec.REParams{ |
| 317 | Labels: map[string]string{"type": "tool", "name": "soong_zip"}, |
| 318 | Inputs: []string{"${config.SoongZipCmd}", "$outDir"}, |
| 319 | OutputFiles: []string{"$outDir/classes.dex.jar"}, |
| 320 | ExecStrategy: "${config.RER8ExecStrategy}", |
| 321 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 322 | }, |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 323 | "$zipUsageTemplate": &remoteexec.REParams{ |
| 324 | Labels: map[string]string{"type": "tool", "name": "soong_zip"}, |
| 325 | Inputs: []string{"${config.SoongZipCmd}", "${outUsage}"}, |
| 326 | OutputFiles: []string{"${outUsageZip}"}, |
| 327 | ExecStrategy: "${config.RER8ExecStrategy}", |
| 328 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 329 | }, |
Ian Zerny | 82044f1 | 2023-01-25 12:11:27 +0100 | [diff] [blame] | 330 | }, []string{"outDir", "outDict", "outConfig", "outUsage", "outUsageZip", "outUsageDir", |
Spandan Das | d447bbb | 2024-06-24 17:13:30 +0000 | [diff] [blame] | 331 | "r8Flags", "zipFlags", "mergeZipsFlags", "resourcesOutput", "outR8ArtProfile"}, []string{"implicits"}) |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 332 | |
Colin Cross | 5ea963e | 2021-09-16 19:13:43 -0700 | [diff] [blame] | 333 | func (d *dexer) dexCommonFlags(ctx android.ModuleContext, |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame] | 334 | dexParams *compileDexParams) (flags []string, deps android.Paths) { |
Colin Cross | 5ea963e | 2021-09-16 19:13:43 -0700 | [diff] [blame] | 335 | |
| 336 | flags = d.dexProperties.Dxflags |
Colin Cross | bafb897 | 2018-06-06 21:46:32 +0000 | [diff] [blame] | 337 | // Translate all the DX flags to D8 ones until all the build files have been migrated |
| 338 | // to D8 flags. See: b/69377755 |
| 339 | flags = android.RemoveListFromList(flags, |
| 340 | []string{"--core-library", "--dex", "--multi-dex"}) |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 341 | |
Colin Cross | 5ea963e | 2021-09-16 19:13:43 -0700 | [diff] [blame] | 342 | for _, f := range android.PathsForModuleSrc(ctx, d.dexProperties.Main_dex_rules) { |
| 343 | flags = append(flags, "--main-dex-rules", f.String()) |
| 344 | deps = append(deps, f) |
| 345 | } |
| 346 | |
Jared Duke | 70b8542 | 2025-01-28 19:13:39 +0000 | [diff] [blame] | 347 | var requestReleaseMode, requestDebugMode bool |
Jared Duke | d32e85f | 2024-09-25 23:54:30 +0000 | [diff] [blame] | 348 | requestReleaseMode, flags = android.RemoveFromList("--release", flags) |
Jared Duke | 70b8542 | 2025-01-28 19:13:39 +0000 | [diff] [blame] | 349 | requestDebugMode, flags = android.RemoveFromList("--debug", flags) |
Jared Duke | d32e85f | 2024-09-25 23:54:30 +0000 | [diff] [blame] | 350 | |
Abdelrahman Daim | 1cde8fc | 2024-10-01 06:55:58 -0700 | [diff] [blame] | 351 | if ctx.Config().Getenv("NO_OPTIMIZE_DX") != "" || ctx.Config().Getenv("GENERATE_DEX_DEBUG") != "" { |
Jared Duke | 70b8542 | 2025-01-28 19:13:39 +0000 | [diff] [blame] | 352 | requestDebugMode = true |
Jared Duke | d32e85f | 2024-09-25 23:54:30 +0000 | [diff] [blame] | 353 | requestReleaseMode = false |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 354 | } |
| 355 | |
Jared Duke | d32e85f | 2024-09-25 23:54:30 +0000 | [diff] [blame] | 356 | // Don't strip out debug information for eng builds, unless the target |
| 357 | // explicitly provided the `--release` build flag. This allows certain |
| 358 | // test targets to remain optimized as part of eng test_suites builds. |
Jared Duke | 70b8542 | 2025-01-28 19:13:39 +0000 | [diff] [blame] | 359 | if requestDebugMode { |
| 360 | flags = append(flags, "--debug") |
| 361 | } else if requestReleaseMode { |
Jared Duke | d32e85f | 2024-09-25 23:54:30 +0000 | [diff] [blame] | 362 | flags = append(flags, "--release") |
| 363 | } else if ctx.Config().Eng() { |
| 364 | flags = append(flags, "--debug") |
LaMont Jones | b97f1d1 | 2025-03-18 15:18:58 -0700 | [diff] [blame] | 365 | } else if !d.effectiveOptimizeEnabled(ctx) && d.dexProperties.Optimize.EnabledByDefault { |
Jared Duke | 70b8542 | 2025-01-28 19:13:39 +0000 | [diff] [blame] | 366 | // D8 uses --debug by default, whereas R8 uses --release by default. |
| 367 | // For targets that default to R8 usage (e.g., apps), but override this default, we still |
| 368 | // want D8 to run in release mode, preserving semantics as much as possible between the two. |
| 369 | flags = append(flags, "--release") |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Jared Duke | 40d731a | 2022-09-20 15:32:14 -0700 | [diff] [blame] | 372 | // Supplying the platform build flag disables various features like API modeling and desugaring. |
| 373 | // For targets with a stable min SDK version (i.e., when the min SDK is both explicitly specified |
| 374 | // and managed+versioned), we suppress this flag to ensure portability. |
| 375 | // Note: Targets with a min SDK kind of core_platform (e.g., framework.jar) or unspecified (e.g., |
| 376 | // services.jar), are not classified as stable, which is WAI. |
| 377 | // TODO(b/232073181): Expand to additional min SDK cases after validation. |
Ian Zerny | c26029b | 2023-08-25 13:43:44 +0200 | [diff] [blame] | 378 | var addAndroidPlatformBuildFlag = false |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame] | 379 | if !dexParams.sdkVersion.Stable() { |
Ian Zerny | c26029b | 2023-08-25 13:43:44 +0200 | [diff] [blame] | 380 | addAndroidPlatformBuildFlag = true |
Jared Duke | 40d731a | 2022-09-20 15:32:14 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame] | 383 | effectiveVersion, err := dexParams.minSdkVersion.EffectiveVersion(ctx) |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 384 | if err != nil { |
| 385 | ctx.PropertyErrorf("min_sdk_version", "%s", err) |
| 386 | } |
David Srbecky | 55b46b0 | 2024-11-26 17:16:16 +0000 | [diff] [blame] | 387 | if !Bool(d.dexProperties.No_dex_container) && effectiveVersion.FinalOrFutureInt() >= 36 && ctx.Config().UseDexV41() { |
Ulya Trofimovich | 5bb4681 | 2024-08-07 19:25:58 +0000 | [diff] [blame] | 388 | // W is 36, but we have not bumped the SDK version yet, so check for both. |
| 389 | if ctx.Config().PlatformSdkVersion().FinalInt() >= 36 || |
David Srbecky | 55b46b0 | 2024-11-26 17:16:16 +0000 | [diff] [blame] | 390 | ctx.Config().PlatformSdkCodename() == "Baklava" { |
| 391 | flags = append([]string{"-JDcom.android.tools.r8.dexContainerExperiment"}, flags...) |
Ulya Trofimovich | 5bb4681 | 2024-08-07 19:25:58 +0000 | [diff] [blame] | 392 | } |
| 393 | } |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 394 | |
Ian Zerny | c26029b | 2023-08-25 13:43:44 +0200 | [diff] [blame] | 395 | // If the specified SDK level is 10000, then configure the compiler to use the |
| 396 | // current platform SDK level and to compile the build as a platform build. |
| 397 | var minApiFlagValue = effectiveVersion.FinalOrFutureInt() |
| 398 | if minApiFlagValue == 10000 { |
| 399 | minApiFlagValue = ctx.Config().PlatformSdkVersion().FinalInt() |
| 400 | addAndroidPlatformBuildFlag = true |
| 401 | } |
| 402 | flags = append(flags, "--min-api "+strconv.Itoa(minApiFlagValue)) |
| 403 | |
| 404 | if addAndroidPlatformBuildFlag { |
| 405 | flags = append(flags, "--android-platform-build") |
| 406 | } |
Colin Cross | 5ea963e | 2021-09-16 19:13:43 -0700 | [diff] [blame] | 407 | return flags, deps |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 408 | } |
| 409 | |
Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 410 | func (d *dexer) d8Flags(ctx android.ModuleContext, dexParams *compileDexParams) (d8Flags []string, d8Deps android.Paths, artProfileOutput *android.OutputPath) { |
| 411 | flags := dexParams.flags |
Colin Cross | c2557d1 | 2019-10-31 15:22:57 -0700 | [diff] [blame] | 412 | d8Flags = append(d8Flags, flags.bootClasspath.FormRepeatedClassPath("--lib ")...) |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 413 | d8Flags = append(d8Flags, flags.dexClasspath.FormRepeatedClassPath("--lib ")...) |
Colin Cross | ffb657e | 2018-09-21 12:29:22 -0700 | [diff] [blame] | 414 | |
Colin Cross | 6dab9bd | 2018-09-28 08:06:24 -0700 | [diff] [blame] | 415 | d8Deps = append(d8Deps, flags.bootClasspath...) |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 416 | d8Deps = append(d8Deps, flags.dexClasspath...) |
Colin Cross | 6dab9bd | 2018-09-28 08:06:24 -0700 | [diff] [blame] | 417 | |
Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 418 | if flags, deps, profileOutput := d.addArtProfile(ctx, dexParams); profileOutput != nil { |
| 419 | d8Flags = append(d8Flags, flags...) |
| 420 | d8Deps = append(d8Deps, deps...) |
| 421 | artProfileOutput = profileOutput |
| 422 | } |
| 423 | |
| 424 | return d8Flags, d8Deps, artProfileOutput |
Colin Cross | ffb657e | 2018-09-21 12:29:22 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Jared Duke | 91d0bee | 2024-11-14 23:10:09 +0000 | [diff] [blame] | 427 | func (d *dexer) r8Flags(ctx android.ModuleContext, dexParams *compileDexParams, debugMode bool) (r8Flags []string, r8Deps android.Paths, artProfileOutput *android.OutputPath) { |
Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 428 | flags := dexParams.flags |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 429 | opt := d.dexProperties.Optimize |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 430 | |
| 431 | // When an app contains references to APIs that are not in the SDK specified by |
| 432 | // its LOCAL_SDK_VERSION for example added by support library or by runtime |
Colin Cross | bafb897 | 2018-06-06 21:46:32 +0000 | [diff] [blame] | 433 | // classes added by desugaring, we artifically raise the "SDK version" "linked" by |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 434 | // ProGuard, to |
| 435 | // - suppress ProGuard warnings of referencing symbols unknown to the lower SDK version. |
| 436 | // - prevent ProGuard stripping subclass in the support library that extends class added in the higher SDK version. |
| 437 | // See b/20667396 |
Jared Duke | 2246844 | 2024-08-20 20:45:35 +0000 | [diff] [blame] | 438 | // TODO(b/360905238): Remove SdkSystemServer exception after resolving missing class references. |
| 439 | if !dexParams.sdkVersion.Stable() || dexParams.sdkVersion.Kind == android.SdkSystemServer { |
| 440 | var proguardRaiseDeps classpath |
Yu Liu | c41eae5 | 2025-01-14 01:03:08 +0000 | [diff] [blame] | 441 | ctx.VisitDirectDepsProxyWithTag(proguardRaiseTag, func(m android.ModuleProxy) { |
Jared Duke | 2246844 | 2024-08-20 20:45:35 +0000 | [diff] [blame] | 442 | if dep, ok := android.OtherModuleProvider(ctx, m, JavaInfoProvider); ok { |
| 443 | proguardRaiseDeps = append(proguardRaiseDeps, dep.RepackagedHeaderJars...) |
| 444 | } |
| 445 | }) |
| 446 | r8Flags = append(r8Flags, proguardRaiseDeps.FormJavaClassPath("-libraryjars")) |
| 447 | r8Deps = append(r8Deps, proguardRaiseDeps...) |
| 448 | } |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 449 | |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 450 | r8Flags = append(r8Flags, flags.bootClasspath.FormJavaClassPath("-libraryjars")) |
Colin Cross | 6dab9bd | 2018-09-28 08:06:24 -0700 | [diff] [blame] | 451 | r8Deps = append(r8Deps, flags.bootClasspath...) |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 452 | r8Flags = append(r8Flags, flags.dexClasspath.FormJavaClassPath("-libraryjars")) |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 453 | r8Deps = append(r8Deps, flags.dexClasspath...) |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 454 | |
| 455 | transitiveStaticLibsLookupMap := map[android.Path]bool{} |
Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 456 | for _, jar := range d.transitiveStaticLibsHeaderJarsForR8.ToList() { |
| 457 | transitiveStaticLibsLookupMap[jar] = true |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 458 | } |
| 459 | transitiveHeaderJars := android.Paths{} |
Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 460 | for _, jar := range d.transitiveLibsHeaderJarsForR8.ToList() { |
| 461 | if _, ok := transitiveStaticLibsLookupMap[jar]; ok { |
| 462 | // don't include a lib if it is already packaged in the current JAR as a static lib |
| 463 | continue |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 464 | } |
Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 465 | transitiveHeaderJars = append(transitiveHeaderJars, jar) |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 466 | } |
| 467 | transitiveClasspath := classpath(transitiveHeaderJars) |
| 468 | r8Flags = append(r8Flags, transitiveClasspath.FormJavaClassPath("-libraryjars")) |
| 469 | r8Deps = append(r8Deps, transitiveClasspath...) |
Colin Cross | 6dab9bd | 2018-09-28 08:06:24 -0700 | [diff] [blame] | 470 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 471 | flagFiles := android.Paths{ |
| 472 | android.PathForSource(ctx, "build/make/core/proguard.flags"), |
| 473 | } |
| 474 | |
Jared Duke | b6f8f4f | 2025-02-18 22:35:45 +0000 | [diff] [blame] | 475 | if ctx.Config().UseR8GlobalCheckNotNullFlags() { |
| 476 | flagFiles = append(flagFiles, android.PathForSource(ctx, |
| 477 | "build/make/core/proguard/checknotnull.flags")) |
| 478 | } |
| 479 | |
Colin Cross | 312634e | 2023-11-21 15:13:56 -0800 | [diff] [blame] | 480 | flagFiles = append(flagFiles, d.extraProguardFlagsFiles...) |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 481 | // TODO(ccross): static android library proguard files |
| 482 | |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 483 | flagFiles = append(flagFiles, android.PathsForModuleSrc(ctx, opt.Proguard_flags_files)...) |
Colin Cross | bd1cef5 | 2018-08-13 10:28:18 -0700 | [diff] [blame] | 484 | |
Jared Duke | 0ac6ae7 | 2025-03-10 23:03:00 +0000 | [diff] [blame] | 485 | traceReferencesSources := android.Paths{} |
| 486 | ctx.VisitDirectDepsProxyWithTag(traceReferencesTag, func(m android.ModuleProxy) { |
| 487 | if dep, ok := android.OtherModuleProvider(ctx, m, JavaInfoProvider); ok { |
| 488 | traceReferencesSources = append(traceReferencesSources, dep.ImplementationJars...) |
| 489 | } |
| 490 | }) |
| 491 | if len(traceReferencesSources) > 0 { |
| 492 | traceTarget := dexParams.classesJar |
| 493 | traceLibs := android.FirstUniquePaths(append(flags.bootClasspath.Paths(), flags.dexClasspath.Paths()...)) |
| 494 | traceReferencesFlags := android.PathForModuleOut(ctx, "proguard", "trace_references.flags") |
| 495 | TraceReferences(ctx, traceReferencesSources, traceTarget, traceLibs, traceReferencesFlags) |
| 496 | flagFiles = append(flagFiles, traceReferencesFlags) |
| 497 | } |
| 498 | |
Sam Delmerico | c8e040c | 2023-10-31 17:27:02 +0000 | [diff] [blame] | 499 | flagFiles = android.FirstUniquePaths(flagFiles) |
| 500 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 501 | r8Flags = append(r8Flags, android.JoinWithPrefix(flagFiles.Strings(), "-include ")) |
| 502 | r8Deps = append(r8Deps, flagFiles...) |
| 503 | |
| 504 | // TODO(b/70942988): This is included from build/make/core/proguard.flags |
| 505 | r8Deps = append(r8Deps, android.PathForSource(ctx, |
| 506 | "build/make/core/proguard_basic_keeps.flags")) |
| 507 | |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 508 | r8Flags = append(r8Flags, opt.Proguard_flags...) |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 509 | |
Christoffer Adamsen | c8fd1f1 | 2025-01-06 19:04:03 +0100 | [diff] [blame] | 510 | if BoolDefault(opt.Ignore_library_extends_program, false) { |
| 511 | r8Flags = append(r8Flags, "--ignore-library-extends-program") |
| 512 | } |
| 513 | |
Christoffer Adamsen | 8ec5f61 | 2025-01-09 11:05:11 +0100 | [diff] [blame] | 514 | if BoolDefault(opt.Keep_runtime_invisible_annotations, false) { |
| 515 | r8Flags = append(r8Flags, "--keep-runtime-invisible-annotations") |
| 516 | } |
| 517 | |
Christoffer Adamsen | f4f4aa6 | 2025-01-14 09:15:11 +0100 | [diff] [blame] | 518 | if BoolDefault(opt.Proguard_compatibility, !ctx.Config().UseR8FullModeByDefault()) { |
Christoffer Quist Adamsen | f2d7b16 | 2020-08-24 15:56:16 +0200 | [diff] [blame] | 519 | r8Flags = append(r8Flags, "--force-proguard-compatibility") |
Jared Duke | b832fbb | 2023-09-15 17:16:36 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Christoffer Adamsen | 18c1da5 | 2025-02-07 03:36:47 -0800 | [diff] [blame] | 522 | if BoolDefault(opt.Protect_api_surface, false) { |
| 523 | r8Flags = append(r8Flags, "--protect-api-surface") |
| 524 | } |
| 525 | |
Jared Duke | 91d0bee | 2024-11-14 23:10:09 +0000 | [diff] [blame] | 526 | // Avoid unnecessary stack frame noise by only injecting source map ids for non-debug |
| 527 | // optimized or obfuscated targets. |
| 528 | if (Bool(opt.Optimize) || Bool(opt.Obfuscate)) && !debugMode { |
Ian Zerny | fc7df61 | 2021-11-02 15:37:06 +0100 | [diff] [blame] | 529 | // TODO(b/213833843): Allow configuration of the prefix via a build variable. |
| 530 | var sourceFilePrefix = "go/retraceme " |
| 531 | var sourceFileTemplate = "\"" + sourceFilePrefix + "%MAP_ID\"" |
Jared Duke | b832fbb | 2023-09-15 17:16:36 +0000 | [diff] [blame] | 532 | r8Flags = append(r8Flags, "--map-id-template", "%MAP_HASH") |
| 533 | r8Flags = append(r8Flags, "--source-file-template", sourceFileTemplate) |
Christoffer Quist Adamsen | f2d7b16 | 2020-08-24 15:56:16 +0200 | [diff] [blame] | 534 | } |
| 535 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 536 | // TODO(ccross): Don't shrink app instrumentation tests by default. |
| 537 | if !Bool(opt.Shrink) { |
| 538 | r8Flags = append(r8Flags, "-dontshrink") |
| 539 | } |
| 540 | |
| 541 | if !Bool(opt.Optimize) { |
| 542 | r8Flags = append(r8Flags, "-dontoptimize") |
| 543 | } |
| 544 | |
| 545 | // TODO(ccross): error if obufscation + app instrumentation test. |
| 546 | if !Bool(opt.Obfuscate) { |
| 547 | r8Flags = append(r8Flags, "-dontobfuscate") |
| 548 | } |
Colin Cross | 4b964c0 | 2018-10-15 16:18:06 -0700 | [diff] [blame] | 549 | // TODO(ccross): if this is an instrumentation test of an obfuscated app, use the |
| 550 | // dictionary of the app and move the app from libraryjars to injars. |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 551 | |
Christoffer Quist Adamsen | e850737 | 2021-02-22 09:44:09 +0100 | [diff] [blame] | 552 | // TODO(b/180878971): missing classes should be added to the relevant builds. |
Ajinkya Chalke | ddad41b | 2023-02-09 14:09:58 +0000 | [diff] [blame] | 553 | // TODO(b/229727645): do not use true as default for Android platform builds. |
| 554 | if proptools.BoolDefault(opt.Ignore_warnings, true) { |
Remi NGUYEN VAN | bdad314 | 2022-08-04 13:19:03 +0900 | [diff] [blame] | 555 | r8Flags = append(r8Flags, "-ignorewarnings") |
| 556 | } |
Christoffer Quist Adamsen | e850737 | 2021-02-22 09:44:09 +0100 | [diff] [blame] | 557 | |
Rico Wind | a2fa263 | 2024-03-13 13:09:17 +0100 | [diff] [blame] | 558 | // resourcesInput is empty when we don't use resource shrinking, if on, pass these to R8 |
Rico Wind | 98e7fa8 | 2023-11-27 09:44:03 +0100 | [diff] [blame] | 559 | if d.resourcesInput.Valid() { |
| 560 | r8Flags = append(r8Flags, "--resource-input", d.resourcesInput.Path().String()) |
| 561 | r8Deps = append(r8Deps, d.resourcesInput.Path()) |
| 562 | r8Flags = append(r8Flags, "--resource-output", d.resourcesOutput.Path().String()) |
Rico Wind | a7b3859 | 2024-08-26 12:10:44 +0200 | [diff] [blame] | 563 | if d.dexProperties.optimizedResourceShrinkingEnabled(ctx) { |
Rico Wind | a2fa263 | 2024-03-13 13:09:17 +0100 | [diff] [blame] | 564 | r8Flags = append(r8Flags, "--optimized-resource-shrinking") |
Rico Wind | 619ed5c | 2024-10-22 14:00:23 +0200 | [diff] [blame] | 565 | if Bool(d.dexProperties.Optimize.Optimized_shrink_resources) { |
| 566 | // Explicitly opted into optimized shrinking, no need for keeping R$id entries |
| 567 | r8Flags = append(r8Flags, "--force-optimized-resource-shrinking") |
| 568 | } |
Rico Wind | a2fa263 | 2024-03-13 13:09:17 +0100 | [diff] [blame] | 569 | } |
Rico Wind | 98e7fa8 | 2023-11-27 09:44:03 +0100 | [diff] [blame] | 570 | } |
| 571 | |
Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 572 | if flags, deps, profileOutput := d.addArtProfile(ctx, dexParams); profileOutput != nil { |
| 573 | r8Flags = append(r8Flags, flags...) |
| 574 | r8Deps = append(r8Deps, deps...) |
| 575 | artProfileOutput = profileOutput |
| 576 | } |
| 577 | |
Christoffer Adamsen | b81fc91 | 2024-12-17 14:30:47 +0100 | [diff] [blame] | 578 | if ctx.Config().UseR8StoreStoreFenceConstructorInlining() { |
| 579 | r8Flags = append(r8Flags, "--store-store-fence-constructor-inlining") |
| 580 | } |
| 581 | |
Christoffer Adamsen | 914fe68 | 2025-02-27 08:23:49 +0100 | [diff] [blame] | 582 | if opt.Exclude != nil { |
| 583 | r8Flags = append(r8Flags, "--exclude", *opt.Exclude) |
| 584 | r8Deps = append(r8Deps, android.PathForModuleSrc(ctx, *opt.Exclude)) |
| 585 | } |
| 586 | |
Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 587 | return r8Flags, r8Deps, artProfileOutput |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 588 | } |
| 589 | |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame] | 590 | type compileDexParams struct { |
Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 591 | flags javaBuilderFlags |
| 592 | sdkVersion android.SdkSpec |
| 593 | minSdkVersion android.ApiLevel |
| 594 | classesJar android.Path |
| 595 | jarName string |
| 596 | artProfileInput *string |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 599 | // Adds --art-profile to r8/d8 command. |
| 600 | // r8/d8 will output a generated profile file to match the optimized dex code. |
| 601 | func (d *dexer) addArtProfile(ctx android.ModuleContext, dexParams *compileDexParams) (flags []string, deps android.Paths, artProfileOutputPath *android.OutputPath) { |
Cole Faust | 4e9f592 | 2024-11-13 16:09:23 -0800 | [diff] [blame] | 602 | if dexParams.artProfileInput == nil { |
| 603 | return nil, nil, nil |
Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 604 | } |
Cole Faust | 4e9f592 | 2024-11-13 16:09:23 -0800 | [diff] [blame] | 605 | artProfileInputPath := android.PathForModuleSrc(ctx, *dexParams.artProfileInput) |
| 606 | artProfileOutputPathValue := android.PathForModuleOut(ctx, "profile.prof.txt").OutputPath |
| 607 | artProfileOutputPath = &artProfileOutputPathValue |
| 608 | flags = []string{ |
| 609 | "--art-profile", |
| 610 | artProfileInputPath.String(), |
| 611 | artProfileOutputPath.String(), |
| 612 | } |
| 613 | deps = append(deps, artProfileInputPath) |
Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 614 | return flags, deps, artProfileOutputPath |
| 615 | |
| 616 | } |
| 617 | |
| 618 | // Return the compiled dex jar and (optional) profile _after_ r8 optimization |
Colin Cross | 7707b24 | 2024-07-26 12:02:36 -0700 | [diff] [blame] | 619 | func (d *dexer) compileDex(ctx android.ModuleContext, dexParams *compileDexParams) (android.Path, android.Path) { |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 620 | |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 621 | // Compile classes.jar into classes.dex and then javalib.jar |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame] | 622 | javalibJar := android.PathForModuleOut(ctx, "dex", dexParams.jarName).OutputPath |
LaMont Jones | ed6f2b2 | 2025-02-10 16:24:40 -0800 | [diff] [blame] | 623 | cleanPhonyPath := android.PathForModuleOut(ctx, "dex", dexParams.jarName+"-partialcompileclean").OutputPath |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 624 | outDir := android.PathForModuleOut(ctx, "dex") |
| 625 | |
Sasha Smundak | d3cf4ee | 2019-02-15 10:14:23 -0800 | [diff] [blame] | 626 | zipFlags := "--ignore_missing_files" |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 627 | if proptools.Bool(d.dexProperties.Uncompress_dex) { |
Sasha Smundak | d3cf4ee | 2019-02-15 10:14:23 -0800 | [diff] [blame] | 628 | zipFlags += " -L 0" |
Colin Cross | 5a0dcd5 | 2018-10-05 14:20:06 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame] | 631 | commonFlags, commonDeps := d.dexCommonFlags(ctx, dexParams) |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 632 | |
Wei Li | 1e73c65 | 2021-12-06 13:35:11 -0800 | [diff] [blame] | 633 | // Exclude kotlinc generated files when "exclude_kotlinc_generated_files" is set to true. |
| 634 | mergeZipsFlags := "" |
| 635 | if proptools.BoolDefault(d.dexProperties.Exclude_kotlinc_generated_files, false) { |
| 636 | mergeZipsFlags = "-stripFile META-INF/*.kotlin_module -stripFile **/*.kotlin_builtins" |
| 637 | } |
| 638 | |
LaMont Jones | b97f1d1 | 2025-03-18 15:18:58 -0700 | [diff] [blame] | 639 | useR8 := d.effectiveOptimizeEnabled(ctx) |
LaMont Jones | ed6f2b2 | 2025-02-10 16:24:40 -0800 | [diff] [blame] | 640 | useD8 := !useR8 || ctx.Config().PartialCompileFlags().Use_d8 |
| 641 | rbeR8 := ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_R8") |
| 642 | rbeD8 := ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_D8") |
| 643 | var rule blueprint.Rule |
| 644 | var description string |
Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 645 | var artProfileOutputPath *android.OutputPath |
LaMont Jones | ed6f2b2 | 2025-02-10 16:24:40 -0800 | [diff] [blame] | 646 | var implicitOutputs android.WritablePaths |
LaMont Jones | ed6f2b2 | 2025-02-10 16:24:40 -0800 | [diff] [blame] | 647 | var deps android.Paths |
| 648 | args := map[string]string{ |
| 649 | "zipFlags": zipFlags, |
| 650 | "outDir": outDir.String(), |
| 651 | "mergeZipsFlags": mergeZipsFlags, |
| 652 | } |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 653 | if useR8 { |
Colin Cross | c0c664c | 2018-05-16 16:47:21 -0700 | [diff] [blame] | 654 | proguardDictionary := android.PathForModuleOut(ctx, "proguard_dictionary") |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 655 | d.proguardDictionary = android.OptionalPathForPath(proguardDictionary) |
Ian Zerny | 82044f1 | 2023-01-25 12:11:27 +0100 | [diff] [blame] | 656 | proguardConfiguration := android.PathForModuleOut(ctx, "proguard_configuration") |
| 657 | d.proguardConfiguration = android.OptionalPathForPath(proguardConfiguration) |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 658 | proguardUsageDir := android.PathForModuleOut(ctx, "proguard_usage") |
| 659 | proguardUsage := proguardUsageDir.Join(ctx, ctx.Namespace().Path, |
| 660 | android.ModuleNameWithPossibleOverride(ctx), "unused.txt") |
| 661 | proguardUsageZip := android.PathForModuleOut(ctx, "proguard_usage.zip") |
| 662 | d.proguardUsageZip = android.OptionalPathForPath(proguardUsageZip) |
Rico Wind | 98e7fa8 | 2023-11-27 09:44:03 +0100 | [diff] [blame] | 663 | resourcesOutput := android.PathForModuleOut(ctx, "package-res-shrunken.apk") |
| 664 | d.resourcesOutput = android.OptionalPathForPath(resourcesOutput) |
LaMont Jones | ed6f2b2 | 2025-02-10 16:24:40 -0800 | [diff] [blame] | 665 | implicitOutputs = append(implicitOutputs, android.WritablePaths{ |
Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 666 | proguardDictionary, |
| 667 | proguardUsageZip, |
| 668 | proguardConfiguration, |
LaMont Jones | ed6f2b2 | 2025-02-10 16:24:40 -0800 | [diff] [blame] | 669 | }...) |
| 670 | description = "r8" |
Jared Duke | 91d0bee | 2024-11-14 23:10:09 +0000 | [diff] [blame] | 671 | debugMode := android.InList("--debug", commonFlags) |
| 672 | r8Flags, r8Deps, r8ArtProfileOutputPath := d.r8Flags(ctx, dexParams, debugMode) |
LaMont Jones | ed6f2b2 | 2025-02-10 16:24:40 -0800 | [diff] [blame] | 673 | deps = append(deps, r8Deps...) |
| 674 | args["r8Flags"] = strings.Join(append(commonFlags, r8Flags...), " ") |
Spandan Das | d447bbb | 2024-06-24 17:13:30 +0000 | [diff] [blame] | 675 | if r8ArtProfileOutputPath != nil { |
| 676 | artProfileOutputPath = r8ArtProfileOutputPath |
Spandan Das | d447bbb | 2024-06-24 17:13:30 +0000 | [diff] [blame] | 677 | // Add the implicit r8 Art profile output to args so that r8RE knows |
| 678 | // about this implicit output |
LaMont Jones | 61d14fa | 2025-02-20 13:49:07 -0800 | [diff] [blame] | 679 | args["outR8ArtProfile"] = r8ArtProfileOutputPath.String() |
Spandan Das | d447bbb | 2024-06-24 17:13:30 +0000 | [diff] [blame] | 680 | } |
LaMont Jones | ed6f2b2 | 2025-02-10 16:24:40 -0800 | [diff] [blame] | 681 | args["outDict"] = proguardDictionary.String() |
| 682 | args["outConfig"] = proguardConfiguration.String() |
| 683 | args["outUsageDir"] = proguardUsageDir.String() |
| 684 | args["outUsage"] = proguardUsage.String() |
| 685 | args["outUsageZip"] = proguardUsageZip.String() |
Rico Wind | 98e7fa8 | 2023-11-27 09:44:03 +0100 | [diff] [blame] | 686 | if d.resourcesInput.Valid() { |
| 687 | implicitOutputs = append(implicitOutputs, resourcesOutput) |
| 688 | args["resourcesOutput"] = resourcesOutput.String() |
| 689 | } |
LaMont Jones | ed6f2b2 | 2025-02-10 16:24:40 -0800 | [diff] [blame] | 690 | |
| 691 | rule = r8 |
| 692 | if rbeR8 { |
| 693 | rule = r8RE |
| 694 | args["implicits"] = strings.Join(deps.Strings(), ",") |
| 695 | } |
| 696 | } |
| 697 | if useD8 { |
| 698 | description = "d8" |
Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 699 | d8Flags, d8Deps, d8ArtProfileOutputPath := d.d8Flags(ctx, dexParams) |
LaMont Jones | ed6f2b2 | 2025-02-10 16:24:40 -0800 | [diff] [blame] | 700 | deps = append(deps, d8Deps...) |
| 701 | deps = append(deps, commonDeps...) |
| 702 | args["d8Flags"] = strings.Join(append(commonFlags, d8Flags...), " ") |
Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 703 | if d8ArtProfileOutputPath != nil { |
| 704 | artProfileOutputPath = d8ArtProfileOutputPath |
Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 705 | } |
LaMont Jones | ed6f2b2 | 2025-02-10 16:24:40 -0800 | [diff] [blame] | 706 | // If we are generating both d8 and r8, only use RBE when both are enabled. |
| 707 | switch { |
| 708 | case useR8 && rule == r8: |
| 709 | rule = d8r8 |
| 710 | description = "d8r8" |
| 711 | case useR8 && rule == r8RE && rbeD8: |
| 712 | rule = d8r8RE |
| 713 | description = "d8r8" |
| 714 | case rbeD8: |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 715 | rule = d8RE |
LaMont Jones | ed6f2b2 | 2025-02-10 16:24:40 -0800 | [diff] [blame] | 716 | default: |
| 717 | rule = d8 |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 718 | } |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 719 | } |
LaMont Jones | 61d14fa | 2025-02-20 13:49:07 -0800 | [diff] [blame] | 720 | if artProfileOutputPath != nil { |
| 721 | implicitOutputs = append( |
| 722 | implicitOutputs, |
| 723 | artProfileOutputPath, |
| 724 | ) |
| 725 | } |
LaMont Jones | ed6f2b2 | 2025-02-10 16:24:40 -0800 | [diff] [blame] | 726 | ctx.Build(pctx, android.BuildParams{ |
| 727 | Rule: rule, |
| 728 | Description: description, |
| 729 | Output: javalibJar, |
| 730 | ImplicitOutputs: implicitOutputs, |
| 731 | Input: dexParams.classesJar, |
| 732 | Implicits: deps, |
| 733 | Args: args, |
| 734 | }) |
| 735 | if useR8 && useD8 { |
| 736 | // Generate the rule for partial compile clean. |
| 737 | args["builtOut"] = javalibJar.String() |
| 738 | ctx.Build(pctx, android.BuildParams{ |
| 739 | Rule: d8r8Clean, |
| 740 | Description: "d8r8Clean", |
| 741 | Output: cleanPhonyPath, |
| 742 | Args: args, |
| 743 | PhonyOutput: true, |
| 744 | }) |
| 745 | ctx.Phony("partialcompileclean", cleanPhonyPath) |
| 746 | } |
| 747 | |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 748 | if proptools.Bool(d.dexProperties.Uncompress_dex) { |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame] | 749 | alignedJavalibJar := android.PathForModuleOut(ctx, "aligned", dexParams.jarName).OutputPath |
Cole Faust | 51d7bfd | 2023-09-07 05:31:32 +0000 | [diff] [blame] | 750 | TransformZipAlign(ctx, alignedJavalibJar, javalibJar, nil) |
Nicolas Geoffray | 65fd8ba | 2019-01-21 23:20:23 +0000 | [diff] [blame] | 751 | javalibJar = alignedJavalibJar |
| 752 | } |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 753 | |
Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 754 | return javalibJar, artProfileOutputPath |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 755 | } |
Cole Faust | 1dcf9e4 | 2025-02-19 17:23:34 -0800 | [diff] [blame] | 756 | |
| 757 | type ProguardInfo struct { |
| 758 | ModuleName string |
| 759 | Class string |
| 760 | ProguardDictionary android.Path |
| 761 | ProguardUsageZip android.Path |
| 762 | ClassesJar android.Path |
| 763 | } |
| 764 | |
| 765 | var ProguardProvider = blueprint.NewProvider[ProguardInfo]() |