Ensure sscp jars get copied to $OUT/soong/system_server_dexjars

(This was missed in aosp/2876756, which copied only the bcp jars)

This hardcoded location is used by dex2oat to compile the dexpreopt
artifacts. The copy rules are currently generated by java_(sdk)_library
for source builds, and their prebuilt counterparts in prebuilt builds.

After this change, the copy rule will be bifurcated between source and
prebuilt builds
1. For source builds, it will come from java_(sdk)_library
2. For prebuilt builds, it will come from the top-level prebuilt apex.
   Since there can be multiple prebuilt apexes in trunk stable,
   HideFromMake will be used to determine which deapexed jar to copy.

The bifurfaction is expected to be temporary. It is needed for now since
the `apex_contributions` which will be used for source vs prebuilt
selection have not been populated completely.

Test: Added a unit test
Test: Presubmits
Test: git_master-art-host:
art-gtest (https://android-build.corp.google.com/builds/abtd/run/L40800030001459791)
Test: git_main:art_standalone_tests
(https://android-build.corp.google.com/builds/abtd/run/L09000030001463855)

Bug: 308790457

Change-Id: I3105d3b3a7e5c41cb601d07806f4ea483a61b50a
diff --git a/apex/apex_test.go b/apex/apex_test.go
index 7e67c0f..b58441a 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -11537,6 +11537,17 @@
 		android.AssertStringMatches(t, "Could not find the correct boot dex jar in monolithic hiddenapi flags generation command", monolithicHiddenapiFlagsCmd, "--boot-dex="+expectedBootJar)
 	}
 
+	// Check that system server jars is copied to out/soong/system_server_dexjars. This hardcoded location is used by dexpreopt
+	checkSystemServerJarOnHost := func(t *testing.T, ctx *android.TestContext, m, libName, expectedSystemServerJarPath string) {
+		variation := "android_common_com.android.foo"
+		if m == "com.android.foo" {
+			m = libName // for source builds, the cp rule is still generated by the java_library module
+			variation = "android_common_apex10000"
+		}
+		output := ctx.ModuleForTests(m, variation).MaybeOutput("system_server_dexjars/" + libName + ".jar")
+		android.AssertStringListContains(t, libName+" is expected to be copied to out/soong/system_server_dexjars/ from ", output.Implicits.Strings(), expectedSystemServerJarPath)
+	}
+
 	bp := `
 		// Source APEX.
 
@@ -11560,6 +11571,24 @@
 			},
 		}
 
+		java_library {
+			name: "service-foo",
+			srcs: ["foo.java"],
+			installable: true,
+			apex_available: [
+				"com.android.foo",
+			],
+		}
+
+		systemserverclasspath_fragment {
+			name: "foo-systemserverclasspath-fragment",
+			contents: ["service-foo"],
+			apex_available: [
+				"com.android.foo",
+			],
+		}
+
+
 		apex_key {
 			name: "com.android.foo.key",
 			public_key: "com.android.foo.avbpubkey",
@@ -11570,18 +11599,16 @@
 			name: "com.android.foo",
 			key: "com.android.foo.key",
 			bootclasspath_fragments: ["foo-bootclasspath-fragment"],
+			systemserverclasspath_fragments: ["foo-systemserverclasspath-fragment"],
 			updatable: false,
 		}
 
 		// Prebuilt APEX.
 
-		java_sdk_library_import {
+		java_import {
 			name: "framework-foo",
-			public: {
-				jars: ["foo.jar"],
-			},
+			jars: ["foo.jar"],
 			apex_available: ["com.android.foo"],
-			shared_library: false,
 		}
 
 		prebuilt_bootclasspath_fragment {
@@ -11599,11 +11626,27 @@
 			],
 		}
 
+		java_import {
+			name: "service-foo",
+			jars: ["foo.jar"],
+			apex_available: ["com.android.foo"],
+		}
+
+		prebuilt_systemserverclasspath_fragment {
+			name: "foo-systemserverclasspath-fragment",
+			contents: ["service-foo"],
+			apex_available: [
+				"com.android.foo",
+			],
+		}
+
+
 		prebuilt_apex {
 			name: "com.android.foo",
 			apex_name: "com.android.foo",
 			src: "com.android.foo-arm.apex",
 			exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
+			exported_systemserverclasspath_fragments: ["foo-systemserverclasspath-fragment"],
 		}
 
 		// Another Prebuilt ART APEX
@@ -11612,48 +11655,41 @@
 			apex_name: "com.android.foo", // Used to determine the API domain
 			src: "com.android.foo-arm.apex",
 			exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
+			exported_systemserverclasspath_fragments: ["foo-systemserverclasspath-fragment"],
 		}
 
 		// APEX contribution modules
 
 		apex_contributions {
-			name: "foo.source.contributions",
+			name: "foo.contributions",
 			api_domain: "com.android.foo",
-			contents: ["com.android.foo"],
-		}
-
-		apex_contributions {
-			name: "foo.prebuilt.contributions",
-			api_domain: "com.android.foo",
-			contents: ["prebuilt_com.android.foo"],
-		}
-
-		apex_contributions {
-			name: "foo.prebuilt.v2.contributions",
-			api_domain: "com.android.foo",
-			contents: ["com.android.foo.v2"], // prebuilt_ prefix is missing because of prebuilt_rename mutator
+			contents: ["%v"],
 		}
 	`
 
 	testCases := []struct {
-		desc                      string
-		selectedApexContributions string
-		expectedBootJar           string
+		desc                    string
+		selectedApex            string
+		expectedBootJar         string
+		expectedSystemServerJar string
 	}{
 		{
-			desc:                      "Source apex com.android.foo is selected, bootjar should come from source java library",
-			selectedApexContributions: "foo.source.contributions",
-			expectedBootJar:           "out/soong/.intermediates/foo-bootclasspath-fragment/android_common_apex10000/hiddenapi-modular/encoded/framework-foo.jar",
+			desc:                    "Source apex com.android.foo is selected, bootjar should come from source java library",
+			selectedApex:            "com.android.foo",
+			expectedBootJar:         "out/soong/.intermediates/foo-bootclasspath-fragment/android_common_apex10000/hiddenapi-modular/encoded/framework-foo.jar",
+			expectedSystemServerJar: "out/soong/.intermediates/service-foo/android_common_apex10000/aligned/service-foo.jar",
 		},
 		{
-			desc:                      "Prebuilt apex prebuilt_com.android.foo is selected, profile should come from .prof deapexed from the prebuilt",
-			selectedApexContributions: "foo.prebuilt.contributions",
-			expectedBootJar:           "out/soong/.intermediates/prebuilt_com.android.foo.deapexer/android_common/deapexer/javalib/framework-foo.jar",
+			desc:                    "Prebuilt apex prebuilt_com.android.foo is selected, profile should come from .prof deapexed from the prebuilt",
+			selectedApex:            "prebuilt_com.android.foo",
+			expectedBootJar:         "out/soong/.intermediates/prebuilt_com.android.foo.deapexer/android_common/deapexer/javalib/framework-foo.jar",
+			expectedSystemServerJar: "out/soong/.intermediates/prebuilt_com.android.foo.deapexer/android_common/deapexer/javalib/service-foo.jar",
 		},
 		{
-			desc:                      "Prebuilt apex prebuilt_com.android.foo.v2 is selected, profile should come from .prof deapexed from the prebuilt",
-			selectedApexContributions: "foo.prebuilt.v2.contributions",
-			expectedBootJar:           "out/soong/.intermediates/prebuilt_com.android.foo.v2.deapexer/android_common/deapexer/javalib/framework-foo.jar",
+			desc:                    "Prebuilt apex prebuilt_com.android.foo.v2 is selected, profile should come from .prof deapexed from the prebuilt",
+			selectedApex:            "com.android.foo.v2",
+			expectedBootJar:         "out/soong/.intermediates/prebuilt_com.android.foo.v2.deapexer/android_common/deapexer/javalib/framework-foo.jar",
+			expectedSystemServerJar: "out/soong/.intermediates/prebuilt_com.android.foo.v2.deapexer/android_common/deapexer/javalib/service-foo.jar",
 		},
 	}
 
@@ -11665,19 +11701,21 @@
 	for _, tc := range testCases {
 		preparer := android.GroupFixturePreparers(
 			java.FixtureConfigureApexBootJars("com.android.foo:framework-foo"),
+			dexpreopt.FixtureSetApexSystemServerJars("com.android.foo:service-foo"),
 			android.FixtureMergeMockFs(map[string][]byte{
 				"system/sepolicy/apex/com.android.foo-file_contexts": nil,
 			}),
 			android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
 				variables.BuildFlags = map[string]string{
-					"RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": tc.selectedApexContributions,
+					"RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": "foo.contributions",
 				}
 			}),
 		)
-		ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
+		ctx := testDexpreoptWithApexes(t, fmt.Sprintf(bp, tc.selectedApex), "", preparer, fragment)
 		checkBootDexJarPath(t, ctx, "framework-foo", tc.expectedBootJar)
 		checkBootJarsPackageCheck(t, ctx, tc.expectedBootJar)
 		checkBootJarsForMonolithicHiddenapi(t, ctx, tc.expectedBootJar)
+		checkSystemServerJarOnHost(t, ctx, tc.selectedApex, "service-foo", tc.expectedSystemServerJar)
 	}
 }