diff options
Diffstat (limited to 'java/java.go')
| -rw-r--r-- | java/java.go | 160 |
1 files changed, 62 insertions, 98 deletions
diff --git a/java/java.go b/java/java.go index dbf202a00..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", @@ -597,6 +634,29 @@ func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaB // classpath flags.bootClasspath.AddPaths(deps.bootClasspath) flags.classpath.AddPaths(deps.classpath) + + if len(flags.bootClasspath) == 0 && ctx.Host() && !ctx.Config().TargetOpenJDK9() && + !Bool(j.properties.No_standard_libs) && + inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) { + // Give host-side tools a version of OpenJDK's standard libraries + // close to what they're targeting. As of Dec 2017, AOSP is only + // bundling OpenJDK 8 and 9, so nothing < 8 is available. + // + // When building with OpenJDK 8, the following should have no + // effect since those jars would be available by default. + // + // When building with OpenJDK 9 but targeting a version < 1.8, + // putting them on the bootclasspath means that: + // a) code can't (accidentally) refer to OpenJDK 9 specific APIs + // b) references to existing APIs are not reinterpreted in an + // OpenJDK 9-specific way, eg. calls to subclasses of + // java.nio.Buffer as in http://b/70862583 + java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME") + flags.bootClasspath = append(flags.bootClasspath, + android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"), + android.PathForSource(ctx, java8Home, "jre/lib/rt.jar")) + } + // systemModules if deps.systemModules != nil { flags.systemModules = append(flags.systemModules, deps.systemModules) @@ -677,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") { @@ -806,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 @@ -821,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 } @@ -873,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) @@ -909,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 { |