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