summaryrefslogtreecommitdiff
path: root/java
diff options
context:
space:
mode:
Diffstat (limited to 'java')
-rw-r--r--java/aar.go2
-rw-r--r--java/android_manifest.go2
-rwxr-xr-xjava/app.go30
-rw-r--r--java/app_test.go157
-rw-r--r--java/java.go96
-rw-r--r--java/java_test.go19
-rw-r--r--java/prebuilt_apis.go14
-rw-r--r--java/sdk.go28
-rw-r--r--java/sdk_library.go47
-rw-r--r--java/testing.go3
10 files changed, 324 insertions, 74 deletions
diff --git a/java/aar.go b/java/aar.go
index 5c19a9cd3..0f5e30deb 100644
--- a/java/aar.go
+++ b/java/aar.go
@@ -390,7 +390,7 @@ func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext) (transitiveStati
// (including the java_sdk_library) itself then append any implicit sdk library
// names to the list of sdk libraries to be added to the manifest.
if component, ok := module.(SdkLibraryComponentDependency); ok {
- sdkLibraries.AddLibraryPath(ctx, component.OptionalImplicitSdkLibrary(),
+ sdkLibraries.MaybeAddLibraryPath(ctx, component.OptionalImplicitSdkLibrary(),
component.DexJarBuildPath(), component.DexJarInstallPath())
}
diff --git a/java/android_manifest.go b/java/android_manifest.go
index f45ebe8d5..41fcafe0c 100644
--- a/java/android_manifest.go
+++ b/java/android_manifest.go
@@ -80,7 +80,7 @@ func manifestFixer(ctx android.ModuleContext, manifest android.Path, sdkContext
args = append(args, "--use-embedded-dex")
}
- for usesLib, _ := range sdkLibraries {
+ for _, usesLib := range android.SortedStringKeys(sdkLibraries) {
if inList(usesLib, optionalUsesLibs) {
args = append(args, "--optional-uses-library", usesLib)
} else {
diff --git a/java/app.go b/java/app.go
index 5e3a9d9bf..34f96dd50 100755
--- a/java/app.go
+++ b/java/app.go
@@ -586,7 +586,7 @@ func (a *AndroidApp) installPath(ctx android.ModuleContext) android.InstallPath
return android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk")
}
-func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
+func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext, sdkLibs dexpreopt.LibraryPaths) android.Path {
a.dexpreopter.installPath = a.installPath(ctx)
if a.dexProperties.Uncompress_dex == nil {
// If the value was not force-set by the user, use reasonable default based on the module.
@@ -597,6 +597,7 @@ func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
a.dexpreopter.usesLibs = a.usesLibrary.usesLibraryProperties.Uses_libs
a.dexpreopter.optionalUsesLibs = a.usesLibrary.presentOptionalUsesLibs(ctx)
a.dexpreopter.libraryPaths = a.usesLibrary.usesLibraryPaths(ctx)
+ a.dexpreopter.libraryPaths.AddLibraryPaths(sdkLibs)
a.dexpreopter.manifestFile = a.mergedManifestFile
a.exportedSdkLibs = make(dexpreopt.LibraryPaths)
@@ -767,6 +768,15 @@ func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
// Process all building blocks, from AAPT to certificates.
a.aaptBuildActions(ctx)
+ // The decision to enforce <uses-library> checks is made before adding implicit SDK libraries.
+ a.usesLibrary.freezeEnforceUsesLibraries()
+
+ // Add implicit SDK libraries to <uses-library> list.
+ for _, usesLib := range android.SortedStringKeys(a.aapt.sdkLibraries) {
+ a.usesLibrary.addLib(usesLib, inList(usesLib, optionalUsesLibs))
+ }
+
+ // Check that the <uses-library> list is coherent with the manifest.
if a.usesLibrary.enforceUsesLibraries() {
manifestCheckFile := a.usesLibrary.verifyUsesLibrariesManifest(ctx, a.mergedManifestFile)
apkDeps = append(apkDeps, manifestCheckFile)
@@ -779,7 +789,7 @@ func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
a.linter.resources = a.aapt.resourceFiles
a.linter.buildModuleReportZip = ctx.Config().UnbundledBuildApps()
- dexJarFile := a.dexBuildActions(ctx)
+ dexJarFile := a.dexBuildActions(ctx, a.aapt.sdkLibraries)
jniLibs, certificateDeps := collectAppDeps(ctx, a, a.shouldEmbedJnis(ctx), !Bool(a.appProperties.Jni_uses_platform_apis))
jniJarFile := a.jniBuildActions(jniLibs, ctx)
@@ -1888,6 +1898,16 @@ type usesLibrary struct {
usesLibraryProperties UsesLibraryProperties
}
+func (u *usesLibrary) addLib(lib string, optional bool) {
+ if !android.InList(lib, u.usesLibraryProperties.Uses_libs) && !android.InList(lib, u.usesLibraryProperties.Optional_uses_libs) {
+ if optional {
+ u.usesLibraryProperties.Optional_uses_libs = append(u.usesLibraryProperties.Optional_uses_libs, lib)
+ } else {
+ u.usesLibraryProperties.Uses_libs = append(u.usesLibraryProperties.Uses_libs, lib)
+ }
+ }
+}
+
func (u *usesLibrary) deps(ctx android.BottomUpMutatorContext, hasFrameworkLibs bool) {
if !ctx.Config().UnbundledBuild() {
ctx.AddVariationDependencies(nil, usesLibTag, u.usesLibraryProperties.Uses_libs...)
@@ -1960,6 +1980,12 @@ func (u *usesLibrary) enforceUsesLibraries() bool {
return BoolDefault(u.usesLibraryProperties.Enforce_uses_libs, defaultEnforceUsesLibs)
}
+// Freeze the value of `enforce_uses_libs` based on the current values of `uses_libs` and `optional_uses_libs`.
+func (u *usesLibrary) freezeEnforceUsesLibraries() {
+ enforce := u.enforceUsesLibraries()
+ u.usesLibraryProperties.Enforce_uses_libs = &enforce
+}
+
// verifyUsesLibrariesManifest checks the <uses-library> tags in an AndroidManifest.xml against the ones specified
// in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the manifest.
func (u *usesLibrary) verifyUsesLibrariesManifest(ctx android.ModuleContext, manifest android.Path) android.Path {
diff --git a/java/app_test.go b/java/app_test.go
index 6b27124e4..b8d8616f3 100644
--- a/java/app_test.go
+++ b/java/app_test.go
@@ -1038,6 +1038,35 @@ func TestAndroidResources(t *testing.T) {
}
}
+func checkSdkVersion(t *testing.T, config android.Config, expectedSdkVersion string) {
+ ctx := testContext()
+
+ run(t, ctx, config)
+
+ foo := ctx.ModuleForTests("foo", "android_common")
+ link := foo.Output("package-res.apk")
+ linkFlags := strings.Split(link.Args["flags"], " ")
+ min := android.IndexList("--min-sdk-version", linkFlags)
+ target := android.IndexList("--target-sdk-version", linkFlags)
+
+ if min == -1 || target == -1 || min == len(linkFlags)-1 || target == len(linkFlags)-1 {
+ t.Fatalf("missing --min-sdk-version or --target-sdk-version in link flags: %q", linkFlags)
+ }
+
+ gotMinSdkVersion := linkFlags[min+1]
+ gotTargetSdkVersion := linkFlags[target+1]
+
+ if gotMinSdkVersion != expectedSdkVersion {
+ t.Errorf("incorrect --min-sdk-version, expected %q got %q",
+ expectedSdkVersion, gotMinSdkVersion)
+ }
+
+ if gotTargetSdkVersion != expectedSdkVersion {
+ t.Errorf("incorrect --target-sdk-version, expected %q got %q",
+ expectedSdkVersion, gotTargetSdkVersion)
+ }
+}
+
func TestAppSdkVersion(t *testing.T) {
testCases := []struct {
name string
@@ -1107,34 +1136,81 @@ func TestAppSdkVersion(t *testing.T) {
config.TestProductVariables.Platform_sdk_version = &test.platformSdkInt
config.TestProductVariables.Platform_sdk_codename = &test.platformSdkCodename
config.TestProductVariables.Platform_sdk_final = &test.platformSdkFinal
+ checkSdkVersion(t, config, test.expectedMinSdkVersion)
- ctx := testContext()
-
- run(t, ctx, config)
-
- foo := ctx.ModuleForTests("foo", "android_common")
- link := foo.Output("package-res.apk")
- linkFlags := strings.Split(link.Args["flags"], " ")
- min := android.IndexList("--min-sdk-version", linkFlags)
- target := android.IndexList("--target-sdk-version", linkFlags)
-
- if min == -1 || target == -1 || min == len(linkFlags)-1 || target == len(linkFlags)-1 {
- t.Fatalf("missing --min-sdk-version or --target-sdk-version in link flags: %q", linkFlags)
- }
-
- gotMinSdkVersion := linkFlags[min+1]
- gotTargetSdkVersion := linkFlags[target+1]
+ })
+ }
+ }
+}
- if gotMinSdkVersion != test.expectedMinSdkVersion {
- t.Errorf("incorrect --min-sdk-version, expected %q got %q",
- test.expectedMinSdkVersion, gotMinSdkVersion)
- }
+func TestVendorAppSdkVersion(t *testing.T) {
+ testCases := []struct {
+ name string
+ sdkVersion string
+ platformSdkInt int
+ platformSdkCodename string
+ platformSdkFinal bool
+ deviceCurrentApiLevelForVendorModules string
+ expectedMinSdkVersion string
+ }{
+ {
+ name: "current final SDK",
+ sdkVersion: "current",
+ platformSdkInt: 29,
+ platformSdkCodename: "REL",
+ platformSdkFinal: true,
+ deviceCurrentApiLevelForVendorModules: "29",
+ expectedMinSdkVersion: "29",
+ },
+ {
+ name: "current final SDK",
+ sdkVersion: "current",
+ platformSdkInt: 29,
+ platformSdkCodename: "REL",
+ platformSdkFinal: true,
+ deviceCurrentApiLevelForVendorModules: "28",
+ expectedMinSdkVersion: "28",
+ },
+ {
+ name: "current final SDK",
+ sdkVersion: "current",
+ platformSdkInt: 29,
+ platformSdkCodename: "Q",
+ platformSdkFinal: false,
+ deviceCurrentApiLevelForVendorModules: "current",
+ expectedMinSdkVersion: "Q",
+ },
+ {
+ name: "current final SDK",
+ sdkVersion: "current",
+ platformSdkInt: 29,
+ platformSdkCodename: "Q",
+ platformSdkFinal: false,
+ deviceCurrentApiLevelForVendorModules: "28",
+ expectedMinSdkVersion: "28",
+ },
+ }
- if gotTargetSdkVersion != test.expectedMinSdkVersion {
- t.Errorf("incorrect --target-sdk-version, expected %q got %q",
- test.expectedMinSdkVersion, gotTargetSdkVersion)
- }
- })
+ for _, moduleType := range []string{"android_app", "android_library"} {
+ for _, sdkKind := range []string{"", "system_"} {
+ for _, test := range testCases {
+ t.Run(moduleType+" "+test.name, func(t *testing.T) {
+ bp := fmt.Sprintf(`%s {
+ name: "foo",
+ srcs: ["a.java"],
+ sdk_version: "%s%s",
+ vendor: true,
+ }`, moduleType, sdkKind, test.sdkVersion)
+
+ config := testAppConfig(nil, bp, nil)
+ config.TestProductVariables.Platform_sdk_version = &test.platformSdkInt
+ config.TestProductVariables.Platform_sdk_codename = &test.platformSdkCodename
+ config.TestProductVariables.Platform_sdk_final = &test.platformSdkFinal
+ config.TestProductVariables.DeviceCurrentApiLevelForVendorModules = &test.deviceCurrentApiLevelForVendorModules
+ config.TestProductVariables.DeviceSystemSdkVersions = []string{"28", "29"}
+ checkSdkVersion(t, config, test.expectedMinSdkVersion)
+ })
+ }
}
}
}
@@ -2542,6 +2618,19 @@ func TestUsesLibraries(t *testing.T) {
android_app {
name: "app",
srcs: ["a.java"],
+ libs: ["qux", "quuz"],
+ static_libs: ["static-runtime-helper"],
+ uses_libs: ["foo"],
+ sdk_version: "current",
+ optional_uses_libs: [
+ "bar",
+ "baz",
+ ],
+ }
+
+ android_app {
+ name: "app_with_stub_deps",
+ srcs: ["a.java"],
libs: ["qux", "quuz.stubs"],
static_libs: ["static-runtime-helper"],
uses_libs: ["foo"],
@@ -2572,6 +2661,7 @@ func TestUsesLibraries(t *testing.T) {
run(t, ctx, config)
app := ctx.ModuleForTests("app", "android_common")
+ appWithStubDeps := ctx.ModuleForTests("app_with_stub_deps", "android_common")
prebuilt := ctx.ModuleForTests("prebuilt", "android_common")
// Test that implicit dependencies on java_sdk_library instances are passed to the manifest.
@@ -2602,15 +2692,24 @@ func TestUsesLibraries(t *testing.T) {
t.Errorf("wanted %q in %q", w, cmd)
}
- // Test that only present libraries are preopted
+ // Test that all present libraries are preopted, including implicit SDK dependencies
cmd = app.Rule("dexpreopt").RuleParams.Command
-
- if w := `--target-classpath-for-sdk any /system/framework/foo.jar:/system/framework/bar.jar`; !strings.Contains(cmd, w) {
+ w := `--target-classpath-for-sdk any` +
+ ` /system/framework/foo.jar` +
+ `:/system/framework/quuz.jar` +
+ `:/system/framework/qux.jar` +
+ `:/system/framework/runtime-library.jar` +
+ `:/system/framework/bar.jar`
+ if !strings.Contains(cmd, w) {
t.Errorf("wanted %q in %q", w, cmd)
}
- cmd = prebuilt.Rule("dexpreopt").RuleParams.Command
+ // TODO(skvadrik) fix dexpreopt for stub libraries for which the implementation is present
+ if appWithStubDeps.MaybeRule("dexpreopt").RuleParams.Command != "" {
+ t.Errorf("dexpreopt should be disabled for apps with dependencies on stub libraries")
+ }
+ cmd = prebuilt.Rule("dexpreopt").RuleParams.Command
if w := `--target-classpath-for-sdk any /system/framework/foo.jar:/system/framework/bar.jar`; !strings.Contains(cmd, w) {
t.Errorf("wanted %q in %q", w, cmd)
}
diff --git a/java/java.go b/java/java.go
index 4d7d568ad..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())
@@ -991,7 +995,7 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps {
case libTag:
deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
// names of sdk libs that are directly depended are exported
- j.exportedSdkLibs.AddLibraryPath(ctx, dep.OptionalImplicitSdkLibrary(), dep.DexJarBuildPath(), dep.DexJarInstallPath())
+ j.exportedSdkLibs.MaybeAddLibraryPath(ctx, dep.OptionalImplicitSdkLibrary(), dep.DexJarBuildPath(), dep.DexJarInstallPath())
case staticLibTag:
ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
}
@@ -1970,7 +1974,7 @@ func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
// add the name of that java_sdk_library to the exported sdk libs to make sure
// that, if necessary, a <uses-library> element for that java_sdk_library is
// added to the Android manifest.
- j.exportedSdkLibs.AddLibraryPath(ctx, j.OptionalImplicitSdkLibrary(), j.DexJarBuildPath(), j.DexJarInstallPath())
+ j.exportedSdkLibs.MaybeAddLibraryPath(ctx, j.OptionalImplicitSdkLibrary(), j.DexJarBuildPath(), j.DexJarInstallPath())
j.distFiles = j.GenerateTaggedDistFiles(ctx)
}
@@ -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,14 +2626,18 @@ 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())
+ j.exportedSdkLibs.AddLibraryPath(ctx, otherName, dep.DexJarBuildPath(), dep.DexJarInstallPath())
}
}
})
@@ -2623,9 +2652,29 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
// add the name of that java_sdk_library to the exported sdk libs to make sure
// that, if necessary, a <uses-library> element for that java_sdk_library is
// added to the Android manifest.
- j.exportedSdkLibs.AddLibraryPath(ctx, j.OptionalImplicitSdkLibrary(), outputFile, installFile)
+ 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 0e936118c..3f7bab194 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")
}
diff --git a/java/prebuilt_apis.go b/java/prebuilt_apis.go
index ac8337dc5..bcc6cc0cf 100644
--- a/java/prebuilt_apis.go
+++ b/java/prebuilt_apis.go
@@ -38,6 +38,9 @@ type prebuiltApisProperties struct {
// The sdk_version of java_import modules generated based on jar files.
// Defaults to "current"
Imports_sdk_version *string
+
+ // If set to true, compile dex for java_import modules. Defaults to false.
+ Imports_compile_dex *bool
}
type prebuiltApis struct {
@@ -78,17 +81,19 @@ func prebuiltApiModuleName(mctx android.LoadHookContext, module string, scope st
return mctx.ModuleName() + "_" + scope + "_" + apiver + "_" + module
}
-func createImport(mctx android.LoadHookContext, module, scope, apiver, path, sdk_version string) {
+func createImport(mctx android.LoadHookContext, module, scope, apiver, path, sdkVersion string, compileDex bool) {
props := struct {
Name *string
Jars []string
Sdk_version *string
Installable *bool
+ Compile_dex *bool
}{}
props.Name = proptools.StringPtr(prebuiltApiModuleName(mctx, module, scope, apiver))
props.Jars = append(props.Jars, path)
- props.Sdk_version = proptools.StringPtr(sdk_version)
+ props.Sdk_version = proptools.StringPtr(sdkVersion)
props.Installable = proptools.BoolPtr(false)
+ props.Compile_dex = proptools.BoolPtr(compileDex)
mctx.CreateModule(ImportFactory, &props)
}
@@ -124,13 +129,14 @@ func prebuiltSdkStubs(mctx android.LoadHookContext, p *prebuiltApis) {
// <apiver>/<scope>/<module>.jar
files := getPrebuiltFiles(mctx, p, "*.jar")
- sdk_version := proptools.StringDefault(p.properties.Imports_sdk_version, "current")
+ sdkVersion := proptools.StringDefault(p.properties.Imports_sdk_version, "current")
+ compileDex := proptools.BoolDefault(p.properties.Imports_compile_dex, false)
for _, f := range files {
// create a Import module for each jar file
localPath := strings.TrimPrefix(f, mydir)
module, apiver, scope := parseJarPath(localPath)
- createImport(mctx, module, scope, apiver, localPath, sdk_version)
+ createImport(mctx, module, scope, apiver, localPath, sdkVersion, compileDex)
}
}
diff --git a/java/sdk.go b/java/sdk.go
index b44cd8e1a..56fa12b3e 100644
--- a/java/sdk.go
+++ b/java/sdk.go
@@ -191,6 +191,26 @@ func (s sdkSpec) prebuiltSdkAvailableForUnbundledBuild() bool {
return s.kind != sdkPrivate && s.kind != sdkNone && s.kind != sdkCorePlatform
}
+func (s sdkSpec) forVendorPartition(ctx android.EarlyModuleContext) sdkSpec {
+ // If BOARD_CURRENT_API_LEVEL_FOR_VENDOR_MODULES has a numeric value,
+ // use it instead of "current" for the vendor partition.
+ currentSdkVersion := ctx.DeviceConfig().CurrentApiLevelForVendorModules()
+ if currentSdkVersion == "current" {
+ return s
+ }
+
+ if s.kind == sdkPublic || s.kind == sdkSystem {
+ if s.version.isCurrent() {
+ if i, err := strconv.Atoi(currentSdkVersion); err == nil {
+ version := sdkVersion(i)
+ return sdkSpec{s.kind, version, s.raw}
+ }
+ panic(fmt.Errorf("BOARD_CURRENT_API_LEVEL_FOR_VENDOR_MODULES must be either \"current\" or a number, but was %q", currentSdkVersion))
+ }
+ }
+ return s
+}
+
// usePrebuilt determines whether prebuilt SDK should be used for this sdkSpec with the given context.
func (s sdkSpec) usePrebuilt(ctx android.EarlyModuleContext) bool {
if s.version.isCurrent() {
@@ -216,6 +236,10 @@ func (s sdkSpec) effectiveVersion(ctx android.EarlyModuleContext) (sdkVersion, e
if !s.valid() {
return s.version, fmt.Errorf("invalid sdk version %q", s.raw)
}
+
+ if ctx.DeviceSpecific() || ctx.SocSpecific() {
+ s = s.forVendorPartition(ctx)
+ }
if s.version.isNumbered() {
return s.version, nil
}
@@ -330,6 +354,10 @@ func decodeSdkDep(ctx android.EarlyModuleContext, sdkContext sdkContext) sdkDep
return sdkDep{}
}
+ if ctx.DeviceSpecific() || ctx.SocSpecific() {
+ sdkVersion = sdkVersion.forVendorPartition(ctx)
+ }
+
if !sdkVersion.validateSystemSdk(ctx) {
return sdkDep{}
}
diff --git a/java/sdk_library.go b/java/sdk_library.go
index a5db56c2b..88cf46826 100644
--- a/java/sdk_library.go
+++ b/java/sdk_library.go
@@ -419,6 +419,9 @@ type sdkLibraryProperties struct {
// $(location <label>): the path to the droiddoc_option_files with name <label>
Droiddoc_options []string
+ // is set to true, Metalava will allow framework SDK to contain annotations.
+ Annotations_enabled *bool
+
// a list of top-level directories containing files to merge qualifier annotations
// (i.e. those intended to be included in the stubs written) from.
Merge_annotations_dirs []string
@@ -1086,11 +1089,25 @@ func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) stri
return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
}
+func childModuleVisibility(childVisibility []string) []string {
+ if childVisibility == nil {
+ // No child visibility set. The child will use the visibility of the sdk_library.
+ return nil
+ }
+
+ // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
+ var visibility []string
+ visibility = append(visibility, "//visibility:override")
+ visibility = append(visibility, childVisibility...)
+ return visibility
+}
+
// Creates the implementation java library
func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
-
moduleNamePtr := proptools.StringPtr(module.BaseModuleName())
+ visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
+
props := struct {
Name *string
Visibility []string
@@ -1098,7 +1115,7 @@ func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext)
ConfigurationName *string
}{
Name: proptools.StringPtr(module.implLibraryModuleName()),
- Visibility: module.sdkLibraryProperties.Impl_library_visibility,
+ Visibility: visibility,
// Set the instrument property to ensure it is instrumented when instrumentation is required.
Instrument: true,
@@ -1145,12 +1162,7 @@ func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext
}{}
props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
-
- // If stubs_library_visibility is not set then the created module will use the
- // visibility of this module.
- visibility := module.sdkLibraryProperties.Stubs_library_visibility
- props.Visibility = visibility
-
+ props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
// sources are generated from the droiddoc
props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
@@ -1159,6 +1171,11 @@ func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext
props.Patch_module = module.properties.Patch_module
props.Installable = proptools.BoolPtr(false)
props.Libs = module.sdkLibraryProperties.Stub_only_libs
+ // The stub-annotations library contains special versions of the annotations
+ // with CLASS retention policy, so that they're kept.
+ if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
+ props.Libs = append(props.Libs, "stub-annotations")
+ }
props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
// We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
@@ -1193,6 +1210,7 @@ func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookC
Arg_files []string
Args *string
Java_version *string
+ Annotations_enabled *bool
Merge_annotations_dirs []string
Merge_inclusion_annotations_dirs []string
Generate_stubs *bool
@@ -1225,12 +1243,7 @@ func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookC
// * libs (static_libs/libs)
props.Name = proptools.StringPtr(name)
-
- // If stubs_source_visibility is not set then the created module will use the
- // visibility of this module.
- visibility := module.sdkLibraryProperties.Stubs_source_visibility
- props.Visibility = visibility
-
+ props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
props.Srcs = append(props.Srcs, module.properties.Srcs...)
props.Sdk_version = module.deviceProperties.Sdk_version
props.System_modules = module.deviceProperties.System_modules
@@ -1243,6 +1256,7 @@ func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookC
props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
props.Java_version = module.properties.Java_version
+ props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
@@ -2070,6 +2084,11 @@ func (module *sdkLibraryXml) UniqueApexVariations() bool {
}
// from android.PrebuiltEtcModule
+func (module *sdkLibraryXml) BaseDir() string {
+ return "etc"
+}
+
+// from android.PrebuiltEtcModule
func (module *sdkLibraryXml) SubDir() string {
return "permissions"
}
diff --git a/java/testing.go b/java/testing.go
index 1e725fa73..1db6ef264 100644
--- a/java/testing.go
+++ b/java/testing.go
@@ -44,6 +44,9 @@ func TestConfig(buildDir string, env map[string]string, bp string, fs map[string
"prebuilts/sdk/17/public/android.jar": nil,
"prebuilts/sdk/17/public/framework.aidl": nil,
"prebuilts/sdk/17/system/android.jar": nil,
+ "prebuilts/sdk/28/public/android.jar": nil,
+ "prebuilts/sdk/28/public/framework.aidl": nil,
+ "prebuilts/sdk/28/system/android.jar": nil,
"prebuilts/sdk/29/public/android.jar": nil,
"prebuilts/sdk/29/public/framework.aidl": nil,
"prebuilts/sdk/29/system/android.jar": nil,