blob: 913eee6ad437fd8d14dc185a0a4d5513e9def769 [file] [log] [blame]
Colin Crossf0056cb2017-12-22 15:56:08 -08001// 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
15package java
16
17import (
18 "strings"
19
20 "github.com/google/blueprint"
21
22 "android/soong/android"
23)
24
Colin Crossf0056cb2017-12-22 15:56:08 -080025var d8 = pctx.AndroidStaticRule("d8",
26 blueprint.RuleParams{
27 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Colin Crossffb657e2018-09-21 12:29:22 -070028 `${config.D8Cmd} --output $outDir $d8Flags $in && ` +
Colin Cross5a0dcd52018-10-05 14:20:06 -070029 `${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` +
Colin Cross4c03f682018-07-15 08:16:31 -070030 `${config.MergeZipsCmd} -D -stripFile "**/*.class" $out $outDir/classes.dex.jar $in`,
Colin Crossf0056cb2017-12-22 15:56:08 -080031 CommandDeps: []string{
32 "${config.D8Cmd}",
33 "${config.SoongZipCmd}",
34 "${config.MergeZipsCmd}",
35 },
36 },
Colin Cross5a0dcd52018-10-05 14:20:06 -070037 "outDir", "d8Flags", "zipFlags")
Colin Crossf0056cb2017-12-22 15:56:08 -080038
Colin Cross66dbc0b2017-12-28 12:23:20 -080039var r8 = pctx.AndroidStaticRule("r8",
40 blueprint.RuleParams{
41 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Søren Gjesse0e849352018-08-22 16:16:23 +020042 `rm -f "$outDict" && ` +
Colin Cross66dbc0b2017-12-28 12:23:20 -080043 `${config.R8Cmd} -injars $in --output $outDir ` +
44 `--force-proguard-compatibility ` +
Søren Gjesse24f17022018-09-14 15:20:42 +020045 `--no-data-resources ` +
Colin Cross66dbc0b2017-12-28 12:23:20 -080046 `-printmapping $outDict ` +
Colin Crossffb657e2018-09-21 12:29:22 -070047 `$r8Flags && ` +
Søren Gjesse0e849352018-08-22 16:16:23 +020048 `touch "$outDict" && ` +
Colin Cross5a0dcd52018-10-05 14:20:06 -070049 `${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` +
Colin Cross4c03f682018-07-15 08:16:31 -070050 `${config.MergeZipsCmd} -D -stripFile "**/*.class" $out $outDir/classes.dex.jar $in`,
Colin Cross66dbc0b2017-12-28 12:23:20 -080051 CommandDeps: []string{
52 "${config.R8Cmd}",
53 "${config.SoongZipCmd}",
54 "${config.MergeZipsCmd}",
55 },
56 },
Colin Cross5a0dcd52018-10-05 14:20:06 -070057 "outDir", "outDict", "r8Flags", "zipFlags")
Colin Cross66dbc0b2017-12-28 12:23:20 -080058
Colin Crossffb657e2018-09-21 12:29:22 -070059func (j *Module) dexCommonFlags(ctx android.ModuleContext) []string {
Colin Crossf0056cb2017-12-22 15:56:08 -080060 flags := j.deviceProperties.Dxflags
Colin Crossbafb8972018-06-06 21:46:32 +000061 // Translate all the DX flags to D8 ones until all the build files have been migrated
62 // to D8 flags. See: b/69377755
63 flags = android.RemoveListFromList(flags,
64 []string{"--core-library", "--dex", "--multi-dex"})
Colin Crossf0056cb2017-12-22 15:56:08 -080065
66 if ctx.Config().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Crossbafb8972018-06-06 21:46:32 +000067 flags = append(flags, "--debug")
Colin Crossf0056cb2017-12-22 15:56:08 -080068 }
69
70 if ctx.Config().Getenv("GENERATE_DEX_DEBUG") != "" {
71 flags = append(flags,
72 "--debug",
73 "--verbose")
Colin Crossf0056cb2017-12-22 15:56:08 -080074 }
75
Colin Cross83bb3162018-06-25 15:48:06 -070076 minSdkVersion, err := sdkVersionToNumberAsString(ctx, j.minSdkVersion())
77 if err != nil {
78 ctx.PropertyErrorf("min_sdk_version", "%s", err)
79 }
80
81 flags = append(flags, "--min-api "+minSdkVersion)
Colin Crossf0056cb2017-12-22 15:56:08 -080082 return flags
83}
84
Colin Cross6dab9bd2018-09-28 08:06:24 -070085func (j *Module) d8Flags(ctx android.ModuleContext, flags javaBuilderFlags) ([]string, android.Paths) {
Colin Crossffb657e2018-09-21 12:29:22 -070086 d8Flags := j.dexCommonFlags(ctx)
87
Colin Crossafbb1732019-01-17 15:42:52 -080088 d8Flags = append(d8Flags, flags.bootClasspath.FormTurbineClasspath("--lib ")...)
89 d8Flags = append(d8Flags, flags.classpath.FormTurbineClasspath("--lib ")...)
Colin Crossffb657e2018-09-21 12:29:22 -070090
Colin Cross6dab9bd2018-09-28 08:06:24 -070091 var d8Deps android.Paths
92 d8Deps = append(d8Deps, flags.bootClasspath...)
93 d8Deps = append(d8Deps, flags.classpath...)
94
95 return d8Flags, d8Deps
Colin Crossffb657e2018-09-21 12:29:22 -070096}
97
Colin Cross66dbc0b2017-12-28 12:23:20 -080098func (j *Module) r8Flags(ctx android.ModuleContext, flags javaBuilderFlags) (r8Flags []string, r8Deps android.Paths) {
99 opt := j.deviceProperties.Optimize
100
101 // When an app contains references to APIs that are not in the SDK specified by
102 // its LOCAL_SDK_VERSION for example added by support library or by runtime
Colin Crossbafb8972018-06-06 21:46:32 +0000103 // classes added by desugaring, we artifically raise the "SDK version" "linked" by
Colin Cross66dbc0b2017-12-28 12:23:20 -0800104 // ProGuard, to
105 // - suppress ProGuard warnings of referencing symbols unknown to the lower SDK version.
106 // - prevent ProGuard stripping subclass in the support library that extends class added in the higher SDK version.
107 // See b/20667396
108 var proguardRaiseDeps classpath
109 ctx.VisitDirectDepsWithTag(proguardRaiseTag, func(dep android.Module) {
110 proguardRaiseDeps = append(proguardRaiseDeps, dep.(Dependency).HeaderJars()...)
111 })
112
Colin Crossffb657e2018-09-21 12:29:22 -0700113 r8Flags = append(r8Flags, j.dexCommonFlags(ctx)...)
114
Colin Cross66dbc0b2017-12-28 12:23:20 -0800115 r8Flags = append(r8Flags, proguardRaiseDeps.FormJavaClassPath("-libraryjars"))
116 r8Flags = append(r8Flags, flags.bootClasspath.FormJavaClassPath("-libraryjars"))
117 r8Flags = append(r8Flags, flags.classpath.FormJavaClassPath("-libraryjars"))
118 r8Flags = append(r8Flags, "-forceprocessing")
119
Colin Cross6dab9bd2018-09-28 08:06:24 -0700120 r8Deps = append(r8Deps, proguardRaiseDeps...)
121 r8Deps = append(r8Deps, flags.bootClasspath...)
122 r8Deps = append(r8Deps, flags.classpath...)
123
Colin Cross66dbc0b2017-12-28 12:23:20 -0800124 flagFiles := android.Paths{
125 android.PathForSource(ctx, "build/make/core/proguard.flags"),
126 }
127
Colin Cross3144dfc2018-01-03 15:06:47 -0800128 if j.shouldInstrumentStatic(ctx) {
129 flagFiles = append(flagFiles,
130 android.PathForSource(ctx, "build/make/core/proguard.jacoco.flags"))
131 }
132
Colin Cross66dbc0b2017-12-28 12:23:20 -0800133 flagFiles = append(flagFiles, j.extraProguardFlagFiles...)
134 // TODO(ccross): static android library proguard files
135
Colin Crossbd1cef52018-08-13 10:28:18 -0700136 flagFiles = append(flagFiles, android.PathsForModuleSrc(ctx, j.deviceProperties.Optimize.Proguard_flags_files)...)
137
Colin Cross66dbc0b2017-12-28 12:23:20 -0800138 r8Flags = append(r8Flags, android.JoinWithPrefix(flagFiles.Strings(), "-include "))
139 r8Deps = append(r8Deps, flagFiles...)
140
141 // TODO(b/70942988): This is included from build/make/core/proguard.flags
142 r8Deps = append(r8Deps, android.PathForSource(ctx,
143 "build/make/core/proguard_basic_keeps.flags"))
144
145 r8Flags = append(r8Flags, j.deviceProperties.Optimize.Proguard_flags...)
146
147 // TODO(ccross): Don't shrink app instrumentation tests by default.
148 if !Bool(opt.Shrink) {
149 r8Flags = append(r8Flags, "-dontshrink")
150 }
151
152 if !Bool(opt.Optimize) {
153 r8Flags = append(r8Flags, "-dontoptimize")
154 }
155
156 // TODO(ccross): error if obufscation + app instrumentation test.
157 if !Bool(opt.Obfuscate) {
158 r8Flags = append(r8Flags, "-dontobfuscate")
159 }
Colin Cross4b964c02018-10-15 16:18:06 -0700160 // TODO(ccross): if this is an instrumentation test of an obfuscated app, use the
161 // dictionary of the app and move the app from libraryjars to injars.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800162
Jaewoong Jung1d6eb682018-11-29 15:08:44 -0800163 // Don't strip out debug information for eng builds.
164 if ctx.Config().Eng() {
165 r8Flags = append(r8Flags, "--debug")
166 }
167
Colin Cross66dbc0b2017-12-28 12:23:20 -0800168 return r8Flags, r8Deps
169}
170
Colin Crossf0056cb2017-12-22 15:56:08 -0800171func (j *Module) compileDex(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -0700172 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crossf0056cb2017-12-22 15:56:08 -0800173
Colin Cross66dbc0b2017-12-28 12:23:20 -0800174 useR8 := Bool(j.deviceProperties.Optimize.Enabled)
Colin Crossf0056cb2017-12-22 15:56:08 -0800175
Colin Crossf0056cb2017-12-22 15:56:08 -0800176 // Compile classes.jar into classes.dex and then javalib.jar
177 javalibJar := android.PathForModuleOut(ctx, "dex", jarName)
178 outDir := android.PathForModuleOut(ctx, "dex")
179
Colin Cross5a0dcd52018-10-05 14:20:06 -0700180 zipFlags := ""
181 if j.deviceProperties.UncompressDex {
182 zipFlags = "-L 0"
183 }
184
Colin Cross66dbc0b2017-12-28 12:23:20 -0800185 if useR8 {
Colin Crossc0c664c2018-05-16 16:47:21 -0700186 proguardDictionary := android.PathForModuleOut(ctx, "proguard_dictionary")
187 j.proguardDictionary = proguardDictionary
Colin Cross66dbc0b2017-12-28 12:23:20 -0800188 r8Flags, r8Deps := j.r8Flags(ctx, flags)
189 ctx.Build(pctx, android.BuildParams{
Colin Crossc0c664c2018-05-16 16:47:21 -0700190 Rule: r8,
191 Description: "r8",
192 Output: javalibJar,
193 ImplicitOutput: proguardDictionary,
194 Input: classesJar,
195 Implicits: r8Deps,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800196 Args: map[string]string{
Colin Cross5a0dcd52018-10-05 14:20:06 -0700197 "r8Flags": strings.Join(r8Flags, " "),
198 "zipFlags": zipFlags,
199 "outDict": j.proguardDictionary.String(),
200 "outDir": outDir.String(),
Colin Cross66dbc0b2017-12-28 12:23:20 -0800201 },
202 })
203 } else {
Colin Cross6dab9bd2018-09-28 08:06:24 -0700204 d8Flags, d8Deps := j.d8Flags(ctx, flags)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800205 ctx.Build(pctx, android.BuildParams{
Colin Crossbafb8972018-06-06 21:46:32 +0000206 Rule: d8,
207 Description: "d8",
Colin Cross66dbc0b2017-12-28 12:23:20 -0800208 Output: javalibJar,
209 Input: classesJar,
Colin Cross6dab9bd2018-09-28 08:06:24 -0700210 Implicits: d8Deps,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800211 Args: map[string]string{
Colin Cross5a0dcd52018-10-05 14:20:06 -0700212 "d8Flags": strings.Join(d8Flags, " "),
213 "zipFlags": zipFlags,
214 "outDir": outDir.String(),
Colin Cross66dbc0b2017-12-28 12:23:20 -0800215 },
216 })
Colin Crossf0056cb2017-12-22 15:56:08 -0800217 }
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000218 if j.deviceProperties.UncompressDex {
219 alignedJavalibJar := android.PathForModuleOut(ctx, "aligned", jarName)
220 TransformZipAlign(ctx, alignedJavalibJar, javalibJar)
221 javalibJar = alignedJavalibJar
222 }
Colin Crossf0056cb2017-12-22 15:56:08 -0800223
Colin Crossf0056cb2017-12-22 15:56:08 -0800224 return javalibJar
225}