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 | |
| 35 | Optimize struct { |
| 36 | // If false, disable all optimization. Defaults to true for android_app and android_test |
| 37 | // modules, false for java_library and java_test modules. |
| 38 | Enabled *bool |
| 39 | // True if the module containing this has it set by default. |
| 40 | EnabledByDefault bool `blueprint:"mutated"` |
| 41 | |
Christoffer Quist Adamsen | f2d7b16 | 2020-08-24 15:56:16 +0200 | [diff] [blame] | 42 | // If true, runs R8 in Proguard compatibility mode (default). |
| 43 | // Otherwise, runs R8 in full mode. |
| 44 | Proguard_compatibility *bool |
| 45 | |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 46 | // If true, optimize for size by removing unused code. Defaults to true for apps, |
| 47 | // false for libraries and tests. |
| 48 | Shrink *bool |
| 49 | |
| 50 | // If true, optimize bytecode. Defaults to false. |
| 51 | Optimize *bool |
| 52 | |
| 53 | // If true, obfuscate bytecode. Defaults to false. |
| 54 | Obfuscate *bool |
| 55 | |
| 56 | // If true, do not use the flag files generated by aapt that automatically keep |
| 57 | // classes referenced by the app manifest. Defaults to false. |
| 58 | No_aapt_flags *bool |
| 59 | |
| 60 | // Flags to pass to proguard. |
| 61 | Proguard_flags []string |
| 62 | |
| 63 | // Specifies the locations of files containing proguard flags. |
| 64 | Proguard_flags_files []string `android:"path"` |
| 65 | } |
| 66 | |
| 67 | // Keep the data uncompressed. We always need uncompressed dex for execution, |
| 68 | // so this might actually save space by avoiding storing the same data twice. |
| 69 | // This defaults to reasonable value based on module and should not be set. |
| 70 | // It exists only to support ART tests. |
| 71 | Uncompress_dex *bool |
| 72 | } |
| 73 | |
| 74 | type dexer struct { |
| 75 | dexProperties DexProperties |
| 76 | |
| 77 | // list of extra proguard flag files |
| 78 | extraProguardFlagFiles android.Paths |
| 79 | proguardDictionary android.OptionalPath |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 80 | proguardUsageZip android.OptionalPath |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | func (d *dexer) effectiveOptimizeEnabled() bool { |
| 84 | return BoolDefault(d.dexProperties.Optimize.Enabled, d.dexProperties.Optimize.EnabledByDefault) |
| 85 | } |
| 86 | |
Colin Cross | 77cdcfd | 2021-03-12 11:28:25 -0800 | [diff] [blame] | 87 | var d8, d8RE = pctx.MultiCommandRemoteStaticRules("d8", |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 88 | blueprint.RuleParams{ |
| 89 | Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` + |
Colin Cross | 1d73c73 | 2021-08-05 15:16:11 +0000 | [diff] [blame] | 90 | `$d8Template${config.D8Cmd} ${config.DexFlags} --output $outDir $d8Flags $in && ` + |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 91 | `$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` + |
Colin Cross | 4c03f68 | 2018-07-15 08:16:31 -0700 | [diff] [blame] | 92 | `${config.MergeZipsCmd} -D -stripFile "**/*.class" $out $outDir/classes.dex.jar $in`, |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 93 | CommandDeps: []string{ |
| 94 | "${config.D8Cmd}", |
| 95 | "${config.SoongZipCmd}", |
| 96 | "${config.MergeZipsCmd}", |
| 97 | }, |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 98 | }, map[string]*remoteexec.REParams{ |
| 99 | "$d8Template": &remoteexec.REParams{ |
| 100 | Labels: map[string]string{"type": "compile", "compiler": "d8"}, |
| 101 | Inputs: []string{"${config.D8Jar}"}, |
| 102 | ExecStrategy: "${config.RED8ExecStrategy}", |
| 103 | ToolchainInputs: []string{"${config.JavaCmd}"}, |
| 104 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 105 | }, |
| 106 | "$zipTemplate": &remoteexec.REParams{ |
| 107 | Labels: map[string]string{"type": "tool", "name": "soong_zip"}, |
| 108 | Inputs: []string{"${config.SoongZipCmd}", "$outDir"}, |
| 109 | OutputFiles: []string{"$outDir/classes.dex.jar"}, |
| 110 | ExecStrategy: "${config.RED8ExecStrategy}", |
| 111 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 112 | }, |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 113 | }, []string{"outDir", "d8Flags", "zipFlags"}, nil) |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 114 | |
Colin Cross | 77cdcfd | 2021-03-12 11:28:25 -0800 | [diff] [blame] | 115 | var r8, r8RE = pctx.MultiCommandRemoteStaticRules("r8", |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 116 | blueprint.RuleParams{ |
| 117 | Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` + |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 118 | `rm -f "$outDict" && rm -rf "${outUsageDir}" && ` + |
| 119 | `mkdir -p $$(dirname ${outUsage}) && ` + |
Colin Cross | a832a04 | 2021-08-05 16:56:18 -0700 | [diff] [blame^] | 120 | `$r8Template${config.R8Cmd} ${config.DexFlags} -injars $in --output $outDir ` + |
Søren Gjesse | 24f1702 | 2018-09-14 15:20:42 +0200 | [diff] [blame] | 121 | `--no-data-resources ` + |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 122 | `-printmapping ${outDict} ` + |
| 123 | `-printusage ${outUsage} ` + |
Colin Cross | ffb657e | 2018-09-21 12:29:22 -0700 | [diff] [blame] | 124 | `$r8Flags && ` + |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 125 | `touch "${outDict}" "${outUsage}" && ` + |
| 126 | `${config.SoongZipCmd} -o ${outUsageZip} -C ${outUsageDir} -f ${outUsage} && ` + |
| 127 | `rm -rf ${outUsageDir} && ` + |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 128 | `$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` + |
Colin Cross | 4c03f68 | 2018-07-15 08:16:31 -0700 | [diff] [blame] | 129 | `${config.MergeZipsCmd} -D -stripFile "**/*.class" $out $outDir/classes.dex.jar $in`, |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 130 | CommandDeps: []string{ |
Colin Cross | a832a04 | 2021-08-05 16:56:18 -0700 | [diff] [blame^] | 131 | "${config.R8Cmd}", |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 132 | "${config.SoongZipCmd}", |
| 133 | "${config.MergeZipsCmd}", |
| 134 | }, |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 135 | }, map[string]*remoteexec.REParams{ |
| 136 | "$r8Template": &remoteexec.REParams{ |
| 137 | Labels: map[string]string{"type": "compile", "compiler": "r8"}, |
| 138 | Inputs: []string{"$implicits", "${config.R8Jar}"}, |
Colin Cross | e00c0e7 | 2020-09-17 11:54:30 -0700 | [diff] [blame] | 139 | OutputFiles: []string{"${outUsage}"}, |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 140 | ExecStrategy: "${config.RER8ExecStrategy}", |
| 141 | ToolchainInputs: []string{"${config.JavaCmd}"}, |
| 142 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 143 | }, |
| 144 | "$zipTemplate": &remoteexec.REParams{ |
| 145 | Labels: map[string]string{"type": "tool", "name": "soong_zip"}, |
| 146 | Inputs: []string{"${config.SoongZipCmd}", "$outDir"}, |
| 147 | OutputFiles: []string{"$outDir/classes.dex.jar"}, |
| 148 | ExecStrategy: "${config.RER8ExecStrategy}", |
| 149 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 150 | }, |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 151 | "$zipUsageTemplate": &remoteexec.REParams{ |
| 152 | Labels: map[string]string{"type": "tool", "name": "soong_zip"}, |
| 153 | Inputs: []string{"${config.SoongZipCmd}", "${outUsage}"}, |
| 154 | OutputFiles: []string{"${outUsageZip}"}, |
| 155 | ExecStrategy: "${config.RER8ExecStrategy}", |
| 156 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 157 | }, |
| 158 | }, []string{"outDir", "outDict", "outUsage", "outUsageZip", "outUsageDir", |
| 159 | "r8Flags", "zipFlags"}, []string{"implicits"}) |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 160 | |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 161 | func (d *dexer) dexCommonFlags(ctx android.ModuleContext, minSdkVersion android.SdkSpec) []string { |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 162 | flags := d.dexProperties.Dxflags |
Colin Cross | bafb897 | 2018-06-06 21:46:32 +0000 | [diff] [blame] | 163 | // Translate all the DX flags to D8 ones until all the build files have been migrated |
| 164 | // to D8 flags. See: b/69377755 |
| 165 | flags = android.RemoveListFromList(flags, |
| 166 | []string{"--core-library", "--dex", "--multi-dex"}) |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 167 | |
| 168 | if ctx.Config().Getenv("NO_OPTIMIZE_DX") != "" { |
Colin Cross | bafb897 | 2018-06-06 21:46:32 +0000 | [diff] [blame] | 169 | flags = append(flags, "--debug") |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | if ctx.Config().Getenv("GENERATE_DEX_DEBUG") != "" { |
| 173 | flags = append(flags, |
| 174 | "--debug", |
| 175 | "--verbose") |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 178 | effectiveVersion, err := minSdkVersion.EffectiveVersion(ctx) |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 179 | if err != nil { |
| 180 | ctx.PropertyErrorf("min_sdk_version", "%s", err) |
| 181 | } |
| 182 | |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 183 | flags = append(flags, "--min-api "+strconv.Itoa(effectiveVersion.FinalOrFutureInt())) |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 184 | return flags |
| 185 | } |
| 186 | |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 187 | func d8Flags(flags javaBuilderFlags) (d8Flags []string, d8Deps android.Paths) { |
Colin Cross | c2557d1 | 2019-10-31 15:22:57 -0700 | [diff] [blame] | 188 | d8Flags = append(d8Flags, flags.bootClasspath.FormRepeatedClassPath("--lib ")...) |
| 189 | d8Flags = append(d8Flags, flags.classpath.FormRepeatedClassPath("--lib ")...) |
Colin Cross | ffb657e | 2018-09-21 12:29:22 -0700 | [diff] [blame] | 190 | |
Colin Cross | 6dab9bd | 2018-09-28 08:06:24 -0700 | [diff] [blame] | 191 | d8Deps = append(d8Deps, flags.bootClasspath...) |
| 192 | d8Deps = append(d8Deps, flags.classpath...) |
| 193 | |
| 194 | return d8Flags, d8Deps |
Colin Cross | ffb657e | 2018-09-21 12:29:22 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 197 | func (d *dexer) r8Flags(ctx android.ModuleContext, flags javaBuilderFlags) (r8Flags []string, r8Deps android.Paths) { |
| 198 | opt := d.dexProperties.Optimize |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 199 | |
| 200 | // When an app contains references to APIs that are not in the SDK specified by |
| 201 | // 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] | 202 | // classes added by desugaring, we artifically raise the "SDK version" "linked" by |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 203 | // ProGuard, to |
| 204 | // - suppress ProGuard warnings of referencing symbols unknown to the lower SDK version. |
| 205 | // - prevent ProGuard stripping subclass in the support library that extends class added in the higher SDK version. |
| 206 | // See b/20667396 |
| 207 | var proguardRaiseDeps classpath |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 208 | ctx.VisitDirectDepsWithTag(proguardRaiseTag, func(m android.Module) { |
| 209 | dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo) |
| 210 | proguardRaiseDeps = append(proguardRaiseDeps, dep.HeaderJars...) |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 211 | }) |
| 212 | |
| 213 | r8Flags = append(r8Flags, proguardRaiseDeps.FormJavaClassPath("-libraryjars")) |
| 214 | r8Flags = append(r8Flags, flags.bootClasspath.FormJavaClassPath("-libraryjars")) |
| 215 | r8Flags = append(r8Flags, flags.classpath.FormJavaClassPath("-libraryjars")) |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 216 | |
Colin Cross | 6dab9bd | 2018-09-28 08:06:24 -0700 | [diff] [blame] | 217 | r8Deps = append(r8Deps, proguardRaiseDeps...) |
| 218 | r8Deps = append(r8Deps, flags.bootClasspath...) |
| 219 | r8Deps = append(r8Deps, flags.classpath...) |
| 220 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 221 | flagFiles := android.Paths{ |
| 222 | android.PathForSource(ctx, "build/make/core/proguard.flags"), |
| 223 | } |
| 224 | |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 225 | flagFiles = append(flagFiles, d.extraProguardFlagFiles...) |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 226 | // TODO(ccross): static android library proguard files |
| 227 | |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 228 | flagFiles = append(flagFiles, android.PathsForModuleSrc(ctx, opt.Proguard_flags_files)...) |
Colin Cross | bd1cef5 | 2018-08-13 10:28:18 -0700 | [diff] [blame] | 229 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 230 | r8Flags = append(r8Flags, android.JoinWithPrefix(flagFiles.Strings(), "-include ")) |
| 231 | r8Deps = append(r8Deps, flagFiles...) |
| 232 | |
| 233 | // TODO(b/70942988): This is included from build/make/core/proguard.flags |
| 234 | r8Deps = append(r8Deps, android.PathForSource(ctx, |
| 235 | "build/make/core/proguard_basic_keeps.flags")) |
| 236 | |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 237 | r8Flags = append(r8Flags, opt.Proguard_flags...) |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 238 | |
Christoffer Quist Adamsen | f2d7b16 | 2020-08-24 15:56:16 +0200 | [diff] [blame] | 239 | if BoolDefault(opt.Proguard_compatibility, true) { |
| 240 | r8Flags = append(r8Flags, "--force-proguard-compatibility") |
| 241 | } |
| 242 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 243 | // TODO(ccross): Don't shrink app instrumentation tests by default. |
| 244 | if !Bool(opt.Shrink) { |
| 245 | r8Flags = append(r8Flags, "-dontshrink") |
| 246 | } |
| 247 | |
| 248 | if !Bool(opt.Optimize) { |
| 249 | r8Flags = append(r8Flags, "-dontoptimize") |
| 250 | } |
| 251 | |
| 252 | // TODO(ccross): error if obufscation + app instrumentation test. |
| 253 | if !Bool(opt.Obfuscate) { |
| 254 | r8Flags = append(r8Flags, "-dontobfuscate") |
| 255 | } |
Colin Cross | 4b964c0 | 2018-10-15 16:18:06 -0700 | [diff] [blame] | 256 | // TODO(ccross): if this is an instrumentation test of an obfuscated app, use the |
| 257 | // dictionary of the app and move the app from libraryjars to injars. |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 258 | |
Jaewoong Jung | 1d6eb68 | 2018-11-29 15:08:44 -0800 | [diff] [blame] | 259 | // Don't strip out debug information for eng builds. |
| 260 | if ctx.Config().Eng() { |
| 261 | r8Flags = append(r8Flags, "--debug") |
| 262 | } |
| 263 | |
Christoffer Quist Adamsen | e850737 | 2021-02-22 09:44:09 +0100 | [diff] [blame] | 264 | // TODO(b/180878971): missing classes should be added to the relevant builds. |
| 265 | r8Flags = append(r8Flags, "-ignorewarnings") |
| 266 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 267 | return r8Flags, r8Deps |
| 268 | } |
| 269 | |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 270 | func (d *dexer) compileDex(ctx android.ModuleContext, flags javaBuilderFlags, minSdkVersion android.SdkSpec, |
Paul Duffin | 612e610 | 2021-02-02 13:38:13 +0000 | [diff] [blame] | 271 | classesJar android.Path, jarName string) android.OutputPath { |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 272 | |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 273 | // Compile classes.jar into classes.dex and then javalib.jar |
Paul Duffin | 612e610 | 2021-02-02 13:38:13 +0000 | [diff] [blame] | 274 | javalibJar := android.PathForModuleOut(ctx, "dex", jarName).OutputPath |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 275 | outDir := android.PathForModuleOut(ctx, "dex") |
| 276 | |
Sasha Smundak | d3cf4ee | 2019-02-15 10:14:23 -0800 | [diff] [blame] | 277 | zipFlags := "--ignore_missing_files" |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 278 | if proptools.Bool(d.dexProperties.Uncompress_dex) { |
Sasha Smundak | d3cf4ee | 2019-02-15 10:14:23 -0800 | [diff] [blame] | 279 | zipFlags += " -L 0" |
Colin Cross | 5a0dcd5 | 2018-10-05 14:20:06 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 282 | commonFlags := d.dexCommonFlags(ctx, minSdkVersion) |
| 283 | |
| 284 | useR8 := d.effectiveOptimizeEnabled() |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 285 | if useR8 { |
Colin Cross | c0c664c | 2018-05-16 16:47:21 -0700 | [diff] [blame] | 286 | proguardDictionary := android.PathForModuleOut(ctx, "proguard_dictionary") |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 287 | d.proguardDictionary = android.OptionalPathForPath(proguardDictionary) |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 288 | proguardUsageDir := android.PathForModuleOut(ctx, "proguard_usage") |
| 289 | proguardUsage := proguardUsageDir.Join(ctx, ctx.Namespace().Path, |
| 290 | android.ModuleNameWithPossibleOverride(ctx), "unused.txt") |
| 291 | proguardUsageZip := android.PathForModuleOut(ctx, "proguard_usage.zip") |
| 292 | d.proguardUsageZip = android.OptionalPathForPath(proguardUsageZip) |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 293 | r8Flags, r8Deps := d.r8Flags(ctx, flags) |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 294 | rule := r8 |
| 295 | args := map[string]string{ |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 296 | "r8Flags": strings.Join(append(commonFlags, r8Flags...), " "), |
| 297 | "zipFlags": zipFlags, |
| 298 | "outDict": proguardDictionary.String(), |
| 299 | "outUsageDir": proguardUsageDir.String(), |
| 300 | "outUsage": proguardUsage.String(), |
| 301 | "outUsageZip": proguardUsageZip.String(), |
| 302 | "outDir": outDir.String(), |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 303 | } |
Ramy Medhat | 16f23a4 | 2020-09-03 01:29:49 -0400 | [diff] [blame] | 304 | if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_R8") { |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 305 | rule = r8RE |
| 306 | args["implicits"] = strings.Join(r8Deps.Strings(), ",") |
| 307 | } |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 308 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 309 | Rule: rule, |
| 310 | Description: "r8", |
| 311 | Output: javalibJar, |
| 312 | ImplicitOutputs: android.WritablePaths{proguardDictionary, proguardUsageZip}, |
| 313 | Input: classesJar, |
| 314 | Implicits: r8Deps, |
| 315 | Args: args, |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 316 | }) |
| 317 | } else { |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 318 | d8Flags, d8Deps := d8Flags(flags) |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 319 | rule := d8 |
Ramy Medhat | 16f23a4 | 2020-09-03 01:29:49 -0400 | [diff] [blame] | 320 | if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_D8") { |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 321 | rule = d8RE |
| 322 | } |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 323 | ctx.Build(pctx, android.BuildParams{ |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 324 | Rule: rule, |
Colin Cross | bafb897 | 2018-06-06 21:46:32 +0000 | [diff] [blame] | 325 | Description: "d8", |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 326 | Output: javalibJar, |
| 327 | Input: classesJar, |
Colin Cross | 6dab9bd | 2018-09-28 08:06:24 -0700 | [diff] [blame] | 328 | Implicits: d8Deps, |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 329 | Args: map[string]string{ |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 330 | "d8Flags": strings.Join(append(commonFlags, d8Flags...), " "), |
Colin Cross | 5a0dcd5 | 2018-10-05 14:20:06 -0700 | [diff] [blame] | 331 | "zipFlags": zipFlags, |
| 332 | "outDir": outDir.String(), |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 333 | }, |
| 334 | }) |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 335 | } |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 336 | if proptools.Bool(d.dexProperties.Uncompress_dex) { |
Paul Duffin | 612e610 | 2021-02-02 13:38:13 +0000 | [diff] [blame] | 337 | alignedJavalibJar := android.PathForModuleOut(ctx, "aligned", jarName).OutputPath |
Nicolas Geoffray | 65fd8ba | 2019-01-21 23:20:23 +0000 | [diff] [blame] | 338 | TransformZipAlign(ctx, alignedJavalibJar, javalibJar) |
| 339 | javalibJar = alignedJavalibJar |
| 340 | } |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 341 | |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 342 | return javalibJar |
| 343 | } |