Use the profiles in the APEX to dexpreopt system server jars.

After this change, if "profile_guided: true" is set, profile-guided
compilation will be enabled for the jar and the ".prof" file next to
the jar in the APEX ("javalib/<name>.jar.prof") will be used as the
profile when dexpreopting for the prebuilt APEX.

Bug: 241823638
Test: m nothing
Test: -
  1. (on internal master) Patch aosp/2426453.
  2. Build the APEX bundle and the module SDK of com.android.wifi
  3. (on tm-dev) Patch this CL and aosp/2141972.
  4. Copy the APEX bundle and the module SDK built on step 2 to the
     source tree
  5. Disable hiddenapi check
  6. lunch cf_x86_64_phone-userdebug && m MODULE_BUILD_FROM_SOURCE=false com.google.android.wifi
  7. cat out/soong/.intermediates/prebuilts/module_sdk/Wifi/current/prebuilt_service-wifi/android_common_com.android.wifi/dexpreopt/oat/x86_64/javalib.invocation
  8. See the profile being used.
Change-Id: I55a5a295e9c5d6f0564afb139c5fb7da91ab8cae
diff --git a/java/dexpreopt.go b/java/dexpreopt.go
index fa52ae6..e0a0629 100644
--- a/java/dexpreopt.go
+++ b/java/dexpreopt.go
@@ -23,11 +23,24 @@
 )
 
 type DexpreopterInterface interface {
-	IsInstallable() bool // Structs that embed dexpreopter must implement this.
+	// True if the java module is to be dexed and installed on devices.
+	// Structs that embed dexpreopter must implement this.
+	IsInstallable() bool
+
+	// True if dexpreopt is disabled for the java module.
 	dexpreoptDisabled(ctx android.BaseModuleContext) bool
+
+	// If the java module is to be installed into an APEX, this list contains information about the
+	// dexpreopt outputs to be installed on devices. Note that these dexpreopt outputs are installed
+	// outside of the APEX.
 	DexpreoptBuiltInstalledForApex() []dexpreopterInstall
+
+	// The Make entries to install the dexpreopt outputs. Derived from
+	// `DexpreoptBuiltInstalledForApex`.
 	AndroidMkEntriesForApex() []android.AndroidMkEntries
-	ProfilePathOnHost() android.Path
+
+	// See `dexpreopter.outputProfilePathOnHost`.
+	OutputProfilePathOnHost() android.Path
 }
 
 type dexpreopterInstall struct {
@@ -106,8 +119,13 @@
 	//   dexpreopt another partition).
 	configPath android.WritablePath
 
-	// The path to the profile on host.
-	profilePathOnHost android.Path
+	// The path to the profile on host that dexpreopter generates. This is used as the input for
+	// dex2oat.
+	outputProfilePathOnHost android.Path
+
+	// The path to the profile that dexpreopter accepts. It must be in the binary format. If this is
+	// set, it overrides the profile settings in `dexpreoptProperties`.
+	inputProfilePathOnHost android.Path
 }
 
 type DexpreoptProperties struct {
@@ -308,7 +326,9 @@
 	var profileClassListing android.OptionalPath
 	var profileBootListing android.OptionalPath
 	profileIsTextListing := false
-	if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
+	if d.inputProfilePathOnHost != nil {
+		profileClassListing = android.OptionalPathForPath(d.inputProfilePathOnHost)
+	} else if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) && !forPrebuiltApex(ctx) {
 		// If dex_preopt.profile_guided is not set, default it based on the existence of the
 		// dexprepot.profile option or the profile class listing.
 		if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
@@ -389,7 +409,7 @@
 		isProfile := strings.HasSuffix(installBase, ".prof")
 
 		if isProfile {
-			d.profilePathOnHost = install.From
+			d.outputProfilePathOnHost = install.From
 		}
 
 		if isApexSystemServerJar {
@@ -431,6 +451,6 @@
 	return entries
 }
 
-func (d *dexpreopter) ProfilePathOnHost() android.Path {
-	return d.profilePathOnHost
+func (d *dexpreopter) OutputProfilePathOnHost() android.Path {
+	return d.outputProfilePathOnHost
 }