2017-12-23 00:56:08 +01:00
|
|
|
// Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package java
|
|
|
|
|
|
|
|
import (
|
2021-03-31 11:17:53 +02:00
|
|
|
"strconv"
|
2017-12-23 00:56:08 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
2020-05-20 23:20:28 +02:00
|
|
|
"github.com/google/blueprint/proptools"
|
2017-12-23 00:56:08 +01:00
|
|
|
|
|
|
|
"android/soong/android"
|
2020-04-22 03:36:23 +02:00
|
|
|
"android/soong/remoteexec"
|
2017-12-23 00:56:08 +01:00
|
|
|
)
|
|
|
|
|
2020-07-10 00:16:41 +02:00
|
|
|
type DexProperties struct {
|
|
|
|
// If set to true, compile dex regardless of installable. Defaults to false.
|
|
|
|
Compile_dex *bool
|
|
|
|
|
|
|
|
// list of module-specific flags that will be used for dex compiles
|
|
|
|
Dxflags []string `android:"arch_variant"`
|
|
|
|
|
2021-09-17 04:13:43 +02:00
|
|
|
// A list of files containing rules that specify the classes to keep in the main dex file.
|
|
|
|
Main_dex_rules []string `android:"path"`
|
|
|
|
|
2020-07-10 00:16:41 +02:00
|
|
|
Optimize struct {
|
2022-06-02 21:11:14 +02:00
|
|
|
// If false, disable all optimization. Defaults to true for android_app and
|
|
|
|
// android_test_helper_app modules, false for android_test, java_library, and java_test modules.
|
2020-07-10 00:16:41 +02:00
|
|
|
Enabled *bool
|
|
|
|
// True if the module containing this has it set by default.
|
|
|
|
EnabledByDefault bool `blueprint:"mutated"`
|
|
|
|
|
2023-02-09 15:09:58 +01:00
|
|
|
// Whether to continue building even if warnings are emitted. Defaults to true.
|
2022-08-04 06:19:03 +02:00
|
|
|
Ignore_warnings *bool
|
|
|
|
|
2023-08-29 19:07:20 +02:00
|
|
|
// If true, runs R8 in Proguard compatibility mode, otherwise runs R8 in full mode.
|
|
|
|
// Defaults to false for apps, true for libraries and tests.
|
2020-08-24 15:56:16 +02:00
|
|
|
Proguard_compatibility *bool
|
|
|
|
|
2020-07-10 00:16:41 +02:00
|
|
|
// If true, optimize for size by removing unused code. Defaults to true for apps,
|
|
|
|
// false for libraries and tests.
|
|
|
|
Shrink *bool
|
|
|
|
|
|
|
|
// If true, optimize bytecode. Defaults to false.
|
|
|
|
Optimize *bool
|
|
|
|
|
|
|
|
// If true, obfuscate bytecode. Defaults to false.
|
|
|
|
Obfuscate *bool
|
|
|
|
|
|
|
|
// If true, do not use the flag files generated by aapt that automatically keep
|
|
|
|
// classes referenced by the app manifest. Defaults to false.
|
|
|
|
No_aapt_flags *bool
|
|
|
|
|
2022-09-28 01:53:11 +02:00
|
|
|
// If true, optimize for size by removing unused resources. Defaults to false.
|
2022-09-22 10:41:42 +02:00
|
|
|
Shrink_resources *bool
|
|
|
|
|
2020-07-10 00:16:41 +02:00
|
|
|
// Flags to pass to proguard.
|
|
|
|
Proguard_flags []string
|
|
|
|
|
|
|
|
// Specifies the locations of files containing proguard flags.
|
|
|
|
Proguard_flags_files []string `android:"path"`
|
2023-08-03 00:00:35 +02:00
|
|
|
|
|
|
|
// If true, transitive reverse dependencies of this module will have this
|
|
|
|
// module's proguard spec appended to their optimization action
|
|
|
|
Export_proguard_flags_files *bool
|
2020-07-10 00:16:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Keep the data uncompressed. We always need uncompressed dex for execution,
|
|
|
|
// so this might actually save space by avoiding storing the same data twice.
|
|
|
|
// This defaults to reasonable value based on module and should not be set.
|
|
|
|
// It exists only to support ART tests.
|
|
|
|
Uncompress_dex *bool
|
2021-12-06 22:35:11 +01:00
|
|
|
|
2022-01-12 22:22:55 +01:00
|
|
|
// Exclude kotlinc generate files: *.kotlin_module, *.kotlin_builtins. Defaults to false.
|
2021-12-06 22:35:11 +01:00
|
|
|
Exclude_kotlinc_generated_files *bool
|
2020-07-10 00:16:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type dexer struct {
|
|
|
|
dexProperties DexProperties
|
|
|
|
|
|
|
|
// list of extra proguard flag files
|
|
|
|
extraProguardFlagFiles android.Paths
|
|
|
|
proguardDictionary android.OptionalPath
|
2023-01-25 12:11:27 +01:00
|
|
|
proguardConfiguration android.OptionalPath
|
2020-08-15 02:39:29 +02:00
|
|
|
proguardUsageZip android.OptionalPath
|
2022-11-29 17:19:37 +01:00
|
|
|
|
|
|
|
providesTransitiveHeaderJars
|
2020-07-10 00:16:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dexer) effectiveOptimizeEnabled() bool {
|
|
|
|
return BoolDefault(d.dexProperties.Optimize.Enabled, d.dexProperties.Optimize.EnabledByDefault)
|
|
|
|
}
|
|
|
|
|
2021-03-12 20:28:25 +01:00
|
|
|
var d8, d8RE = pctx.MultiCommandRemoteStaticRules("d8",
|
2017-12-23 00:56:08 +01:00
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
|
2023-10-10 12:22:39 +02:00
|
|
|
`$d8Template${config.D8Cmd} ${config.D8Flags} --output $outDir $d8Flags --no-dex-input-jar $in && ` +
|
2020-05-20 20:27:16 +02:00
|
|
|
`$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` +
|
2023-09-28 23:38:06 +02:00
|
|
|
`${config.MergeZipsCmd} -D -stripFile "**/*.class" $mergeZipsFlags $out $outDir/classes.dex.jar $in && ` +
|
2023-10-10 12:22:39 +02:00
|
|
|
`rm -f "$outDir/classes*.dex" "$outDir/classes.dex.jar"`,
|
2017-12-23 00:56:08 +01:00
|
|
|
CommandDeps: []string{
|
|
|
|
"${config.D8Cmd}",
|
|
|
|
"${config.SoongZipCmd}",
|
|
|
|
"${config.MergeZipsCmd}",
|
|
|
|
},
|
2020-05-20 20:27:16 +02:00
|
|
|
}, map[string]*remoteexec.REParams{
|
|
|
|
"$d8Template": &remoteexec.REParams{
|
|
|
|
Labels: map[string]string{"type": "compile", "compiler": "d8"},
|
|
|
|
Inputs: []string{"${config.D8Jar}"},
|
|
|
|
ExecStrategy: "${config.RED8ExecStrategy}",
|
|
|
|
ToolchainInputs: []string{"${config.JavaCmd}"},
|
|
|
|
Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
|
|
|
|
},
|
|
|
|
"$zipTemplate": &remoteexec.REParams{
|
|
|
|
Labels: map[string]string{"type": "tool", "name": "soong_zip"},
|
|
|
|
Inputs: []string{"${config.SoongZipCmd}", "$outDir"},
|
|
|
|
OutputFiles: []string{"$outDir/classes.dex.jar"},
|
|
|
|
ExecStrategy: "${config.RED8ExecStrategy}",
|
|
|
|
Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
|
|
|
|
},
|
2023-10-10 12:22:39 +02:00
|
|
|
}, []string{"outDir", "d8Flags", "zipFlags", "mergeZipsFlags"}, nil)
|
2020-04-22 03:36:23 +02:00
|
|
|
|
2021-03-12 20:28:25 +01:00
|
|
|
var r8, r8RE = pctx.MultiCommandRemoteStaticRules("r8",
|
2017-12-28 21:23:20 +01:00
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
|
2023-01-25 12:11:27 +01:00
|
|
|
`rm -f "$outDict" && rm -f "$outConfig" && rm -rf "${outUsageDir}" && ` +
|
2020-08-15 02:39:29 +02:00
|
|
|
`mkdir -p $$(dirname ${outUsage}) && ` +
|
2023-10-03 20:24:06 +02:00
|
|
|
`$r8Template${config.R8Cmd} ${config.R8Flags} -injars $in --output $outDir ` +
|
2018-09-14 15:20:42 +02:00
|
|
|
`--no-data-resources ` +
|
2020-08-15 02:39:29 +02:00
|
|
|
`-printmapping ${outDict} ` +
|
2023-05-05 22:40:20 +02:00
|
|
|
`-printconfiguration ${outConfig} ` +
|
2020-08-15 02:39:29 +02:00
|
|
|
`-printusage ${outUsage} ` +
|
2022-03-21 20:19:44 +01:00
|
|
|
`--deps-file ${out}.d ` +
|
2018-09-21 21:29:22 +02:00
|
|
|
`$r8Flags && ` +
|
2023-01-25 12:11:27 +01:00
|
|
|
`touch "${outDict}" "${outConfig}" "${outUsage}" && ` +
|
2020-08-15 02:39:29 +02:00
|
|
|
`${config.SoongZipCmd} -o ${outUsageZip} -C ${outUsageDir} -f ${outUsage} && ` +
|
|
|
|
`rm -rf ${outUsageDir} && ` +
|
2020-05-20 20:27:16 +02:00
|
|
|
`$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` +
|
2023-09-28 23:38:06 +02:00
|
|
|
`${config.MergeZipsCmd} -D -stripFile "**/*.class" $mergeZipsFlags $out $outDir/classes.dex.jar $in && ` +
|
2023-10-03 18:40:06 +02:00
|
|
|
`rm -f "$outDir/classes*.dex" "$outDir/classes.dex.jar"`,
|
2022-03-21 20:19:44 +01:00
|
|
|
Depfile: "${out}.d",
|
|
|
|
Deps: blueprint.DepsGCC,
|
2017-12-28 21:23:20 +01:00
|
|
|
CommandDeps: []string{
|
2021-08-06 01:56:18 +02:00
|
|
|
"${config.R8Cmd}",
|
2017-12-28 21:23:20 +01:00
|
|
|
"${config.SoongZipCmd}",
|
|
|
|
"${config.MergeZipsCmd}",
|
|
|
|
},
|
2020-05-20 20:27:16 +02:00
|
|
|
}, map[string]*remoteexec.REParams{
|
|
|
|
"$r8Template": &remoteexec.REParams{
|
|
|
|
Labels: map[string]string{"type": "compile", "compiler": "r8"},
|
|
|
|
Inputs: []string{"$implicits", "${config.R8Jar}"},
|
2023-11-03 11:26:38 +01:00
|
|
|
OutputFiles: []string{"${outUsage}", "${outConfig}", "${outDict}"},
|
2020-05-20 20:27:16 +02:00
|
|
|
ExecStrategy: "${config.RER8ExecStrategy}",
|
|
|
|
ToolchainInputs: []string{"${config.JavaCmd}"},
|
|
|
|
Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
|
|
|
|
},
|
|
|
|
"$zipTemplate": &remoteexec.REParams{
|
|
|
|
Labels: map[string]string{"type": "tool", "name": "soong_zip"},
|
|
|
|
Inputs: []string{"${config.SoongZipCmd}", "$outDir"},
|
|
|
|
OutputFiles: []string{"$outDir/classes.dex.jar"},
|
|
|
|
ExecStrategy: "${config.RER8ExecStrategy}",
|
|
|
|
Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
|
|
|
|
},
|
2020-08-15 02:39:29 +02:00
|
|
|
"$zipUsageTemplate": &remoteexec.REParams{
|
|
|
|
Labels: map[string]string{"type": "tool", "name": "soong_zip"},
|
|
|
|
Inputs: []string{"${config.SoongZipCmd}", "${outUsage}"},
|
|
|
|
OutputFiles: []string{"${outUsageZip}"},
|
|
|
|
ExecStrategy: "${config.RER8ExecStrategy}",
|
|
|
|
Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
|
|
|
|
},
|
2023-01-25 12:11:27 +01:00
|
|
|
}, []string{"outDir", "outDict", "outConfig", "outUsage", "outUsageZip", "outUsageDir",
|
2023-11-03 11:26:38 +01:00
|
|
|
"r8Flags", "zipFlags", "mergeZipsFlags"}, []string{"implicits"})
|
2017-12-28 21:23:20 +01:00
|
|
|
|
2021-09-17 04:13:43 +02:00
|
|
|
func (d *dexer) dexCommonFlags(ctx android.ModuleContext,
|
2023-02-23 19:05:05 +01:00
|
|
|
dexParams *compileDexParams) (flags []string, deps android.Paths) {
|
2021-09-17 04:13:43 +02:00
|
|
|
|
|
|
|
flags = d.dexProperties.Dxflags
|
2018-06-06 23:46:32 +02:00
|
|
|
// Translate all the DX flags to D8 ones until all the build files have been migrated
|
|
|
|
// to D8 flags. See: b/69377755
|
|
|
|
flags = android.RemoveListFromList(flags,
|
|
|
|
[]string{"--core-library", "--dex", "--multi-dex"})
|
2017-12-23 00:56:08 +01:00
|
|
|
|
2021-09-17 04:13:43 +02:00
|
|
|
for _, f := range android.PathsForModuleSrc(ctx, d.dexProperties.Main_dex_rules) {
|
|
|
|
flags = append(flags, "--main-dex-rules", f.String())
|
|
|
|
deps = append(deps, f)
|
|
|
|
}
|
|
|
|
|
2017-12-23 00:56:08 +01:00
|
|
|
if ctx.Config().Getenv("NO_OPTIMIZE_DX") != "" {
|
2018-06-06 23:46:32 +02:00
|
|
|
flags = append(flags, "--debug")
|
2017-12-23 00:56:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Config().Getenv("GENERATE_DEX_DEBUG") != "" {
|
|
|
|
flags = append(flags,
|
|
|
|
"--debug",
|
|
|
|
"--verbose")
|
|
|
|
}
|
|
|
|
|
2022-09-21 00:32:14 +02:00
|
|
|
// Supplying the platform build flag disables various features like API modeling and desugaring.
|
|
|
|
// For targets with a stable min SDK version (i.e., when the min SDK is both explicitly specified
|
|
|
|
// and managed+versioned), we suppress this flag to ensure portability.
|
|
|
|
// Note: Targets with a min SDK kind of core_platform (e.g., framework.jar) or unspecified (e.g.,
|
|
|
|
// services.jar), are not classified as stable, which is WAI.
|
|
|
|
// TODO(b/232073181): Expand to additional min SDK cases after validation.
|
2023-08-25 13:43:44 +02:00
|
|
|
var addAndroidPlatformBuildFlag = false
|
2023-02-23 19:05:05 +01:00
|
|
|
if !dexParams.sdkVersion.Stable() {
|
2023-08-25 13:43:44 +02:00
|
|
|
addAndroidPlatformBuildFlag = true
|
2022-09-21 00:32:14 +02:00
|
|
|
}
|
|
|
|
|
2023-02-23 19:05:05 +01:00
|
|
|
effectiveVersion, err := dexParams.minSdkVersion.EffectiveVersion(ctx)
|
2018-06-26 00:48:06 +02:00
|
|
|
if err != nil {
|
|
|
|
ctx.PropertyErrorf("min_sdk_version", "%s", err)
|
|
|
|
}
|
|
|
|
|
2023-08-25 13:43:44 +02:00
|
|
|
// If the specified SDK level is 10000, then configure the compiler to use the
|
|
|
|
// current platform SDK level and to compile the build as a platform build.
|
|
|
|
var minApiFlagValue = effectiveVersion.FinalOrFutureInt()
|
|
|
|
if minApiFlagValue == 10000 {
|
|
|
|
minApiFlagValue = ctx.Config().PlatformSdkVersion().FinalInt()
|
|
|
|
addAndroidPlatformBuildFlag = true
|
|
|
|
}
|
|
|
|
flags = append(flags, "--min-api "+strconv.Itoa(minApiFlagValue))
|
|
|
|
|
|
|
|
if addAndroidPlatformBuildFlag {
|
|
|
|
flags = append(flags, "--android-platform-build")
|
|
|
|
}
|
2021-09-17 04:13:43 +02:00
|
|
|
return flags, deps
|
2017-12-23 00:56:08 +01:00
|
|
|
}
|
|
|
|
|
2020-07-10 00:16:41 +02:00
|
|
|
func d8Flags(flags javaBuilderFlags) (d8Flags []string, d8Deps android.Paths) {
|
2019-10-31 23:22:57 +01:00
|
|
|
d8Flags = append(d8Flags, flags.bootClasspath.FormRepeatedClassPath("--lib ")...)
|
2022-03-17 19:12:32 +01:00
|
|
|
d8Flags = append(d8Flags, flags.dexClasspath.FormRepeatedClassPath("--lib ")...)
|
2018-09-21 21:29:22 +02:00
|
|
|
|
2018-09-28 17:06:24 +02:00
|
|
|
d8Deps = append(d8Deps, flags.bootClasspath...)
|
2022-03-17 19:12:32 +01:00
|
|
|
d8Deps = append(d8Deps, flags.dexClasspath...)
|
2018-09-28 17:06:24 +02:00
|
|
|
|
|
|
|
return d8Flags, d8Deps
|
2018-09-21 21:29:22 +02:00
|
|
|
}
|
|
|
|
|
2020-07-10 00:16:41 +02:00
|
|
|
func (d *dexer) r8Flags(ctx android.ModuleContext, flags javaBuilderFlags) (r8Flags []string, r8Deps android.Paths) {
|
|
|
|
opt := d.dexProperties.Optimize
|
2017-12-28 21:23:20 +01:00
|
|
|
|
|
|
|
// When an app contains references to APIs that are not in the SDK specified by
|
|
|
|
// its LOCAL_SDK_VERSION for example added by support library or by runtime
|
2018-06-06 23:46:32 +02:00
|
|
|
// classes added by desugaring, we artifically raise the "SDK version" "linked" by
|
2017-12-28 21:23:20 +01:00
|
|
|
// ProGuard, to
|
|
|
|
// - suppress ProGuard warnings of referencing symbols unknown to the lower SDK version.
|
|
|
|
// - prevent ProGuard stripping subclass in the support library that extends class added in the higher SDK version.
|
|
|
|
// See b/20667396
|
|
|
|
var proguardRaiseDeps classpath
|
2021-02-01 22:59:03 +01:00
|
|
|
ctx.VisitDirectDepsWithTag(proguardRaiseTag, func(m android.Module) {
|
|
|
|
dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo)
|
|
|
|
proguardRaiseDeps = append(proguardRaiseDeps, dep.HeaderJars...)
|
2017-12-28 21:23:20 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
r8Flags = append(r8Flags, proguardRaiseDeps.FormJavaClassPath("-libraryjars"))
|
2018-09-28 17:06:24 +02:00
|
|
|
r8Deps = append(r8Deps, proguardRaiseDeps...)
|
2022-11-29 17:19:37 +01:00
|
|
|
r8Flags = append(r8Flags, flags.bootClasspath.FormJavaClassPath("-libraryjars"))
|
2018-09-28 17:06:24 +02:00
|
|
|
r8Deps = append(r8Deps, flags.bootClasspath...)
|
2022-11-29 17:19:37 +01:00
|
|
|
r8Flags = append(r8Flags, flags.dexClasspath.FormJavaClassPath("-libraryjars"))
|
2022-03-17 19:12:32 +01:00
|
|
|
r8Deps = append(r8Deps, flags.dexClasspath...)
|
2022-11-29 17:19:37 +01:00
|
|
|
|
|
|
|
transitiveStaticLibsLookupMap := map[android.Path]bool{}
|
|
|
|
if d.transitiveStaticLibsHeaderJars != nil {
|
|
|
|
for _, jar := range d.transitiveStaticLibsHeaderJars.ToList() {
|
|
|
|
transitiveStaticLibsLookupMap[jar] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
transitiveHeaderJars := android.Paths{}
|
|
|
|
if d.transitiveLibsHeaderJars != nil {
|
|
|
|
for _, jar := range d.transitiveLibsHeaderJars.ToList() {
|
|
|
|
if _, ok := transitiveStaticLibsLookupMap[jar]; ok {
|
|
|
|
// don't include a lib if it is already packaged in the current JAR as a static lib
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
transitiveHeaderJars = append(transitiveHeaderJars, jar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
transitiveClasspath := classpath(transitiveHeaderJars)
|
|
|
|
r8Flags = append(r8Flags, transitiveClasspath.FormJavaClassPath("-libraryjars"))
|
|
|
|
r8Deps = append(r8Deps, transitiveClasspath...)
|
2018-09-28 17:06:24 +02:00
|
|
|
|
2017-12-28 21:23:20 +01:00
|
|
|
flagFiles := android.Paths{
|
|
|
|
android.PathForSource(ctx, "build/make/core/proguard.flags"),
|
|
|
|
}
|
|
|
|
|
2020-07-10 00:16:41 +02:00
|
|
|
flagFiles = append(flagFiles, d.extraProguardFlagFiles...)
|
2017-12-28 21:23:20 +01:00
|
|
|
// TODO(ccross): static android library proguard files
|
|
|
|
|
2020-07-10 00:16:41 +02:00
|
|
|
flagFiles = append(flagFiles, android.PathsForModuleSrc(ctx, opt.Proguard_flags_files)...)
|
2018-08-13 19:28:18 +02:00
|
|
|
|
2023-10-31 18:27:02 +01:00
|
|
|
flagFiles = android.FirstUniquePaths(flagFiles)
|
|
|
|
|
2017-12-28 21:23:20 +01:00
|
|
|
r8Flags = append(r8Flags, android.JoinWithPrefix(flagFiles.Strings(), "-include "))
|
|
|
|
r8Deps = append(r8Deps, flagFiles...)
|
|
|
|
|
|
|
|
// TODO(b/70942988): This is included from build/make/core/proguard.flags
|
|
|
|
r8Deps = append(r8Deps, android.PathForSource(ctx,
|
|
|
|
"build/make/core/proguard_basic_keeps.flags"))
|
|
|
|
|
2020-07-10 00:16:41 +02:00
|
|
|
r8Flags = append(r8Flags, opt.Proguard_flags...)
|
2017-12-28 21:23:20 +01:00
|
|
|
|
2020-08-24 15:56:16 +02:00
|
|
|
if BoolDefault(opt.Proguard_compatibility, true) {
|
|
|
|
r8Flags = append(r8Flags, "--force-proguard-compatibility")
|
2023-09-15 19:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if Bool(opt.Optimize) || Bool(opt.Obfuscate) {
|
2021-11-02 15:37:06 +01:00
|
|
|
// TODO(b/213833843): Allow configuration of the prefix via a build variable.
|
|
|
|
var sourceFilePrefix = "go/retraceme "
|
|
|
|
var sourceFileTemplate = "\"" + sourceFilePrefix + "%MAP_ID\""
|
2023-09-15 19:16:36 +02:00
|
|
|
r8Flags = append(r8Flags, "--map-id-template", "%MAP_HASH")
|
|
|
|
r8Flags = append(r8Flags, "--source-file-template", sourceFileTemplate)
|
2020-08-24 15:56:16 +02:00
|
|
|
}
|
|
|
|
|
2017-12-28 21:23:20 +01:00
|
|
|
// TODO(ccross): Don't shrink app instrumentation tests by default.
|
|
|
|
if !Bool(opt.Shrink) {
|
|
|
|
r8Flags = append(r8Flags, "-dontshrink")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !Bool(opt.Optimize) {
|
|
|
|
r8Flags = append(r8Flags, "-dontoptimize")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(ccross): error if obufscation + app instrumentation test.
|
|
|
|
if !Bool(opt.Obfuscate) {
|
|
|
|
r8Flags = append(r8Flags, "-dontobfuscate")
|
|
|
|
}
|
2018-10-16 01:18:06 +02:00
|
|
|
// TODO(ccross): if this is an instrumentation test of an obfuscated app, use the
|
|
|
|
// dictionary of the app and move the app from libraryjars to injars.
|
2017-12-28 21:23:20 +01:00
|
|
|
|
2018-11-30 00:08:44 +01:00
|
|
|
// Don't strip out debug information for eng builds.
|
|
|
|
if ctx.Config().Eng() {
|
|
|
|
r8Flags = append(r8Flags, "--debug")
|
|
|
|
}
|
|
|
|
|
2021-02-22 09:44:09 +01:00
|
|
|
// TODO(b/180878971): missing classes should be added to the relevant builds.
|
2023-02-09 15:09:58 +01:00
|
|
|
// TODO(b/229727645): do not use true as default for Android platform builds.
|
|
|
|
if proptools.BoolDefault(opt.Ignore_warnings, true) {
|
2022-08-04 06:19:03 +02:00
|
|
|
r8Flags = append(r8Flags, "-ignorewarnings")
|
|
|
|
}
|
2021-02-22 09:44:09 +01:00
|
|
|
|
2017-12-28 21:23:20 +01:00
|
|
|
return r8Flags, r8Deps
|
|
|
|
}
|
|
|
|
|
2023-02-23 19:05:05 +01:00
|
|
|
type compileDexParams struct {
|
|
|
|
flags javaBuilderFlags
|
|
|
|
sdkVersion android.SdkSpec
|
2023-03-03 22:20:36 +01:00
|
|
|
minSdkVersion android.ApiLevel
|
2023-02-23 19:05:05 +01:00
|
|
|
classesJar android.Path
|
|
|
|
jarName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dexer) compileDex(ctx android.ModuleContext, dexParams *compileDexParams) android.OutputPath {
|
2017-12-23 00:56:08 +01:00
|
|
|
|
|
|
|
// Compile classes.jar into classes.dex and then javalib.jar
|
2023-02-23 19:05:05 +01:00
|
|
|
javalibJar := android.PathForModuleOut(ctx, "dex", dexParams.jarName).OutputPath
|
2017-12-23 00:56:08 +01:00
|
|
|
outDir := android.PathForModuleOut(ctx, "dex")
|
|
|
|
|
2019-02-15 19:14:23 +01:00
|
|
|
zipFlags := "--ignore_missing_files"
|
2020-07-10 00:16:41 +02:00
|
|
|
if proptools.Bool(d.dexProperties.Uncompress_dex) {
|
2019-02-15 19:14:23 +01:00
|
|
|
zipFlags += " -L 0"
|
2018-10-05 23:20:06 +02:00
|
|
|
}
|
|
|
|
|
2023-02-23 19:05:05 +01:00
|
|
|
commonFlags, commonDeps := d.dexCommonFlags(ctx, dexParams)
|
2020-07-10 00:16:41 +02:00
|
|
|
|
2021-12-06 22:35:11 +01:00
|
|
|
// Exclude kotlinc generated files when "exclude_kotlinc_generated_files" is set to true.
|
|
|
|
mergeZipsFlags := ""
|
|
|
|
if proptools.BoolDefault(d.dexProperties.Exclude_kotlinc_generated_files, false) {
|
|
|
|
mergeZipsFlags = "-stripFile META-INF/*.kotlin_module -stripFile **/*.kotlin_builtins"
|
|
|
|
}
|
|
|
|
|
2020-07-10 00:16:41 +02:00
|
|
|
useR8 := d.effectiveOptimizeEnabled()
|
2017-12-28 21:23:20 +01:00
|
|
|
if useR8 {
|
2018-05-17 01:47:21 +02:00
|
|
|
proguardDictionary := android.PathForModuleOut(ctx, "proguard_dictionary")
|
2020-07-10 00:16:41 +02:00
|
|
|
d.proguardDictionary = android.OptionalPathForPath(proguardDictionary)
|
2023-01-25 12:11:27 +01:00
|
|
|
proguardConfiguration := android.PathForModuleOut(ctx, "proguard_configuration")
|
|
|
|
d.proguardConfiguration = android.OptionalPathForPath(proguardConfiguration)
|
2020-08-15 02:39:29 +02:00
|
|
|
proguardUsageDir := android.PathForModuleOut(ctx, "proguard_usage")
|
|
|
|
proguardUsage := proguardUsageDir.Join(ctx, ctx.Namespace().Path,
|
|
|
|
android.ModuleNameWithPossibleOverride(ctx), "unused.txt")
|
|
|
|
proguardUsageZip := android.PathForModuleOut(ctx, "proguard_usage.zip")
|
|
|
|
d.proguardUsageZip = android.OptionalPathForPath(proguardUsageZip)
|
2023-02-23 19:05:05 +01:00
|
|
|
r8Flags, r8Deps := d.r8Flags(ctx, dexParams.flags)
|
2021-09-17 04:13:43 +02:00
|
|
|
r8Deps = append(r8Deps, commonDeps...)
|
2020-04-22 03:36:23 +02:00
|
|
|
rule := r8
|
|
|
|
args := map[string]string{
|
2021-12-06 22:35:11 +01:00
|
|
|
"r8Flags": strings.Join(append(commonFlags, r8Flags...), " "),
|
|
|
|
"zipFlags": zipFlags,
|
|
|
|
"outDict": proguardDictionary.String(),
|
2023-01-25 12:11:27 +01:00
|
|
|
"outConfig": proguardConfiguration.String(),
|
2021-12-06 22:35:11 +01:00
|
|
|
"outUsageDir": proguardUsageDir.String(),
|
|
|
|
"outUsage": proguardUsage.String(),
|
|
|
|
"outUsageZip": proguardUsageZip.String(),
|
|
|
|
"outDir": outDir.String(),
|
|
|
|
"mergeZipsFlags": mergeZipsFlags,
|
2020-04-22 03:36:23 +02:00
|
|
|
}
|
2020-09-03 07:29:49 +02:00
|
|
|
if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_R8") {
|
2020-04-22 03:36:23 +02:00
|
|
|
rule = r8RE
|
|
|
|
args["implicits"] = strings.Join(r8Deps.Strings(), ",")
|
|
|
|
}
|
2017-12-28 21:23:20 +01:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
2023-11-03 11:26:38 +01:00
|
|
|
Rule: rule,
|
|
|
|
Description: "r8",
|
|
|
|
Output: javalibJar,
|
|
|
|
ImplicitOutputs: android.WritablePaths{
|
|
|
|
proguardDictionary,
|
|
|
|
proguardUsageZip,
|
|
|
|
proguardConfiguration},
|
|
|
|
Input: dexParams.classesJar,
|
|
|
|
Implicits: r8Deps,
|
|
|
|
Args: args,
|
2017-12-28 21:23:20 +01:00
|
|
|
})
|
|
|
|
} else {
|
2023-02-23 19:05:05 +01:00
|
|
|
d8Flags, d8Deps := d8Flags(dexParams.flags)
|
2021-09-17 04:13:43 +02:00
|
|
|
d8Deps = append(d8Deps, commonDeps...)
|
2020-04-22 03:36:23 +02:00
|
|
|
rule := d8
|
2020-09-03 07:29:49 +02:00
|
|
|
if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_D8") {
|
2020-04-22 03:36:23 +02:00
|
|
|
rule = d8RE
|
|
|
|
}
|
2017-12-28 21:23:20 +01:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
2020-04-22 03:36:23 +02:00
|
|
|
Rule: rule,
|
2018-06-06 23:46:32 +02:00
|
|
|
Description: "d8",
|
2017-12-28 21:23:20 +01:00
|
|
|
Output: javalibJar,
|
2023-02-23 19:05:05 +01:00
|
|
|
Input: dexParams.classesJar,
|
2018-09-28 17:06:24 +02:00
|
|
|
Implicits: d8Deps,
|
2017-12-28 21:23:20 +01:00
|
|
|
Args: map[string]string{
|
2021-12-06 22:35:11 +01:00
|
|
|
"d8Flags": strings.Join(append(commonFlags, d8Flags...), " "),
|
|
|
|
"zipFlags": zipFlags,
|
|
|
|
"outDir": outDir.String(),
|
|
|
|
"mergeZipsFlags": mergeZipsFlags,
|
2017-12-28 21:23:20 +01:00
|
|
|
},
|
|
|
|
})
|
2017-12-23 00:56:08 +01:00
|
|
|
}
|
2020-07-10 00:16:41 +02:00
|
|
|
if proptools.Bool(d.dexProperties.Uncompress_dex) {
|
2023-02-23 19:05:05 +01:00
|
|
|
alignedJavalibJar := android.PathForModuleOut(ctx, "aligned", dexParams.jarName).OutputPath
|
2023-09-07 07:31:32 +02:00
|
|
|
TransformZipAlign(ctx, alignedJavalibJar, javalibJar, nil)
|
2019-01-22 00:20:23 +01:00
|
|
|
javalibJar = alignedJavalibJar
|
|
|
|
}
|
2017-12-23 00:56:08 +01:00
|
|
|
|
|
|
|
return javalibJar
|
|
|
|
}
|