blob: 6445940374523214ac8d62203717f7a41bd96547 [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" && ` +
28 `${config.D8Cmd} --output $outDir $dxFlags $in && ` +
29 `${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -D $outDir && ` +
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 },
37 "outDir", "dxFlags")
38
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 ` +
47 `$dxFlags $r8Flags && ` +
Søren Gjesse0e849352018-08-22 16:16:23 +020048 `touch "$outDict" && ` +
Colin Cross66dbc0b2017-12-28 12:23:20 -080049 `${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -D $outDir && ` +
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 },
57 "outDir", "outDict", "dxFlags", "r8Flags")
58
Colin Crossbafb8972018-06-06 21:46:32 +000059func (j *Module) dxFlags(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 Cross66dbc0b2017-12-28 12:23:20 -080085func (j *Module) r8Flags(ctx android.ModuleContext, flags javaBuilderFlags) (r8Flags []string, r8Deps android.Paths) {
86 opt := j.deviceProperties.Optimize
87
88 // When an app contains references to APIs that are not in the SDK specified by
89 // its LOCAL_SDK_VERSION for example added by support library or by runtime
Colin Crossbafb8972018-06-06 21:46:32 +000090 // classes added by desugaring, we artifically raise the "SDK version" "linked" by
Colin Cross66dbc0b2017-12-28 12:23:20 -080091 // ProGuard, to
92 // - suppress ProGuard warnings of referencing symbols unknown to the lower SDK version.
93 // - prevent ProGuard stripping subclass in the support library that extends class added in the higher SDK version.
94 // See b/20667396
95 var proguardRaiseDeps classpath
96 ctx.VisitDirectDepsWithTag(proguardRaiseTag, func(dep android.Module) {
97 proguardRaiseDeps = append(proguardRaiseDeps, dep.(Dependency).HeaderJars()...)
98 })
99
100 r8Flags = append(r8Flags, proguardRaiseDeps.FormJavaClassPath("-libraryjars"))
101 r8Flags = append(r8Flags, flags.bootClasspath.FormJavaClassPath("-libraryjars"))
102 r8Flags = append(r8Flags, flags.classpath.FormJavaClassPath("-libraryjars"))
103 r8Flags = append(r8Flags, "-forceprocessing")
104
105 flagFiles := android.Paths{
106 android.PathForSource(ctx, "build/make/core/proguard.flags"),
107 }
108
Colin Cross3144dfc2018-01-03 15:06:47 -0800109 if j.shouldInstrumentStatic(ctx) {
110 flagFiles = append(flagFiles,
111 android.PathForSource(ctx, "build/make/core/proguard.jacoco.flags"))
112 }
113
Colin Cross66dbc0b2017-12-28 12:23:20 -0800114 flagFiles = append(flagFiles, j.extraProguardFlagFiles...)
115 // TODO(ccross): static android library proguard files
116
Colin Crossbd1cef52018-08-13 10:28:18 -0700117 flagFiles = append(flagFiles, android.PathsForModuleSrc(ctx, j.deviceProperties.Optimize.Proguard_flags_files)...)
118
Colin Cross66dbc0b2017-12-28 12:23:20 -0800119 r8Flags = append(r8Flags, android.JoinWithPrefix(flagFiles.Strings(), "-include "))
120 r8Deps = append(r8Deps, flagFiles...)
121
122 // TODO(b/70942988): This is included from build/make/core/proguard.flags
123 r8Deps = append(r8Deps, android.PathForSource(ctx,
124 "build/make/core/proguard_basic_keeps.flags"))
125
126 r8Flags = append(r8Flags, j.deviceProperties.Optimize.Proguard_flags...)
127
128 // TODO(ccross): Don't shrink app instrumentation tests by default.
129 if !Bool(opt.Shrink) {
130 r8Flags = append(r8Flags, "-dontshrink")
131 }
132
133 if !Bool(opt.Optimize) {
134 r8Flags = append(r8Flags, "-dontoptimize")
135 }
136
137 // TODO(ccross): error if obufscation + app instrumentation test.
138 if !Bool(opt.Obfuscate) {
139 r8Flags = append(r8Flags, "-dontobfuscate")
140 }
141
142 return r8Flags, r8Deps
143}
144
Colin Crossf0056cb2017-12-22 15:56:08 -0800145func (j *Module) compileDex(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -0700146 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crossf0056cb2017-12-22 15:56:08 -0800147
Colin Cross66dbc0b2017-12-28 12:23:20 -0800148 useR8 := Bool(j.deviceProperties.Optimize.Enabled)
Colin Crossf0056cb2017-12-22 15:56:08 -0800149
Colin Crossbafb8972018-06-06 21:46:32 +0000150 dxFlags := j.dxFlags(ctx)
Colin Crossf0056cb2017-12-22 15:56:08 -0800151
152 // Compile classes.jar into classes.dex and then javalib.jar
153 javalibJar := android.PathForModuleOut(ctx, "dex", jarName)
154 outDir := android.PathForModuleOut(ctx, "dex")
155
Colin Cross66dbc0b2017-12-28 12:23:20 -0800156 if useR8 {
157 // TODO(ccross): if this is an instrumentation test of an obfuscated app, use the
158 // dictionary of the app and move the app from libraryjars to injars.
Colin Crossc0c664c2018-05-16 16:47:21 -0700159 proguardDictionary := android.PathForModuleOut(ctx, "proguard_dictionary")
160 j.proguardDictionary = proguardDictionary
Colin Cross66dbc0b2017-12-28 12:23:20 -0800161 r8Flags, r8Deps := j.r8Flags(ctx, flags)
162 ctx.Build(pctx, android.BuildParams{
Colin Crossc0c664c2018-05-16 16:47:21 -0700163 Rule: r8,
164 Description: "r8",
165 Output: javalibJar,
166 ImplicitOutput: proguardDictionary,
167 Input: classesJar,
168 Implicits: r8Deps,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800169 Args: map[string]string{
170 "dxFlags": strings.Join(dxFlags, " "),
171 "r8Flags": strings.Join(r8Flags, " "),
172 "outDict": j.proguardDictionary.String(),
173 "outDir": outDir.String(),
174 },
175 })
176 } else {
Colin Cross66dbc0b2017-12-28 12:23:20 -0800177 ctx.Build(pctx, android.BuildParams{
Colin Crossbafb8972018-06-06 21:46:32 +0000178 Rule: d8,
179 Description: "d8",
Colin Cross66dbc0b2017-12-28 12:23:20 -0800180 Output: javalibJar,
181 Input: classesJar,
182 Args: map[string]string{
183 "dxFlags": strings.Join(dxFlags, " "),
184 "outDir": outDir.String(),
185 },
186 })
Colin Crossf0056cb2017-12-22 15:56:08 -0800187 }
Colin Crossf0056cb2017-12-22 15:56:08 -0800188
Colin Crossf0056cb2017-12-22 15:56:08 -0800189 return javalibJar
190}