summaryrefslogtreecommitdiff
path: root/java/java.go
diff options
context:
space:
mode:
Diffstat (limited to 'java/java.go')
-rw-r--r--java/java.go137
1 files changed, 39 insertions, 98 deletions
diff --git a/java/java.go b/java/java.go
index 8159af850..24debacd8 100644
--- a/java/java.go
+++ b/java/java.go
@@ -191,6 +191,32 @@ type CompilerDeviceProperties struct {
Profile *string
}
+ Optimize struct {
+ // If false, disable all optimization. Defaults to true for apps, false for
+ // libraries and tests.
+ Enabled *bool
+
+ // 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
+
+ // Flags to pass to proguard.
+ Proguard_flags []string
+
+ // Specifies the locations of files containing proguard flags.
+ Proguard_flags_files []string
+ }
+
// When targeting 1.9, override the modules to use with --system
System_modules *string
}
@@ -216,6 +242,9 @@ type Module struct {
// output file containing uninstrumented classes that will be instrumented by jacoco
jacocoReportClassesFile android.Path
+ // output file containing mapping of obfuscated names
+ proguardDictionary android.Path
+
// output file suitable for installing or running
outputFile android.Path
@@ -229,6 +258,9 @@ type Module struct {
// list of .java files and srcjars that was passed to javac
compiledJavaSrcs android.Paths
compiledSrcJars android.Paths
+
+ // list of extra progurad flag files
+ extraProguardFlagFiles android.Paths
}
func (j *Module) Srcs() android.Paths {
@@ -260,6 +292,7 @@ var (
systemModulesTag = dependencyTag{name: "system modules"}
frameworkResTag = dependencyTag{name: "framework-res"}
kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
+ proguardRaiseTag = dependencyTag{name: "proguard-raise"}
)
type sdkDep struct {
@@ -377,6 +410,10 @@ func (j *Module) deps(ctx android.BottomUpMutatorContext) {
ctx.AddDependency(ctx.Module(), systemModulesTag, sdkDep.systemModules)
}
ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.module)
+ if Bool(j.deviceProperties.Optimize.Enabled) {
+ ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultBootclasspathLibraries...)
+ ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultLibraries...)
+ }
}
} else if j.deviceProperties.System_modules == nil {
ctx.PropertyErrorf("no_standard_libs",
@@ -700,7 +737,6 @@ func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path
// Store the list of .java files that was passed to javac
j.compiledJavaSrcs = uniqueSrcFiles
j.compiledSrcJars = srcJars
- fullD8 := ctx.Config().UseD8Desugar()
enable_sharding := false
if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
@@ -829,10 +865,6 @@ func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path
j.headerJarFile = j.implementationJarFile
}
- if !fullD8 && ctx.Device() && j.installable() {
- outputFile = j.desugar(ctx, flags, outputFile, jarName)
- }
-
if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
j.properties.Instrument = true
@@ -844,11 +876,7 @@ func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path
}
if ctx.Device() && j.installable() {
- if fullD8 {
- outputFile = j.compileDexFullD8(ctx, flags, outputFile, jarName)
- } else {
- outputFile = j.compileDex(ctx, flags, outputFile, jarName)
- }
+ outputFile = j.compileDex(ctx, flags, outputFile, jarName)
if ctx.Failed() {
return
}
@@ -896,33 +924,12 @@ func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars
return headerJar
}
-func (j *Module) desugar(ctx android.ModuleContext, flags javaBuilderFlags,
- classesJar android.Path, jarName string) android.Path {
-
- desugarFlags := []string{
- "--min_sdk_version " + j.minSdkVersionNumber(ctx),
- "--desugar_try_with_resources_if_needed=false",
- "--allow_empty_bootclasspath",
- }
-
- if inList("--core-library", j.deviceProperties.Dxflags) {
- desugarFlags = append(desugarFlags, "--core_library")
- }
-
- flags.desugarFlags = strings.Join(desugarFlags, " ")
-
- desugarJar := android.PathForModuleOut(ctx, "desugar", jarName)
- TransformDesugar(ctx, desugarJar, classesJar, flags)
-
- return desugarJar
-}
-
func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
classesJar android.Path, jarName string) android.Path {
specs := j.jacocoModuleToZipCommand(ctx)
- jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco", "jacoco-report-classes.jar")
+ jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
@@ -932,72 +939,6 @@ func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
return instrumentedJar
}
-func (j *Module) compileDex(ctx android.ModuleContext, flags javaBuilderFlags,
- classesJar android.Path, jarName string) android.Path {
-
- dxFlags := j.deviceProperties.Dxflags
-
- if ctx.Config().Getenv("NO_OPTIMIZE_DX") != "" {
- dxFlags = append(dxFlags, "--no-optimize")
- }
-
- if ctx.Config().Getenv("GENERATE_DEX_DEBUG") != "" {
- dxFlags = append(dxFlags,
- "--debug",
- "--verbose",
- "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
- "--dump-width=1000")
- }
-
- dxFlags = append(dxFlags, "--min-sdk-version="+j.minSdkVersionNumber(ctx))
-
- flags.dxFlags = strings.Join(dxFlags, " ")
-
- // Compile classes.jar into classes.dex and then javalib.jar
- javalibJar := android.PathForModuleOut(ctx, "dex", jarName)
- TransformClassesJarToDexJar(ctx, javalibJar, classesJar, flags)
-
- j.dexJarFile = javalibJar
- return javalibJar
-}
-
-func (j *Module) compileDexFullD8(ctx android.ModuleContext, flags javaBuilderFlags,
- classesJar android.Path, jarName string) android.Path {
-
- // Translate all the DX flags to D8 ones until all the build files have been migrated
- // to D8 flags. See: b/69377755
- var dxFlags []string
- for _, x := range j.deviceProperties.Dxflags {
- switch x {
- case "--core-library", "--dex", "--multi-dex":
- continue
- default:
- dxFlags = append(dxFlags, x)
- }
- }
-
- if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
- dxFlags = append(dxFlags, "--debug")
- }
-
- if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
- dxFlags = append(dxFlags,
- "--debug",
- "--verbose")
- }
-
- dxFlags = append(dxFlags, "--min-api "+j.minSdkVersionNumber(ctx))
-
- flags.dxFlags = strings.Join(dxFlags, " ")
-
- // Compile classes.jar into classes.dex and then javalib.jar
- javalibJar := android.PathForModuleOut(ctx, "dex", jarName)
- TransformClassesJarToDexJar(ctx, javalibJar, classesJar, flags)
-
- j.dexJarFile = javalibJar
- return javalibJar
-}
-
// Returns a sdk version as a string that is guaranteed to be a parseable as a number. For
// modules targeting an unreleased SDK (meaning it does not yet have a number) it returns "10000".
func (j *Module) minSdkVersionNumber(ctx android.ModuleContext) string {