From d6c31d274eaf09aa4603f36bd8f3f77782e16dd1 Mon Sep 17 00:00:00 2001 From: Liz Kammer Date: Wed, 5 Aug 2020 15:40:41 -0700 Subject: [PATCH] Add compile_dex for java_import Test: go java tests Test: m Bug: 160455085 Change-Id: I2db95dfe565e78b630007adc3360a58614d0127e --- java/java.go | 88 ++++++++++++++++++++++++++++++++++++++--------- java/java_test.go | 19 ++++++++-- 2 files changed, 88 insertions(+), 19 deletions(-) diff --git a/java/java.go b/java/java.go index 27e425dc1..3606f773e 100644 --- a/java/java.go +++ b/java/java.go @@ -679,25 +679,29 @@ func (j *Module) AvailableFor(what string) bool { return j.ApexModuleBase.AvailableFor(what) } +func sdkDeps(ctx android.BottomUpMutatorContext, sdkContext sdkContext, d dexer) { + sdkDep := decodeSdkDep(ctx, sdkContext) + if sdkDep.useModule { + ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...) + ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...) + ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...) + if d.effectiveOptimizeEnabled() && sdkDep.hasStandardLibs() { + ctx.AddVariationDependencies(nil, proguardRaiseTag, config.LegacyCorePlatformBootclasspathLibraries...) + } + if d.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() { + ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...) + } + } + if sdkDep.systemModules != "" { + ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules) + } +} + func (j *Module) deps(ctx android.BottomUpMutatorContext) { if ctx.Device() { j.linter.deps(ctx) - sdkDep := decodeSdkDep(ctx, sdkContext(j)) - if sdkDep.useModule { - ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...) - ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...) - ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...) - if j.effectiveOptimizeEnabled() && sdkDep.hasStandardLibs() { - ctx.AddVariationDependencies(nil, proguardRaiseTag, config.LegacyCorePlatformBootclasspathLibraries...) - } - if j.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() { - ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...) - } - } - if sdkDep.systemModules != "" { - ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules) - } + sdkDeps(ctx, sdkContext(j), j.dexer) } syspropPublicStubs := syspropPublicStubs(ctx.Config()) @@ -2209,6 +2213,7 @@ type JavaTestImport struct { prebuiltTestProperties prebuiltTestProperties testConfig android.Path + dexJarFile android.Path } func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) { @@ -2531,8 +2536,14 @@ type Import struct { // Functionality common to Module and Import. embeddableInModuleAndImport + hiddenAPI + dexer + properties ImportProperties + // output file containing classes.dex and resources + dexJarFile android.Path + combinedClasspathFile android.Path exportedSdkLibs dexpreopt.LibraryPaths exportAidlIncludeDirs android.Paths @@ -2546,10 +2557,18 @@ func (j *Import) makeSdkVersion() string { return j.sdkVersion().raw } +func (j *Import) systemModules() string { + return "none" +} + func (j *Import) minSdkVersion() sdkSpec { return j.sdkVersion() } +func (j *Import) targetSdkVersion() sdkSpec { + return j.sdkVersion() +} + func (j *Import) MinSdkVersion() string { return j.minSdkVersion().version.String() } @@ -2576,6 +2595,10 @@ func (a *Import) JacocoReportClassesFile() android.Path { func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) { ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...) + + if ctx.Device() && Bool(j.dexProperties.Compile_dex) { + sdkDeps(ctx, sdkContext(j), j.dexer) + } } func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) { @@ -2593,6 +2616,8 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) { j.combinedClasspathFile = outputFile j.exportedSdkLibs = make(dexpreopt.LibraryPaths) + var flags javaBuilderFlags + ctx.VisitDirectDeps(func(module android.Module) { otherName := ctx.OtherModuleName(module) tag := ctx.OtherModuleDependencyTag(module) @@ -2601,12 +2626,16 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) { case Dependency: switch tag { case libTag, staticLibTag: + flags.classpath = append(flags.classpath, dep.HeaderJars()...) // sdk lib names from dependencies are re-exported j.exportedSdkLibs.AddLibraryPaths(dep.ExportedSdkLibs()) + case bootClasspathTag: + flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars()...) } case SdkLibraryDependency: switch tag { case libTag: + flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...) // names of sdk libs that are directly depended are exported j.exportedSdkLibs.AddLibraryPath(ctx, otherName, dep.DexJarBuildPath(), dep.DexJarInstallPath()) } @@ -2626,6 +2655,26 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) { j.exportedSdkLibs.MaybeAddLibraryPath(ctx, j.OptionalImplicitSdkLibrary(), outputFile, installFile) j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs) + + if ctx.Device() && Bool(j.dexProperties.Compile_dex) { + sdkDep := decodeSdkDep(ctx, sdkContext(j)) + if sdkDep.invalidVersion { + ctx.AddMissingDependencies(sdkDep.bootclasspath) + ctx.AddMissingDependencies(sdkDep.java9Classpath) + } else if sdkDep.useFiles { + // sdkDep.jar is actually equivalent to turbine header.jar. + flags.classpath = append(flags.classpath, sdkDep.jars...) + } + + // Dex compilation + var dexOutputFile android.ModuleOutPath + dexOutputFile = j.dexer.compileDex(ctx, flags, j.minSdkVersion(), outputFile, jarName) + if ctx.Failed() { + return + } + + j.dexJarFile = dexOutputFile + } } var _ Dependency = (*Import)(nil) @@ -2656,7 +2705,7 @@ func (j *Import) ImplementationAndResourcesJars() android.Paths { } func (j *Import) DexJarBuildPath() android.Path { - return nil + return j.dexJarFile } func (j *Import) DexJarInstallPath() android.Path { @@ -2724,10 +2773,15 @@ var _ android.PrebuiltInterface = (*Import)(nil) func ImportFactory() android.Module { module := &Import{} - module.AddProperties(&module.properties) + module.AddProperties( + &module.properties, + &module.dexer.dexProperties, + ) module.initModuleAndImport(&module.ModuleBase) + module.dexProperties.Optimize.EnabledByDefault = false + android.InitPrebuiltModule(module, &module.properties.Jars) android.InitApexModule(module) android.InitSdkAwareModule(module) diff --git a/java/java_test.go b/java/java_test.go index d75be1817..9e6357792 100644 --- a/java/java_test.go +++ b/java/java_test.go @@ -525,6 +525,8 @@ func TestPrebuilts(t *testing.T) { java_import { name: "baz", jars: ["b.jar"], + sdk_version: "current", + compile_dex: true, } dex_import { @@ -555,8 +557,10 @@ func TestPrebuilts(t *testing.T) { fooModule := ctx.ModuleForTests("foo", "android_common") javac := fooModule.Rule("javac") combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") - barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output - bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output + barModule := ctx.ModuleForTests("bar", "android_common") + barJar := barModule.Rule("combineJar").Output + bazModule := ctx.ModuleForTests("baz", "android_common") + bazJar := bazModule.Rule("combineJar").Output sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output fooLibrary := fooModule.Module().(*Library) @@ -571,6 +575,11 @@ func TestPrebuilts(t *testing.T) { t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String()) } + barDexJar := barModule.Module().(*Import).DexJarBuildPath() + if barDexJar != nil { + t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar) + } + if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) { t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String()) } @@ -579,6 +588,12 @@ func TestPrebuilts(t *testing.T) { t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String()) } + bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().String() + expectedDexJar := buildDir + "/.intermediates/baz/android_common/dex/baz.jar" + if bazDexJar != expectedDexJar { + t.Errorf("baz dex jar build path expected %q, got %q", expectedDexJar, bazDexJar) + } + ctx.ModuleForTests("qux", "android_common").Rule("Cp") }