Move installation of dexpreopted system server classpath libraries into apex
System server jars in apexes currently use a heuristic to determine
whether or not they are the "main" variant in order to create rules
that copy their dexpreopt artifacts to the final installed location
and their dex jars to a known location for other modules to reference.
The heuristic depends on knowing all apexes that depend on the
module, which will not be possible when the apex info mutator is
rewritten to support future incremental analysis.
Instead, export the dexpreopt artifacts and dex jars, and let the
apex decide whether or not to install them.
Bug: 372543712
Test: all soong tests pass
Change-Id: I478bc2edceca4614e08e9a695b05d091e5437ba1
diff --git a/apex/androidmk.go b/apex/androidmk.go
index ec5ca15..3a81ee4 100644
--- a/apex/androidmk.go
+++ b/apex/androidmk.go
@@ -226,11 +226,6 @@
required = append(required, a.required...)
targetRequired = append(targetRequired, a.TargetRequiredModuleNames()...)
hostRequired = append(hostRequired, a.HostRequiredModuleNames()...)
- for _, fi := range a.filesInfo {
- required = append(required, fi.requiredModuleNames...)
- targetRequired = append(targetRequired, fi.targetRequiredModuleNames...)
- hostRequired = append(hostRequired, fi.hostRequiredModuleNames...)
- }
android.AndroidMkEmitAssignList(w, "LOCAL_REQUIRED_MODULES", moduleNames, a.makeModulesToInstall, required)
android.AndroidMkEmitAssignList(w, "LOCAL_TARGET_REQUIRED_MODULES", targetRequired)
android.AndroidMkEmitAssignList(w, "LOCAL_HOST_REQUIRED_MODULES", hostRequired)
@@ -261,6 +256,7 @@
fmt.Fprintln(w, "LOCAL_SOONG_INSTALLED_MODULE :=", a.installedFile.String())
fmt.Fprintln(w, "LOCAL_SOONG_INSTALL_PAIRS :=", a.outputFile.String()+":"+a.installedFile.String())
fmt.Fprintln(w, "LOCAL_SOONG_INSTALL_SYMLINKS := ", strings.Join(a.compatSymlinks.Strings(), " "))
+ fmt.Fprintln(w, "LOCAL_SOONG_INSTALL_PAIRS +=", a.extraInstalledPairs.String())
}
fmt.Fprintln(w, "LOCAL_APEX_KEY_PATH := ", a.apexKeysPath.String())
diff --git a/apex/apex.go b/apex/apex.go
index d98cfae..a310671 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -415,6 +415,30 @@
Min_sdk_version *string
}
+// installPair stores a path to a built object and its install location. It is used for holding
+// the installation location of the dexpreopt artifacts for system server jars in apexes that need
+// to be installed when the apex is installed.
+type installPair struct {
+ from android.Path
+ to android.InstallPath
+}
+
+type installPairs []installPair
+
+// String converts a list of installPair structs to the form accepted by LOCAL_SOONG_INSTALL_PAIRS.
+func (p installPairs) String() string {
+ sb := &strings.Builder{}
+ for i, pair := range p {
+ if i != 0 {
+ sb.WriteByte(' ')
+ }
+ sb.WriteString(pair.from.String())
+ sb.WriteByte(':')
+ sb.WriteString(pair.to.String())
+ }
+ return sb.String()
+}
+
type apexBundle struct {
// Inherited structs
android.ModuleBase
@@ -496,6 +520,12 @@
// Path where this APEX was installed.
installedFile android.InstallPath
+ // Extra files that are installed alongside this APEX.
+ extraInstalledFiles android.InstallPaths
+
+ // The source and install locations for extraInstalledFiles for use in LOCAL_SOONG_INSTALL_PAIRS.
+ extraInstalledPairs installPairs
+
// fragment for this apex for apexkeys.txt
apexKeysPath android.WritablePath
@@ -570,13 +600,15 @@
// Info for Android.mk Module name of `module` in AndroidMk. Note the generated AndroidMk
// module for apexFile is named something like <AndroidMk module name>.<apex name>[<apex
// suffix>]
- androidMkModuleName string // becomes LOCAL_MODULE
- class apexFileClass // becomes LOCAL_MODULE_CLASS
- moduleDir string // becomes LOCAL_PATH
- requiredModuleNames []string // becomes LOCAL_REQUIRED_MODULES
- targetRequiredModuleNames []string // becomes LOCAL_TARGET_REQUIRED_MODULES
- hostRequiredModuleNames []string // becomes LOCAL_HOST_REQUIRED_MODULES
- dataPaths []android.DataPath // becomes LOCAL_TEST_DATA
+ androidMkModuleName string // becomes LOCAL_MODULE
+ class apexFileClass // becomes LOCAL_MODULE_CLASS
+ moduleDir string // becomes LOCAL_PATH
+ dataPaths []android.DataPath // becomes LOCAL_TEST_DATA
+
+ // systemServerDexpreoptInstalls stores the list of dexpreopt artifacts for a system server jar.
+ systemServerDexpreoptInstalls []java.DexpreopterInstall
+ // systemServerDexJars stores the list of dexjars for a system server jar.
+ systemServerDexJars android.Paths
jacocoReportClassesFile android.Path // only for javalibs and apps
lintInfo *java.LintInfo // only for javalibs and apps
@@ -892,6 +924,12 @@
// Add a reverse dependency to all_apex_certs singleton module.
// all_apex_certs will use this dependency to collect the certificate of this apex.
ctx.AddReverseDependency(ctx.Module(), allApexCertsDepTag, "all_apex_certs")
+
+ // TODO: When all branches contain this singleton module, make this strict
+ // TODO: Add this dependency only for mainline prebuilts and not every prebuilt module
+ if ctx.OtherModuleExists("all_apex_contributions") {
+ ctx.AddDependency(ctx.Module(), android.AcDepTag, "all_apex_contributions")
+ }
}
type allApexCertsDependencyTag struct {
@@ -1501,16 +1539,15 @@
af.lintInfo = lintInfo
}
af.customStem = module.Stem() + ".jar"
+ // Collect any system server dex jars and dexpreopt artifacts for installation alongside the apex.
// TODO: b/338641779 - Remove special casing of sdkLibrary once bcpf and sscpf depends
// on the implementation library
if sdkLib, ok := module.(*java.SdkLibrary); ok {
- for _, install := range sdkLib.BuiltInstalledForApex() {
- af.requiredModuleNames = append(af.requiredModuleNames, install.FullModuleName())
- }
+ af.systemServerDexpreoptInstalls = append(af.systemServerDexpreoptInstalls, sdkLib.ApexSystemServerDexpreoptInstalls()...)
+ af.systemServerDexJars = append(af.systemServerDexJars, sdkLib.ApexSystemServerDexJars()...)
} else if dexpreopter, ok := module.(java.DexpreopterInterface); ok {
- for _, install := range dexpreopter.DexpreoptBuiltInstalledForApex() {
- af.requiredModuleNames = append(af.requiredModuleNames, install.FullModuleName())
- }
+ af.systemServerDexpreoptInstalls = append(af.systemServerDexpreoptInstalls, dexpreopter.ApexSystemServerDexpreoptInstalls()...)
+ af.systemServerDexJars = append(af.systemServerDexJars, dexpreopter.ApexSystemServerDexJars()...)
}
return af
}
@@ -2258,6 +2295,7 @@
////////////////////////////////////////////////////////////////////////////////////////////
// 4) generate the build rules to create the APEX. This is done in builder.go.
a.buildManifest(ctx, vctx.provideNativeLibs, vctx.requireNativeLibs)
+ a.installApexSystemServerFiles(ctx)
a.buildApex(ctx)
a.buildApexDependencyInfo(ctx)
a.buildLintReports(ctx)
diff --git a/apex/apex_test.go b/apex/apex_test.go
index a5b66c1..1382f69 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -9803,7 +9803,16 @@
var builder strings.Builder
data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
androidMk := builder.String()
- ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex\n")
+ out := ctx.Config().OutDir()
+ ensureContains(t, androidMk, "LOCAL_SOONG_INSTALL_PAIRS += "+
+ filepath.Join(out, "soong/.intermediates/foo/android_common_apex10000/dexpreopt/foo/oat/arm64/javalib.odex")+
+ ":"+
+ filepath.Join(out, "target/product/test_device/system/framework/oat/arm64/apex@myapex@javalib@foo.jar@classes.odex")+
+ " "+
+ filepath.Join(out, "soong/.intermediates/foo/android_common_apex10000/dexpreopt/foo/oat/arm64/javalib.vdex")+
+ ":"+
+ filepath.Join(out, "target/product/test_device/system/framework/oat/arm64/apex@myapex@javalib@foo.jar@classes.vdex")+
+ "\n")
}
func TestAndroidMk_RequiredModules(t *testing.T) {
diff --git a/apex/bootclasspath_fragment_test.go b/apex/bootclasspath_fragment_test.go
index 71a8246..d0bff62 100644
--- a/apex/bootclasspath_fragment_test.go
+++ b/apex/bootclasspath_fragment_test.go
@@ -320,6 +320,7 @@
})
java.CheckModuleDependencies(t, result.TestContext, "com.android.art", "android_common_com.android.art", []string{
+ `all_apex_contributions`,
`art-bootclasspath-fragment`,
`com.android.art.key`,
`dex2oatd`,
@@ -425,6 +426,7 @@
})
java.CheckModuleDependencies(t, result.TestContext, "com.android.art", "android_common_com.android.art", []string{
+ `all_apex_contributions`,
`art-bootclasspath-fragment`,
`com.android.art.key`,
`dex2oatd`,
@@ -698,6 +700,7 @@
})
java.CheckModuleDependencies(t, result.TestContext, "myapex", "android_common_myapex", []string{
+ `all_apex_contributions`,
`dex2oatd`,
`myapex.key`,
`mybootclasspathfragment`,
diff --git a/apex/builder.go b/apex/builder.go
index b317472..31d8557 100644
--- a/apex/builder.go
+++ b/apex/builder.go
@@ -20,12 +20,14 @@
"path"
"path/filepath"
"runtime"
+ "slices"
"sort"
"strconv"
"strings"
"android/soong/aconfig"
"android/soong/android"
+ "android/soong/dexpreopt"
"android/soong/java"
"github.com/google/blueprint"
@@ -542,6 +544,64 @@
return processed
}
+// installApexSystemServerFiles installs dexpreopt and dexjar files for system server classpath entries
+// provided by the apex. They are installed here instead of in library module because there may be multiple
+// variants of the library, generally one for the "main" apex and another with a different min_sdk_version
+// for the Android Go version of the apex. Both variants would attempt to install to the same locations,
+// and the library variants cannot determine which one should. The apex module is better equipped to determine
+// if it is "selected".
+// This assumes that the jars produced by different min_sdk_version values are identical, which is currently
+// true but may not be true if the min_sdk_version difference between the variants spans version that changed
+// the dex format.
+func (a *apexBundle) installApexSystemServerFiles(ctx android.ModuleContext) {
+ // If performInstalls is set this module is responsible for creating the install rules.
+ performInstalls := a.GetOverriddenBy() == "" && !a.testApex && a.installable()
+ // TODO(b/234351700): Remove once ART does not have separated debug APEX, or make the selection
+ // explicit in the ART Android.bp files.
+ if ctx.Config().UseDebugArt() {
+ if ctx.ModuleName() == "com.android.art" {
+ performInstalls = false
+ }
+ } else {
+ if ctx.ModuleName() == "com.android.art.debug" {
+ performInstalls = false
+ }
+ }
+
+ psi := android.PrebuiltSelectionInfoMap{}
+ ctx.VisitDirectDeps(func(am android.Module) {
+ if info, exists := android.OtherModuleProvider(ctx, am, android.PrebuiltSelectionInfoProvider); exists {
+ psi = info
+ }
+ })
+
+ if len(psi.GetSelectedModulesForApiDomain(ctx.ModuleName())) > 0 {
+ performInstalls = false
+ }
+
+ for _, fi := range a.filesInfo {
+ for _, install := range fi.systemServerDexpreoptInstalls {
+ var installedFile android.InstallPath
+ if performInstalls {
+ installedFile = ctx.InstallFile(install.InstallDirOnDevice, install.InstallFileOnDevice, install.OutputPathOnHost)
+ } else {
+ // Another module created the install rules, but this module should still depend on
+ // the installed locations.
+ installedFile = install.InstallDirOnDevice.Join(ctx, install.InstallFileOnDevice)
+ }
+ a.extraInstalledFiles = append(a.extraInstalledFiles, installedFile)
+ a.extraInstalledPairs = append(a.extraInstalledPairs, installPair{install.OutputPathOnHost, installedFile})
+ }
+ if performInstalls {
+ for _, dexJar := range fi.systemServerDexJars {
+ // Copy the system server dex jar to a predefined location where dex2oat will find it.
+ android.CopyFileRule(ctx, dexJar,
+ android.PathForOutput(ctx, dexpreopt.SystemServerDexjarsDir, dexJar.Base()))
+ }
+ }
+ }
+}
+
// buildApex creates build rules to build an APEX using apexer.
func (a *apexBundle) buildApex(ctx android.ModuleContext) {
suffix := imageApexSuffix
@@ -985,9 +1045,9 @@
a.SkipInstall()
}
+ installDeps := slices.Concat(a.compatSymlinks, a.extraInstalledFiles)
// Install to $OUT/soong/{target,host}/.../apex.
- a.installedFile = ctx.InstallFile(a.installDir, a.Name()+installSuffix, a.outputFile,
- a.compatSymlinks...)
+ a.installedFile = ctx.InstallFile(a.installDir, a.Name()+installSuffix, a.outputFile, installDeps...)
// installed-files.txt is dist'ed
a.installedFilesFile = a.buildInstalledFilesFile(ctx, a.outputFile, imageDir)
diff --git a/apex/prebuilt.go b/apex/prebuilt.go
index aaf2cb7..4fa43ba 100644
--- a/apex/prebuilt.go
+++ b/apex/prebuilt.go
@@ -15,6 +15,7 @@
package apex
import (
+ "slices"
"strconv"
"strings"
@@ -59,10 +60,12 @@
// Properties common to both prebuilt_apex and apex_set.
prebuiltCommonProperties *PrebuiltCommonProperties
- installDir android.InstallPath
- installFilename string
- installedFile android.InstallPath
- outputApex android.WritablePath
+ installDir android.InstallPath
+ installFilename string
+ installedFile android.InstallPath
+ extraInstalledFiles android.InstallPaths
+ extraInstalledPairs installPairs
+ outputApex android.WritablePath
// fragment for this apex for apexkeys.txt
apexKeysPath android.WritablePath
@@ -70,8 +73,12 @@
// Installed locations of symlinks for backward compatibility.
compatSymlinks android.InstallPaths
- hostRequired []string
- requiredModuleNames []string
+ // systemServerDexpreoptInstalls stores the list of dexpreopt artifacts for a system server jar.
+ systemServerDexpreoptInstalls []java.DexpreopterInstall
+
+ // systemServerDexJars stores the list of dexjars for system server jars in the prebuilt for use when
+ // dexpreopting system server jars that are later in the system server classpath.
+ systemServerDexJars android.Paths
}
type sanitizedPrebuilt interface {
@@ -188,9 +195,8 @@
// initApexFilesForAndroidMk initializes the prebuiltCommon.requiredModuleNames field with the install only deps of the prebuilt apex
func (p *prebuiltCommon) initApexFilesForAndroidMk(ctx android.ModuleContext) {
// If this apex contains a system server jar, then the dexpreopt artifacts should be added as required
- for _, install := range p.Dexpreopter.DexpreoptBuiltInstalledForApex() {
- p.requiredModuleNames = append(p.requiredModuleNames, install.FullModuleName())
- }
+ p.systemServerDexpreoptInstalls = append(p.systemServerDexpreoptInstalls, p.Dexpreopter.ApexSystemServerDexpreoptInstalls()...)
+ p.systemServerDexJars = append(p.systemServerDexJars, p.Dexpreopter.ApexSystemServerDexJars()...)
}
// If this prebuilt has system server jar, create the rules to dexpreopt it and install it alongside the prebuilt apex
@@ -218,38 +224,58 @@
}
}
-func (p *prebuiltCommon) addRequiredModules(entries *android.AndroidMkEntries) {
- entries.AddStrings("LOCAL_REQUIRED_MODULES", p.requiredModuleNames...)
+// installApexSystemServerFiles installs dexpreopt files for system server classpath entries
+// provided by the apex. They are installed here instead of in library module because there may be multiple
+// variants of the library, generally one for the "main" apex and another with a different min_sdk_version
+// for the Android Go version of the apex. Both variants would attempt to install to the same locations,
+// and the library variants cannot determine which one should. The apex module is better equipped to determine
+// if it is "selected".
+// This assumes that the jars produced by different min_sdk_version values are identical, which is currently
+// true but may not be true if the min_sdk_version difference between the variants spans version that changed
+// the dex format.
+func (p *prebuiltCommon) installApexSystemServerFiles(ctx android.ModuleContext) {
+ performInstalls := android.IsModulePreferred(ctx.Module())
+
+ for _, install := range p.systemServerDexpreoptInstalls {
+ var installedFile android.InstallPath
+ if performInstalls {
+ installedFile = ctx.InstallFile(install.InstallDirOnDevice, install.InstallFileOnDevice, install.OutputPathOnHost)
+ } else {
+ installedFile = install.InstallDirOnDevice.Join(ctx, install.InstallFileOnDevice)
+ }
+ p.extraInstalledFiles = append(p.extraInstalledFiles, installedFile)
+ p.extraInstalledPairs = append(p.extraInstalledPairs, installPair{install.OutputPathOnHost, installedFile})
+ }
+
+ for _, dexJar := range p.systemServerDexJars {
+ // Copy the system server dex jar to a predefined location where dex2oat will find it.
+ android.CopyFileRule(ctx, dexJar,
+ android.PathForOutput(ctx, dexpreopt.SystemServerDexjarsDir, dexJar.Base()))
+ }
}
func (p *prebuiltCommon) AndroidMkEntries() []android.AndroidMkEntries {
entriesList := []android.AndroidMkEntries{
{
- Class: "ETC",
- OutputFile: android.OptionalPathForPath(p.outputApex),
- Include: "$(BUILD_PREBUILT)",
- Host_required: p.hostRequired,
+ Class: "ETC",
+ OutputFile: android.OptionalPathForPath(p.outputApex),
+ Include: "$(BUILD_PREBUILT)",
ExtraEntries: []android.AndroidMkExtraEntriesFunc{
func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
entries.SetPath("LOCAL_SOONG_INSTALLED_MODULE", p.installedFile)
- entries.SetString("LOCAL_SOONG_INSTALL_PAIRS", p.outputApex.String()+":"+p.installedFile.String())
+ installPairs := append(installPairs{{p.outputApex, p.installedFile}}, p.extraInstalledPairs...)
+ entries.SetString("LOCAL_SOONG_INSTALL_PAIRS", installPairs.String())
entries.AddStrings("LOCAL_SOONG_INSTALL_SYMLINKS", p.compatSymlinks.Strings()...)
entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
entries.SetString("LOCAL_APEX_KEY_PATH", p.apexKeysPath.String())
- p.addRequiredModules(entries)
},
},
},
}
- // Add the dexpreopt artifacts to androidmk
- for _, install := range p.Dexpreopter.DexpreoptBuiltInstalledForApex() {
- entriesList = append(entriesList, install.ToMakeEntries())
- }
-
return entriesList
}
@@ -679,7 +705,9 @@
}
if p.installable() {
- p.installedFile = ctx.InstallFile(p.installDir, p.installFilename, p.inputApex, p.compatSymlinks...)
+ p.installApexSystemServerFiles(ctx)
+ installDeps := slices.Concat(p.compatSymlinks, p.extraInstalledFiles)
+ p.installedFile = ctx.InstallFile(p.installDir, p.installFilename, p.inputApex, installDeps...)
p.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, p.inputApex, p.installedFile)
}
@@ -690,16 +718,6 @@
return p.provenanceMetaDataFile
}
-// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
-// module. It extracts the correct apex to use and makes it available for use by apex_set.
-type prebuiltApexExtractorModule struct {
- android.ModuleBase
-
- properties ApexExtractorProperties
-
- extractedApex android.WritablePath
-}
-
// extract registers the build actions to extract an apex from .apks file
// returns the path of the extracted apex
func extract(ctx android.ModuleContext, apexSet android.Path, prerelease *bool) android.Path {
@@ -866,7 +884,8 @@
a.installDir = android.PathForModuleInstall(ctx, "apex")
if a.installable() {
- a.installedFile = ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
+ a.installApexSystemServerFiles(ctx)
+ a.installedFile = ctx.InstallFile(a.installDir, a.installFilename, a.outputApex, a.extraInstalledFiles...)
}
// in case that apex_set replaces source apex (using prefer: prop)
@@ -878,11 +897,3 @@
ctx.SetOutputFiles(android.Paths{a.outputApex}, "")
}
-
-type systemExtContext struct {
- android.ModuleContext
-}
-
-func (*systemExtContext) SystemExtSpecific() bool {
- return true
-}
diff --git a/apex/systemserver_classpath_fragment_test.go b/apex/systemserver_classpath_fragment_test.go
index 7dbac5f..c643a8c 100644
--- a/apex/systemserver_classpath_fragment_test.go
+++ b/apex/systemserver_classpath_fragment_test.go
@@ -108,6 +108,7 @@
})
java.CheckModuleDependencies(t, ctx, "myapex", "android_common_myapex", []string{
+ `all_apex_contributions`,
`dex2oatd`,
`myapex.key`,
`mysystemserverclasspathfragment`,
@@ -166,6 +167,7 @@
})
java.CheckModuleDependencies(t, result.TestContext, "myapex", "android_common_myapex", []string{
+ `all_apex_contributions`,
`dex2oatd`,
`myapex.key`,
`mysystemserverclasspathfragment`,