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 | |
Ajinkya Chalke | ddad41b | 2023-02-09 14:09:58 +0000 | [diff] [blame] | 45 | // 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] | 46 | Ignore_warnings *bool |
| 47 | |
Roy Luo | 74a5c2e | 2023-08-02 23:56:46 +0000 | [diff] [blame] | 48 | // If true, runs R8 in Proguard compatibility mode (default). |
| 49 | // Otherwise, runs R8 in full mode. |
Christoffer Quist Adamsen | f2d7b16 | 2020-08-24 15:56:16 +0200 | [diff] [blame] | 50 | Proguard_compatibility *bool |
| 51 | |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 52 | // If true, optimize for size by removing unused code. Defaults to true for apps, |
| 53 | // false for libraries and tests. |
| 54 | Shrink *bool |
| 55 | |
| 56 | // If true, optimize bytecode. Defaults to false. |
| 57 | Optimize *bool |
| 58 | |
| 59 | // If true, obfuscate bytecode. Defaults to false. |
| 60 | Obfuscate *bool |
| 61 | |
| 62 | // If true, do not use the flag files generated by aapt that automatically keep |
| 63 | // classes referenced by the app manifest. Defaults to false. |
| 64 | No_aapt_flags *bool |
| 65 | |
Jared Duke | 51b0a10 | 2022-09-27 16:53:11 -0700 | [diff] [blame] | 66 | // If true, optimize for size by removing unused resources. Defaults to false. |
Rico Wind | 351bac9 | 2022-09-22 10:41:42 +0200 | [diff] [blame] | 67 | Shrink_resources *bool |
| 68 | |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 69 | // Flags to pass to proguard. |
| 70 | Proguard_flags []string |
| 71 | |
| 72 | // Specifies the locations of files containing proguard flags. |
| 73 | Proguard_flags_files []string `android:"path"` |
| 74 | } |
| 75 | |
| 76 | // Keep the data uncompressed. We always need uncompressed dex for execution, |
| 77 | // so this might actually save space by avoiding storing the same data twice. |
| 78 | // This defaults to reasonable value based on module and should not be set. |
| 79 | // It exists only to support ART tests. |
| 80 | Uncompress_dex *bool |
Wei Li | 1e73c65 | 2021-12-06 13:35:11 -0800 | [diff] [blame] | 81 | |
Wei Li | 92cd54b | 2022-01-12 13:22:55 -0800 | [diff] [blame] | 82 | // Exclude kotlinc generate files: *.kotlin_module, *.kotlin_builtins. Defaults to false. |
Wei Li | 1e73c65 | 2021-12-06 13:35:11 -0800 | [diff] [blame] | 83 | Exclude_kotlinc_generated_files *bool |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | type dexer struct { |
| 87 | dexProperties DexProperties |
| 88 | |
| 89 | // list of extra proguard flag files |
| 90 | extraProguardFlagFiles android.Paths |
| 91 | proguardDictionary android.OptionalPath |
Ian Zerny | 82044f1 | 2023-01-25 12:11:27 +0100 | [diff] [blame] | 92 | proguardConfiguration android.OptionalPath |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 93 | proguardUsageZip android.OptionalPath |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 94 | |
| 95 | providesTransitiveHeaderJars |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | func (d *dexer) effectiveOptimizeEnabled() bool { |
| 99 | return BoolDefault(d.dexProperties.Optimize.Enabled, d.dexProperties.Optimize.EnabledByDefault) |
| 100 | } |
| 101 | |
Colin Cross | 77cdcfd | 2021-03-12 11:28:25 -0800 | [diff] [blame] | 102 | var d8, d8RE = pctx.MultiCommandRemoteStaticRules("d8", |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 103 | blueprint.RuleParams{ |
| 104 | Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` + |
Colin Cross | a79a52c | 2021-08-04 10:52:44 -0700 | [diff] [blame] | 105 | `mkdir -p $$(dirname $tmpJar) && ` + |
| 106 | `${config.Zip2ZipCmd} -i $in -o $tmpJar -x '**/*.dex' && ` + |
Jared Duke | 0cf7c96 | 2022-04-20 20:28:33 +0000 | [diff] [blame] | 107 | `$d8Template${config.D8Cmd} ${config.D8Flags} --output $outDir $d8Flags $tmpJar && ` + |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 108 | `$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` + |
Wei Li | 1e73c65 | 2021-12-06 13:35:11 -0800 | [diff] [blame] | 109 | `${config.MergeZipsCmd} -D -stripFile "**/*.class" $mergeZipsFlags $out $outDir/classes.dex.jar $in`, |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 110 | CommandDeps: []string{ |
| 111 | "${config.D8Cmd}", |
Colin Cross | a79a52c | 2021-08-04 10:52:44 -0700 | [diff] [blame] | 112 | "${config.Zip2ZipCmd}", |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 113 | "${config.SoongZipCmd}", |
| 114 | "${config.MergeZipsCmd}", |
| 115 | }, |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 116 | }, map[string]*remoteexec.REParams{ |
| 117 | "$d8Template": &remoteexec.REParams{ |
| 118 | Labels: map[string]string{"type": "compile", "compiler": "d8"}, |
| 119 | Inputs: []string{"${config.D8Jar}"}, |
| 120 | ExecStrategy: "${config.RED8ExecStrategy}", |
| 121 | ToolchainInputs: []string{"${config.JavaCmd}"}, |
| 122 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 123 | }, |
| 124 | "$zipTemplate": &remoteexec.REParams{ |
| 125 | Labels: map[string]string{"type": "tool", "name": "soong_zip"}, |
| 126 | Inputs: []string{"${config.SoongZipCmd}", "$outDir"}, |
| 127 | OutputFiles: []string{"$outDir/classes.dex.jar"}, |
| 128 | ExecStrategy: "${config.RED8ExecStrategy}", |
| 129 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 130 | }, |
Wei Li | 1e73c65 | 2021-12-06 13:35:11 -0800 | [diff] [blame] | 131 | }, []string{"outDir", "d8Flags", "zipFlags", "tmpJar", "mergeZipsFlags"}, nil) |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 132 | |
Colin Cross | 77cdcfd | 2021-03-12 11:28:25 -0800 | [diff] [blame] | 133 | var r8, r8RE = pctx.MultiCommandRemoteStaticRules("r8", |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 134 | blueprint.RuleParams{ |
| 135 | Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` + |
Ian Zerny | 82044f1 | 2023-01-25 12:11:27 +0100 | [diff] [blame] | 136 | `rm -f "$outDict" && rm -f "$outConfig" && rm -rf "${outUsageDir}" && ` + |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 137 | `mkdir -p $$(dirname ${outUsage}) && ` + |
Colin Cross | a79a52c | 2021-08-04 10:52:44 -0700 | [diff] [blame] | 138 | `mkdir -p $$(dirname $tmpJar) && ` + |
| 139 | `${config.Zip2ZipCmd} -i $in -o $tmpJar -x '**/*.dex' && ` + |
Jared Duke | 0cf7c96 | 2022-04-20 20:28:33 +0000 | [diff] [blame] | 140 | `$r8Template${config.R8Cmd} ${config.R8Flags} -injars $tmpJar --output $outDir ` + |
Søren Gjesse | 24f1702 | 2018-09-14 15:20:42 +0200 | [diff] [blame] | 141 | `--no-data-resources ` + |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 142 | `-printmapping ${outDict} ` + |
Jared Duke | 34c6d7d | 2023-05-05 20:40:20 +0000 | [diff] [blame] | 143 | `-printconfiguration ${outConfig} ` + |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 144 | `-printusage ${outUsage} ` + |
Colin Cross | 22e6a6f | 2022-03-21 12:19:44 -0700 | [diff] [blame] | 145 | `--deps-file ${out}.d ` + |
Colin Cross | ffb657e | 2018-09-21 12:29:22 -0700 | [diff] [blame] | 146 | `$r8Flags && ` + |
Ian Zerny | 82044f1 | 2023-01-25 12:11:27 +0100 | [diff] [blame] | 147 | `touch "${outDict}" "${outConfig}" "${outUsage}" && ` + |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 148 | `${config.SoongZipCmd} -o ${outUsageZip} -C ${outUsageDir} -f ${outUsage} && ` + |
| 149 | `rm -rf ${outUsageDir} && ` + |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 150 | `$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` + |
Wei Li | 1e73c65 | 2021-12-06 13:35:11 -0800 | [diff] [blame] | 151 | `${config.MergeZipsCmd} -D -stripFile "**/*.class" $mergeZipsFlags $out $outDir/classes.dex.jar $in`, |
Colin Cross | 22e6a6f | 2022-03-21 12:19:44 -0700 | [diff] [blame] | 152 | Depfile: "${out}.d", |
| 153 | Deps: blueprint.DepsGCC, |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 154 | CommandDeps: []string{ |
Colin Cross | a832a04 | 2021-08-05 16:56:18 -0700 | [diff] [blame] | 155 | "${config.R8Cmd}", |
Colin Cross | a79a52c | 2021-08-04 10:52:44 -0700 | [diff] [blame] | 156 | "${config.Zip2ZipCmd}", |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 157 | "${config.SoongZipCmd}", |
| 158 | "${config.MergeZipsCmd}", |
| 159 | }, |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 160 | }, map[string]*remoteexec.REParams{ |
| 161 | "$r8Template": &remoteexec.REParams{ |
| 162 | Labels: map[string]string{"type": "compile", "compiler": "r8"}, |
| 163 | Inputs: []string{"$implicits", "${config.R8Jar}"}, |
Rico Wind | b565f2f | 2023-05-12 12:30:07 +0200 | [diff] [blame] | 164 | OutputFiles: []string{"${outUsage}", "${outConfig}", "${outDict}"}, |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 165 | ExecStrategy: "${config.RER8ExecStrategy}", |
| 166 | ToolchainInputs: []string{"${config.JavaCmd}"}, |
| 167 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 168 | }, |
| 169 | "$zipTemplate": &remoteexec.REParams{ |
| 170 | Labels: map[string]string{"type": "tool", "name": "soong_zip"}, |
| 171 | Inputs: []string{"${config.SoongZipCmd}", "$outDir"}, |
| 172 | OutputFiles: []string{"$outDir/classes.dex.jar"}, |
| 173 | ExecStrategy: "${config.RER8ExecStrategy}", |
| 174 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 175 | }, |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 176 | "$zipUsageTemplate": &remoteexec.REParams{ |
| 177 | Labels: map[string]string{"type": "tool", "name": "soong_zip"}, |
| 178 | Inputs: []string{"${config.SoongZipCmd}", "${outUsage}"}, |
| 179 | OutputFiles: []string{"${outUsageZip}"}, |
| 180 | ExecStrategy: "${config.RER8ExecStrategy}", |
| 181 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 182 | }, |
Ian Zerny | 82044f1 | 2023-01-25 12:11:27 +0100 | [diff] [blame] | 183 | }, []string{"outDir", "outDict", "outConfig", "outUsage", "outUsageZip", "outUsageDir", |
Wei Li | 1e73c65 | 2021-12-06 13:35:11 -0800 | [diff] [blame] | 184 | "r8Flags", "zipFlags", "tmpJar", "mergeZipsFlags"}, []string{"implicits"}) |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 185 | |
Colin Cross | 5ea963e | 2021-09-16 19:13:43 -0700 | [diff] [blame] | 186 | func (d *dexer) dexCommonFlags(ctx android.ModuleContext, |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame] | 187 | dexParams *compileDexParams) (flags []string, deps android.Paths) { |
Colin Cross | 5ea963e | 2021-09-16 19:13:43 -0700 | [diff] [blame] | 188 | |
| 189 | flags = d.dexProperties.Dxflags |
Colin Cross | bafb897 | 2018-06-06 21:46:32 +0000 | [diff] [blame] | 190 | // Translate all the DX flags to D8 ones until all the build files have been migrated |
| 191 | // to D8 flags. See: b/69377755 |
| 192 | flags = android.RemoveListFromList(flags, |
| 193 | []string{"--core-library", "--dex", "--multi-dex"}) |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 194 | |
Colin Cross | 5ea963e | 2021-09-16 19:13:43 -0700 | [diff] [blame] | 195 | for _, f := range android.PathsForModuleSrc(ctx, d.dexProperties.Main_dex_rules) { |
| 196 | flags = append(flags, "--main-dex-rules", f.String()) |
| 197 | deps = append(deps, f) |
| 198 | } |
| 199 | |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 200 | if ctx.Config().Getenv("NO_OPTIMIZE_DX") != "" { |
Colin Cross | bafb897 | 2018-06-06 21:46:32 +0000 | [diff] [blame] | 201 | flags = append(flags, "--debug") |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | if ctx.Config().Getenv("GENERATE_DEX_DEBUG") != "" { |
| 205 | flags = append(flags, |
| 206 | "--debug", |
| 207 | "--verbose") |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Jared Duke | 40d731a | 2022-09-20 15:32:14 -0700 | [diff] [blame] | 210 | // Supplying the platform build flag disables various features like API modeling and desugaring. |
| 211 | // For targets with a stable min SDK version (i.e., when the min SDK is both explicitly specified |
| 212 | // and managed+versioned), we suppress this flag to ensure portability. |
| 213 | // Note: Targets with a min SDK kind of core_platform (e.g., framework.jar) or unspecified (e.g., |
| 214 | // services.jar), are not classified as stable, which is WAI. |
| 215 | // TODO(b/232073181): Expand to additional min SDK cases after validation. |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame] | 216 | if !dexParams.sdkVersion.Stable() { |
Jared Duke | 40d731a | 2022-09-20 15:32:14 -0700 | [diff] [blame] | 217 | flags = append(flags, "--android-platform-build") |
| 218 | } |
| 219 | |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame] | 220 | effectiveVersion, err := dexParams.minSdkVersion.EffectiveVersion(ctx) |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 221 | if err != nil { |
| 222 | ctx.PropertyErrorf("min_sdk_version", "%s", err) |
| 223 | } |
| 224 | |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 225 | flags = append(flags, "--min-api "+strconv.Itoa(effectiveVersion.FinalOrFutureInt())) |
Colin Cross | 5ea963e | 2021-09-16 19:13:43 -0700 | [diff] [blame] | 226 | return flags, deps |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 229 | func d8Flags(flags javaBuilderFlags) (d8Flags []string, d8Deps android.Paths) { |
Colin Cross | c2557d1 | 2019-10-31 15:22:57 -0700 | [diff] [blame] | 230 | d8Flags = append(d8Flags, flags.bootClasspath.FormRepeatedClassPath("--lib ")...) |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 231 | d8Flags = append(d8Flags, flags.dexClasspath.FormRepeatedClassPath("--lib ")...) |
Colin Cross | ffb657e | 2018-09-21 12:29:22 -0700 | [diff] [blame] | 232 | |
Colin Cross | 6dab9bd | 2018-09-28 08:06:24 -0700 | [diff] [blame] | 233 | d8Deps = append(d8Deps, flags.bootClasspath...) |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 234 | d8Deps = append(d8Deps, flags.dexClasspath...) |
Colin Cross | 6dab9bd | 2018-09-28 08:06:24 -0700 | [diff] [blame] | 235 | |
| 236 | return d8Flags, d8Deps |
Colin Cross | ffb657e | 2018-09-21 12:29:22 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 239 | func (d *dexer) r8Flags(ctx android.ModuleContext, flags javaBuilderFlags) (r8Flags []string, r8Deps android.Paths) { |
| 240 | opt := d.dexProperties.Optimize |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 241 | |
| 242 | // When an app contains references to APIs that are not in the SDK specified by |
| 243 | // 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] | 244 | // classes added by desugaring, we artifically raise the "SDK version" "linked" by |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 245 | // ProGuard, to |
| 246 | // - suppress ProGuard warnings of referencing symbols unknown to the lower SDK version. |
| 247 | // - prevent ProGuard stripping subclass in the support library that extends class added in the higher SDK version. |
| 248 | // See b/20667396 |
| 249 | var proguardRaiseDeps classpath |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 250 | ctx.VisitDirectDepsWithTag(proguardRaiseTag, func(m android.Module) { |
| 251 | dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo) |
| 252 | proguardRaiseDeps = append(proguardRaiseDeps, dep.HeaderJars...) |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 253 | }) |
| 254 | |
| 255 | r8Flags = append(r8Flags, proguardRaiseDeps.FormJavaClassPath("-libraryjars")) |
Colin Cross | 6dab9bd | 2018-09-28 08:06:24 -0700 | [diff] [blame] | 256 | r8Deps = append(r8Deps, proguardRaiseDeps...) |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 257 | r8Flags = append(r8Flags, flags.bootClasspath.FormJavaClassPath("-libraryjars")) |
Colin Cross | 6dab9bd | 2018-09-28 08:06:24 -0700 | [diff] [blame] | 258 | r8Deps = append(r8Deps, flags.bootClasspath...) |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 259 | r8Flags = append(r8Flags, flags.dexClasspath.FormJavaClassPath("-libraryjars")) |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 260 | r8Deps = append(r8Deps, flags.dexClasspath...) |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 261 | |
| 262 | transitiveStaticLibsLookupMap := map[android.Path]bool{} |
| 263 | if d.transitiveStaticLibsHeaderJars != nil { |
| 264 | for _, jar := range d.transitiveStaticLibsHeaderJars.ToList() { |
| 265 | transitiveStaticLibsLookupMap[jar] = true |
| 266 | } |
| 267 | } |
| 268 | transitiveHeaderJars := android.Paths{} |
| 269 | if d.transitiveLibsHeaderJars != nil { |
| 270 | for _, jar := range d.transitiveLibsHeaderJars.ToList() { |
| 271 | if _, ok := transitiveStaticLibsLookupMap[jar]; ok { |
| 272 | // don't include a lib if it is already packaged in the current JAR as a static lib |
| 273 | continue |
| 274 | } |
| 275 | transitiveHeaderJars = append(transitiveHeaderJars, jar) |
| 276 | } |
| 277 | } |
| 278 | transitiveClasspath := classpath(transitiveHeaderJars) |
| 279 | r8Flags = append(r8Flags, transitiveClasspath.FormJavaClassPath("-libraryjars")) |
| 280 | r8Deps = append(r8Deps, transitiveClasspath...) |
Colin Cross | 6dab9bd | 2018-09-28 08:06:24 -0700 | [diff] [blame] | 281 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 282 | flagFiles := android.Paths{ |
| 283 | android.PathForSource(ctx, "build/make/core/proguard.flags"), |
| 284 | } |
| 285 | |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 286 | flagFiles = append(flagFiles, d.extraProguardFlagFiles...) |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 287 | // TODO(ccross): static android library proguard files |
| 288 | |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 289 | flagFiles = append(flagFiles, android.PathsForModuleSrc(ctx, opt.Proguard_flags_files)...) |
Colin Cross | bd1cef5 | 2018-08-13 10:28:18 -0700 | [diff] [blame] | 290 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 291 | r8Flags = append(r8Flags, android.JoinWithPrefix(flagFiles.Strings(), "-include ")) |
| 292 | r8Deps = append(r8Deps, flagFiles...) |
| 293 | |
| 294 | // TODO(b/70942988): This is included from build/make/core/proguard.flags |
| 295 | r8Deps = append(r8Deps, android.PathForSource(ctx, |
| 296 | "build/make/core/proguard_basic_keeps.flags")) |
| 297 | |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 298 | r8Flags = append(r8Flags, opt.Proguard_flags...) |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 299 | |
Christoffer Quist Adamsen | f2d7b16 | 2020-08-24 15:56:16 +0200 | [diff] [blame] | 300 | if BoolDefault(opt.Proguard_compatibility, true) { |
| 301 | r8Flags = append(r8Flags, "--force-proguard-compatibility") |
Ian Zerny | fc7df61 | 2021-11-02 15:37:06 +0100 | [diff] [blame] | 302 | } else { |
| 303 | // TODO(b/213833843): Allow configuration of the prefix via a build variable. |
| 304 | var sourceFilePrefix = "go/retraceme " |
| 305 | var sourceFileTemplate = "\"" + sourceFilePrefix + "%MAP_ID\"" |
| 306 | // TODO(b/200967150): Also tag the source file in compat builds. |
| 307 | if Bool(opt.Optimize) || Bool(opt.Obfuscate) { |
| 308 | r8Flags = append(r8Flags, "--map-id-template", "%MAP_HASH") |
| 309 | r8Flags = append(r8Flags, "--source-file-template", sourceFileTemplate) |
| 310 | } |
Christoffer Quist Adamsen | f2d7b16 | 2020-08-24 15:56:16 +0200 | [diff] [blame] | 311 | } |
| 312 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 313 | // TODO(ccross): Don't shrink app instrumentation tests by default. |
| 314 | if !Bool(opt.Shrink) { |
| 315 | r8Flags = append(r8Flags, "-dontshrink") |
| 316 | } |
| 317 | |
| 318 | if !Bool(opt.Optimize) { |
| 319 | r8Flags = append(r8Flags, "-dontoptimize") |
| 320 | } |
| 321 | |
| 322 | // TODO(ccross): error if obufscation + app instrumentation test. |
| 323 | if !Bool(opt.Obfuscate) { |
| 324 | r8Flags = append(r8Flags, "-dontobfuscate") |
| 325 | } |
Colin Cross | 4b964c0 | 2018-10-15 16:18:06 -0700 | [diff] [blame] | 326 | // TODO(ccross): if this is an instrumentation test of an obfuscated app, use the |
| 327 | // dictionary of the app and move the app from libraryjars to injars. |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 328 | |
Jaewoong Jung | 1d6eb68 | 2018-11-29 15:08:44 -0800 | [diff] [blame] | 329 | // Don't strip out debug information for eng builds. |
| 330 | if ctx.Config().Eng() { |
| 331 | r8Flags = append(r8Flags, "--debug") |
| 332 | } |
| 333 | |
Christoffer Quist Adamsen | e850737 | 2021-02-22 09:44:09 +0100 | [diff] [blame] | 334 | // TODO(b/180878971): missing classes should be added to the relevant builds. |
Ajinkya Chalke | ddad41b | 2023-02-09 14:09:58 +0000 | [diff] [blame] | 335 | // TODO(b/229727645): do not use true as default for Android platform builds. |
| 336 | if proptools.BoolDefault(opt.Ignore_warnings, true) { |
Remi NGUYEN VAN | bdad314 | 2022-08-04 13:19:03 +0900 | [diff] [blame] | 337 | r8Flags = append(r8Flags, "-ignorewarnings") |
| 338 | } |
Christoffer Quist Adamsen | e850737 | 2021-02-22 09:44:09 +0100 | [diff] [blame] | 339 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 340 | return r8Flags, r8Deps |
| 341 | } |
| 342 | |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame] | 343 | type compileDexParams struct { |
| 344 | flags javaBuilderFlags |
| 345 | sdkVersion android.SdkSpec |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 346 | minSdkVersion android.ApiLevel |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame] | 347 | classesJar android.Path |
| 348 | jarName string |
| 349 | } |
| 350 | |
| 351 | func (d *dexer) compileDex(ctx android.ModuleContext, dexParams *compileDexParams) android.OutputPath { |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 352 | |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 353 | // Compile classes.jar into classes.dex and then javalib.jar |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame] | 354 | javalibJar := android.PathForModuleOut(ctx, "dex", dexParams.jarName).OutputPath |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 355 | outDir := android.PathForModuleOut(ctx, "dex") |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame] | 356 | tmpJar := android.PathForModuleOut(ctx, "withres-withoutdex", dexParams.jarName) |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 357 | |
Sasha Smundak | d3cf4ee | 2019-02-15 10:14:23 -0800 | [diff] [blame] | 358 | zipFlags := "--ignore_missing_files" |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 359 | if proptools.Bool(d.dexProperties.Uncompress_dex) { |
Sasha Smundak | d3cf4ee | 2019-02-15 10:14:23 -0800 | [diff] [blame] | 360 | zipFlags += " -L 0" |
Colin Cross | 5a0dcd5 | 2018-10-05 14:20:06 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame] | 363 | commonFlags, commonDeps := d.dexCommonFlags(ctx, dexParams) |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 364 | |
Wei Li | 1e73c65 | 2021-12-06 13:35:11 -0800 | [diff] [blame] | 365 | // Exclude kotlinc generated files when "exclude_kotlinc_generated_files" is set to true. |
| 366 | mergeZipsFlags := "" |
| 367 | if proptools.BoolDefault(d.dexProperties.Exclude_kotlinc_generated_files, false) { |
| 368 | mergeZipsFlags = "-stripFile META-INF/*.kotlin_module -stripFile **/*.kotlin_builtins" |
| 369 | } |
| 370 | |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 371 | useR8 := d.effectiveOptimizeEnabled() |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 372 | if useR8 { |
Colin Cross | c0c664c | 2018-05-16 16:47:21 -0700 | [diff] [blame] | 373 | proguardDictionary := android.PathForModuleOut(ctx, "proguard_dictionary") |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 374 | d.proguardDictionary = android.OptionalPathForPath(proguardDictionary) |
Ian Zerny | 82044f1 | 2023-01-25 12:11:27 +0100 | [diff] [blame] | 375 | proguardConfiguration := android.PathForModuleOut(ctx, "proguard_configuration") |
| 376 | d.proguardConfiguration = android.OptionalPathForPath(proguardConfiguration) |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 377 | proguardUsageDir := android.PathForModuleOut(ctx, "proguard_usage") |
| 378 | proguardUsage := proguardUsageDir.Join(ctx, ctx.Namespace().Path, |
| 379 | android.ModuleNameWithPossibleOverride(ctx), "unused.txt") |
| 380 | proguardUsageZip := android.PathForModuleOut(ctx, "proguard_usage.zip") |
| 381 | d.proguardUsageZip = android.OptionalPathForPath(proguardUsageZip) |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame] | 382 | r8Flags, r8Deps := d.r8Flags(ctx, dexParams.flags) |
Colin Cross | 5ea963e | 2021-09-16 19:13:43 -0700 | [diff] [blame] | 383 | r8Deps = append(r8Deps, commonDeps...) |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 384 | rule := r8 |
| 385 | args := map[string]string{ |
Wei Li | 1e73c65 | 2021-12-06 13:35:11 -0800 | [diff] [blame] | 386 | "r8Flags": strings.Join(append(commonFlags, r8Flags...), " "), |
| 387 | "zipFlags": zipFlags, |
| 388 | "outDict": proguardDictionary.String(), |
Ian Zerny | 82044f1 | 2023-01-25 12:11:27 +0100 | [diff] [blame] | 389 | "outConfig": proguardConfiguration.String(), |
Wei Li | 1e73c65 | 2021-12-06 13:35:11 -0800 | [diff] [blame] | 390 | "outUsageDir": proguardUsageDir.String(), |
| 391 | "outUsage": proguardUsage.String(), |
| 392 | "outUsageZip": proguardUsageZip.String(), |
| 393 | "outDir": outDir.String(), |
| 394 | "tmpJar": tmpJar.String(), |
| 395 | "mergeZipsFlags": mergeZipsFlags, |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 396 | } |
Ramy Medhat | 16f23a4 | 2020-09-03 01:29:49 -0400 | [diff] [blame] | 397 | if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_R8") { |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 398 | rule = r8RE |
| 399 | args["implicits"] = strings.Join(r8Deps.Strings(), ",") |
| 400 | } |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 401 | ctx.Build(pctx, android.BuildParams{ |
Rico Wind | b565f2f | 2023-05-12 12:30:07 +0200 | [diff] [blame] | 402 | Rule: rule, |
| 403 | Description: "r8", |
| 404 | Output: javalibJar, |
| 405 | ImplicitOutputs: android.WritablePaths{ |
| 406 | proguardDictionary, |
| 407 | proguardUsageZip, |
| 408 | proguardConfiguration}, |
| 409 | Input: dexParams.classesJar, |
| 410 | Implicits: r8Deps, |
| 411 | Args: args, |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 412 | }) |
| 413 | } else { |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame] | 414 | d8Flags, d8Deps := d8Flags(dexParams.flags) |
Colin Cross | 5ea963e | 2021-09-16 19:13:43 -0700 | [diff] [blame] | 415 | d8Deps = append(d8Deps, commonDeps...) |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 416 | rule := d8 |
Ramy Medhat | 16f23a4 | 2020-09-03 01:29:49 -0400 | [diff] [blame] | 417 | if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_D8") { |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 418 | rule = d8RE |
| 419 | } |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 420 | ctx.Build(pctx, android.BuildParams{ |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 421 | Rule: rule, |
Colin Cross | bafb897 | 2018-06-06 21:46:32 +0000 | [diff] [blame] | 422 | Description: "d8", |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 423 | Output: javalibJar, |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame] | 424 | Input: dexParams.classesJar, |
Colin Cross | 6dab9bd | 2018-09-28 08:06:24 -0700 | [diff] [blame] | 425 | Implicits: d8Deps, |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 426 | Args: map[string]string{ |
Wei Li | 1e73c65 | 2021-12-06 13:35:11 -0800 | [diff] [blame] | 427 | "d8Flags": strings.Join(append(commonFlags, d8Flags...), " "), |
| 428 | "zipFlags": zipFlags, |
| 429 | "outDir": outDir.String(), |
| 430 | "tmpJar": tmpJar.String(), |
| 431 | "mergeZipsFlags": mergeZipsFlags, |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 432 | }, |
| 433 | }) |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 434 | } |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 435 | if proptools.Bool(d.dexProperties.Uncompress_dex) { |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame] | 436 | alignedJavalibJar := android.PathForModuleOut(ctx, "aligned", dexParams.jarName).OutputPath |
Nicolas Geoffray | 65fd8ba | 2019-01-21 23:20:23 +0000 | [diff] [blame] | 437 | TransformZipAlign(ctx, alignedJavalibJar, javalibJar) |
| 438 | javalibJar = alignedJavalibJar |
| 439 | } |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 440 | |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 441 | return javalibJar |
| 442 | } |