blob: 8363a7dbb7a8ee05f2ff5fb3afcf0057b176b71d [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" && ` +
42 `${config.R8Cmd} -injars $in --output $outDir ` +
43 `--force-proguard-compatibility ` +
44 `-printmapping $outDict ` +
45 `$dxFlags $r8Flags && ` +
46 `${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -D $outDir && ` +
Colin Cross4c03f682018-07-15 08:16:31 -070047 `${config.MergeZipsCmd} -D -stripFile "**/*.class" $out $outDir/classes.dex.jar $in`,
Colin Cross66dbc0b2017-12-28 12:23:20 -080048 CommandDeps: []string{
49 "${config.R8Cmd}",
50 "${config.SoongZipCmd}",
51 "${config.MergeZipsCmd}",
52 },
53 },
54 "outDir", "outDict", "dxFlags", "r8Flags")
55
Colin Crossbafb8972018-06-06 21:46:32 +000056func (j *Module) dxFlags(ctx android.ModuleContext) []string {
Colin Crossf0056cb2017-12-22 15:56:08 -080057 flags := j.deviceProperties.Dxflags
Colin Crossbafb8972018-06-06 21:46:32 +000058 // Translate all the DX flags to D8 ones until all the build files have been migrated
59 // to D8 flags. See: b/69377755
60 flags = android.RemoveListFromList(flags,
61 []string{"--core-library", "--dex", "--multi-dex"})
Colin Crossf0056cb2017-12-22 15:56:08 -080062
63 if ctx.Config().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Crossbafb8972018-06-06 21:46:32 +000064 flags = append(flags, "--debug")
Colin Crossf0056cb2017-12-22 15:56:08 -080065 }
66
67 if ctx.Config().Getenv("GENERATE_DEX_DEBUG") != "" {
68 flags = append(flags,
69 "--debug",
70 "--verbose")
Colin Crossf0056cb2017-12-22 15:56:08 -080071 }
72
Colin Cross83bb3162018-06-25 15:48:06 -070073 minSdkVersion, err := sdkVersionToNumberAsString(ctx, j.minSdkVersion())
74 if err != nil {
75 ctx.PropertyErrorf("min_sdk_version", "%s", err)
76 }
77
78 flags = append(flags, "--min-api "+minSdkVersion)
Colin Crossf0056cb2017-12-22 15:56:08 -080079 return flags
80}
81
Colin Cross66dbc0b2017-12-28 12:23:20 -080082func (j *Module) r8Flags(ctx android.ModuleContext, flags javaBuilderFlags) (r8Flags []string, r8Deps android.Paths) {
83 opt := j.deviceProperties.Optimize
84
85 // When an app contains references to APIs that are not in the SDK specified by
86 // its LOCAL_SDK_VERSION for example added by support library or by runtime
Colin Crossbafb8972018-06-06 21:46:32 +000087 // classes added by desugaring, we artifically raise the "SDK version" "linked" by
Colin Cross66dbc0b2017-12-28 12:23:20 -080088 // ProGuard, to
89 // - suppress ProGuard warnings of referencing symbols unknown to the lower SDK version.
90 // - prevent ProGuard stripping subclass in the support library that extends class added in the higher SDK version.
91 // See b/20667396
92 var proguardRaiseDeps classpath
93 ctx.VisitDirectDepsWithTag(proguardRaiseTag, func(dep android.Module) {
94 proguardRaiseDeps = append(proguardRaiseDeps, dep.(Dependency).HeaderJars()...)
95 })
96
97 r8Flags = append(r8Flags, proguardRaiseDeps.FormJavaClassPath("-libraryjars"))
98 r8Flags = append(r8Flags, flags.bootClasspath.FormJavaClassPath("-libraryjars"))
99 r8Flags = append(r8Flags, flags.classpath.FormJavaClassPath("-libraryjars"))
100 r8Flags = append(r8Flags, "-forceprocessing")
101
102 flagFiles := android.Paths{
103 android.PathForSource(ctx, "build/make/core/proguard.flags"),
104 }
105
Colin Cross3144dfc2018-01-03 15:06:47 -0800106 if j.shouldInstrumentStatic(ctx) {
107 flagFiles = append(flagFiles,
108 android.PathForSource(ctx, "build/make/core/proguard.jacoco.flags"))
109 }
110
Colin Cross66dbc0b2017-12-28 12:23:20 -0800111 flagFiles = append(flagFiles, j.extraProguardFlagFiles...)
112 // TODO(ccross): static android library proguard files
113
Colin Crossbd1cef52018-08-13 10:28:18 -0700114 flagFiles = append(flagFiles, android.PathsForModuleSrc(ctx, j.deviceProperties.Optimize.Proguard_flags_files)...)
115
Colin Cross66dbc0b2017-12-28 12:23:20 -0800116 r8Flags = append(r8Flags, android.JoinWithPrefix(flagFiles.Strings(), "-include "))
117 r8Deps = append(r8Deps, flagFiles...)
118
119 // TODO(b/70942988): This is included from build/make/core/proguard.flags
120 r8Deps = append(r8Deps, android.PathForSource(ctx,
121 "build/make/core/proguard_basic_keeps.flags"))
122
123 r8Flags = append(r8Flags, j.deviceProperties.Optimize.Proguard_flags...)
124
125 // TODO(ccross): Don't shrink app instrumentation tests by default.
126 if !Bool(opt.Shrink) {
127 r8Flags = append(r8Flags, "-dontshrink")
128 }
129
130 if !Bool(opt.Optimize) {
131 r8Flags = append(r8Flags, "-dontoptimize")
132 }
133
134 // TODO(ccross): error if obufscation + app instrumentation test.
135 if !Bool(opt.Obfuscate) {
136 r8Flags = append(r8Flags, "-dontobfuscate")
137 }
138
139 return r8Flags, r8Deps
140}
141
Colin Crossf0056cb2017-12-22 15:56:08 -0800142func (j *Module) compileDex(ctx android.ModuleContext, flags javaBuilderFlags,
143 classesJar android.Path, jarName string) android.Path {
144
Colin Cross66dbc0b2017-12-28 12:23:20 -0800145 useR8 := Bool(j.deviceProperties.Optimize.Enabled)
Colin Crossf0056cb2017-12-22 15:56:08 -0800146
Colin Crossbafb8972018-06-06 21:46:32 +0000147 dxFlags := j.dxFlags(ctx)
Colin Crossf0056cb2017-12-22 15:56:08 -0800148
149 // Compile classes.jar into classes.dex and then javalib.jar
150 javalibJar := android.PathForModuleOut(ctx, "dex", jarName)
151 outDir := android.PathForModuleOut(ctx, "dex")
152
Colin Cross66dbc0b2017-12-28 12:23:20 -0800153 if useR8 {
154 // TODO(ccross): if this is an instrumentation test of an obfuscated app, use the
155 // dictionary of the app and move the app from libraryjars to injars.
Colin Crossc0c664c2018-05-16 16:47:21 -0700156 proguardDictionary := android.PathForModuleOut(ctx, "proguard_dictionary")
157 j.proguardDictionary = proguardDictionary
Colin Cross66dbc0b2017-12-28 12:23:20 -0800158 r8Flags, r8Deps := j.r8Flags(ctx, flags)
159 ctx.Build(pctx, android.BuildParams{
Colin Crossc0c664c2018-05-16 16:47:21 -0700160 Rule: r8,
161 Description: "r8",
162 Output: javalibJar,
163 ImplicitOutput: proguardDictionary,
164 Input: classesJar,
165 Implicits: r8Deps,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800166 Args: map[string]string{
167 "dxFlags": strings.Join(dxFlags, " "),
168 "r8Flags": strings.Join(r8Flags, " "),
169 "outDict": j.proguardDictionary.String(),
170 "outDir": outDir.String(),
171 },
172 })
173 } else {
Colin Cross66dbc0b2017-12-28 12:23:20 -0800174 ctx.Build(pctx, android.BuildParams{
Colin Crossbafb8972018-06-06 21:46:32 +0000175 Rule: d8,
176 Description: "d8",
Colin Cross66dbc0b2017-12-28 12:23:20 -0800177 Output: javalibJar,
178 Input: classesJar,
179 Args: map[string]string{
180 "dxFlags": strings.Join(dxFlags, " "),
181 "outDir": outDir.String(),
182 },
183 })
Colin Crossf0056cb2017-12-22 15:56:08 -0800184 }
Colin Crossf0056cb2017-12-22 15:56:08 -0800185
186 j.dexJarFile = javalibJar
187 return javalibJar
188}