| 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 ( | 
|  | 18 | "strings" | 
|  | 19 |  | 
|  | 20 | "github.com/google/blueprint" | 
|  | 21 |  | 
|  | 22 | "android/soong/android" | 
|  | 23 | ) | 
|  | 24 |  | 
| Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 25 | var d8 = pctx.AndroidStaticRule("d8", | 
|  | 26 | blueprint.RuleParams{ | 
|  | 27 | Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` + | 
|  | 28 | `${config.D8Cmd} --output $outDir $dxFlags $in && ` + | 
|  | 29 | `${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -D $outDir && ` + | 
| Colin Cross | 4c03f68 | 2018-07-15 08:16:31 -0700 | [diff] [blame] | 30 | `${config.MergeZipsCmd} -D -stripFile "**/*.class" $out $outDir/classes.dex.jar $in`, | 
| Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 31 | CommandDeps: []string{ | 
|  | 32 | "${config.D8Cmd}", | 
|  | 33 | "${config.SoongZipCmd}", | 
|  | 34 | "${config.MergeZipsCmd}", | 
|  | 35 | }, | 
|  | 36 | }, | 
|  | 37 | "outDir", "dxFlags") | 
|  | 38 |  | 
| Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 39 | var r8 = pctx.AndroidStaticRule("r8", | 
|  | 40 | blueprint.RuleParams{ | 
|  | 41 | Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` + | 
| Søren Gjesse | 0e84935 | 2018-08-22 16:16:23 +0200 | [diff] [blame] | 42 | `rm -f "$outDict" && ` + | 
| Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 43 | `${config.R8Cmd} -injars $in --output $outDir ` + | 
|  | 44 | `--force-proguard-compatibility ` + | 
|  | 45 | `-printmapping $outDict ` + | 
|  | 46 | `$dxFlags $r8Flags && ` + | 
| Søren Gjesse | 0e84935 | 2018-08-22 16:16:23 +0200 | [diff] [blame] | 47 | `touch "$outDict" && ` + | 
| Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 48 | `${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -D $outDir && ` + | 
| Colin Cross | 4c03f68 | 2018-07-15 08:16:31 -0700 | [diff] [blame] | 49 | `${config.MergeZipsCmd} -D -stripFile "**/*.class" $out $outDir/classes.dex.jar $in`, | 
| Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 50 | CommandDeps: []string{ | 
|  | 51 | "${config.R8Cmd}", | 
|  | 52 | "${config.SoongZipCmd}", | 
|  | 53 | "${config.MergeZipsCmd}", | 
|  | 54 | }, | 
|  | 55 | }, | 
|  | 56 | "outDir", "outDict", "dxFlags", "r8Flags") | 
|  | 57 |  | 
| Colin Cross | bafb897 | 2018-06-06 21:46:32 +0000 | [diff] [blame] | 58 | func (j *Module) dxFlags(ctx android.ModuleContext) []string { | 
| Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 59 | flags := j.deviceProperties.Dxflags | 
| Colin Cross | bafb897 | 2018-06-06 21:46:32 +0000 | [diff] [blame] | 60 | // Translate all the DX flags to D8 ones until all the build files have been migrated | 
|  | 61 | // to D8 flags. See: b/69377755 | 
|  | 62 | flags = android.RemoveListFromList(flags, | 
|  | 63 | []string{"--core-library", "--dex", "--multi-dex"}) | 
| Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 64 |  | 
|  | 65 | if ctx.Config().Getenv("NO_OPTIMIZE_DX") != "" { | 
| Colin Cross | bafb897 | 2018-06-06 21:46:32 +0000 | [diff] [blame] | 66 | flags = append(flags, "--debug") | 
| Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 67 | } | 
|  | 68 |  | 
|  | 69 | if ctx.Config().Getenv("GENERATE_DEX_DEBUG") != "" { | 
|  | 70 | flags = append(flags, | 
|  | 71 | "--debug", | 
|  | 72 | "--verbose") | 
| Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 73 | } | 
|  | 74 |  | 
| Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 75 | minSdkVersion, err := sdkVersionToNumberAsString(ctx, j.minSdkVersion()) | 
|  | 76 | if err != nil { | 
|  | 77 | ctx.PropertyErrorf("min_sdk_version", "%s", err) | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | flags = append(flags, "--min-api "+minSdkVersion) | 
| Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 81 | return flags | 
|  | 82 | } | 
|  | 83 |  | 
| Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 84 | func (j *Module) r8Flags(ctx android.ModuleContext, flags javaBuilderFlags) (r8Flags []string, r8Deps android.Paths) { | 
|  | 85 | opt := j.deviceProperties.Optimize | 
|  | 86 |  | 
|  | 87 | // When an app contains references to APIs that are not in the SDK specified by | 
|  | 88 | // 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] | 89 | // classes added by desugaring, we artifically raise the "SDK version" "linked" by | 
| Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 90 | // ProGuard, to | 
|  | 91 | // - suppress ProGuard warnings of referencing symbols unknown to the lower SDK version. | 
|  | 92 | // - prevent ProGuard stripping subclass in the support library that extends class added in the higher SDK version. | 
|  | 93 | // See b/20667396 | 
|  | 94 | var proguardRaiseDeps classpath | 
|  | 95 | ctx.VisitDirectDepsWithTag(proguardRaiseTag, func(dep android.Module) { | 
|  | 96 | proguardRaiseDeps = append(proguardRaiseDeps, dep.(Dependency).HeaderJars()...) | 
|  | 97 | }) | 
|  | 98 |  | 
|  | 99 | r8Flags = append(r8Flags, proguardRaiseDeps.FormJavaClassPath("-libraryjars")) | 
|  | 100 | r8Flags = append(r8Flags, flags.bootClasspath.FormJavaClassPath("-libraryjars")) | 
|  | 101 | r8Flags = append(r8Flags, flags.classpath.FormJavaClassPath("-libraryjars")) | 
|  | 102 | r8Flags = append(r8Flags, "-forceprocessing") | 
|  | 103 |  | 
|  | 104 | flagFiles := android.Paths{ | 
|  | 105 | android.PathForSource(ctx, "build/make/core/proguard.flags"), | 
|  | 106 | } | 
|  | 107 |  | 
| Colin Cross | 3144dfc | 2018-01-03 15:06:47 -0800 | [diff] [blame] | 108 | if j.shouldInstrumentStatic(ctx) { | 
|  | 109 | flagFiles = append(flagFiles, | 
|  | 110 | android.PathForSource(ctx, "build/make/core/proguard.jacoco.flags")) | 
|  | 111 | } | 
|  | 112 |  | 
| Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 113 | flagFiles = append(flagFiles, j.extraProguardFlagFiles...) | 
|  | 114 | // TODO(ccross): static android library proguard files | 
|  | 115 |  | 
| Colin Cross | bd1cef5 | 2018-08-13 10:28:18 -0700 | [diff] [blame] | 116 | flagFiles = append(flagFiles, android.PathsForModuleSrc(ctx, j.deviceProperties.Optimize.Proguard_flags_files)...) | 
|  | 117 |  | 
| Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 118 | r8Flags = append(r8Flags, android.JoinWithPrefix(flagFiles.Strings(), "-include ")) | 
|  | 119 | r8Deps = append(r8Deps, flagFiles...) | 
|  | 120 |  | 
|  | 121 | // TODO(b/70942988): This is included from build/make/core/proguard.flags | 
|  | 122 | r8Deps = append(r8Deps, android.PathForSource(ctx, | 
|  | 123 | "build/make/core/proguard_basic_keeps.flags")) | 
|  | 124 |  | 
|  | 125 | r8Flags = append(r8Flags, j.deviceProperties.Optimize.Proguard_flags...) | 
|  | 126 |  | 
|  | 127 | // TODO(ccross): Don't shrink app instrumentation tests by default. | 
|  | 128 | if !Bool(opt.Shrink) { | 
|  | 129 | r8Flags = append(r8Flags, "-dontshrink") | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | if !Bool(opt.Optimize) { | 
|  | 133 | r8Flags = append(r8Flags, "-dontoptimize") | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | // TODO(ccross): error if obufscation + app instrumentation test. | 
|  | 137 | if !Bool(opt.Obfuscate) { | 
|  | 138 | r8Flags = append(r8Flags, "-dontobfuscate") | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | return r8Flags, r8Deps | 
|  | 142 | } | 
|  | 143 |  | 
| Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 144 | func (j *Module) compileDex(ctx android.ModuleContext, flags javaBuilderFlags, | 
| Colin Cross | 3063b78 | 2018-08-15 11:19:12 -0700 | [diff] [blame] | 145 | classesJar android.Path, jarName string) android.ModuleOutPath { | 
| Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 146 |  | 
| Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 147 | useR8 := Bool(j.deviceProperties.Optimize.Enabled) | 
| Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 148 |  | 
| Colin Cross | bafb897 | 2018-06-06 21:46:32 +0000 | [diff] [blame] | 149 | dxFlags := j.dxFlags(ctx) | 
| Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 150 |  | 
|  | 151 | // Compile classes.jar into classes.dex and then javalib.jar | 
|  | 152 | javalibJar := android.PathForModuleOut(ctx, "dex", jarName) | 
|  | 153 | outDir := android.PathForModuleOut(ctx, "dex") | 
|  | 154 |  | 
| Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 155 | if useR8 { | 
|  | 156 | // TODO(ccross): if this is an instrumentation test of an obfuscated app, use the | 
|  | 157 | // dictionary of the app and move the app from libraryjars to injars. | 
| Colin Cross | c0c664c | 2018-05-16 16:47:21 -0700 | [diff] [blame] | 158 | proguardDictionary := android.PathForModuleOut(ctx, "proguard_dictionary") | 
|  | 159 | j.proguardDictionary = proguardDictionary | 
| Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 160 | r8Flags, r8Deps := j.r8Flags(ctx, flags) | 
|  | 161 | ctx.Build(pctx, android.BuildParams{ | 
| Colin Cross | c0c664c | 2018-05-16 16:47:21 -0700 | [diff] [blame] | 162 | Rule:           r8, | 
|  | 163 | Description:    "r8", | 
|  | 164 | Output:         javalibJar, | 
|  | 165 | ImplicitOutput: proguardDictionary, | 
|  | 166 | Input:          classesJar, | 
|  | 167 | Implicits:      r8Deps, | 
| Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 168 | Args: map[string]string{ | 
|  | 169 | "dxFlags": strings.Join(dxFlags, " "), | 
|  | 170 | "r8Flags": strings.Join(r8Flags, " "), | 
|  | 171 | "outDict": j.proguardDictionary.String(), | 
|  | 172 | "outDir":  outDir.String(), | 
|  | 173 | }, | 
|  | 174 | }) | 
|  | 175 | } else { | 
| Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 176 | ctx.Build(pctx, android.BuildParams{ | 
| Colin Cross | bafb897 | 2018-06-06 21:46:32 +0000 | [diff] [blame] | 177 | Rule:        d8, | 
|  | 178 | Description: "d8", | 
| Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 179 | Output:      javalibJar, | 
|  | 180 | Input:       classesJar, | 
|  | 181 | Args: map[string]string{ | 
|  | 182 | "dxFlags": strings.Join(dxFlags, " "), | 
|  | 183 | "outDir":  outDir.String(), | 
|  | 184 | }, | 
|  | 185 | }) | 
| Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 186 | } | 
| Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 187 |  | 
| Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 188 | return javalibJar | 
|  | 189 | } |