Merge "Generate multilib for rust_test"
diff --git a/android/config.go b/android/config.go
index a53f44a..b906108 100644
--- a/android/config.go
+++ b/android/config.go
@@ -712,10 +712,17 @@
 	return Bool(c.productVariables.Allow_missing_dependencies)
 }
 
+// Returns true if building without full platform sources.
 func (c *config) UnbundledBuild() bool {
 	return Bool(c.productVariables.Unbundled_build)
 }
 
+// Returns true if building apps that aren't bundled with the platform.
+// UnbundledBuild() is always true when this is true.
+func (c *config) UnbundledBuildApps() bool {
+	return Bool(c.productVariables.Unbundled_build_apps)
+}
+
 func (c *config) UnbundledBuildUsePrebuiltSdks() bool {
 	return Bool(c.productVariables.Unbundled_build) && !Bool(c.productVariables.Unbundled_build_sdks_from_source)
 }
diff --git a/android/variable.go b/android/variable.go
index 863fe5a..b2149c3 100644
--- a/android/variable.go
+++ b/android/variable.go
@@ -216,6 +216,7 @@
 
 	Allow_missing_dependencies       *bool `json:",omitempty"`
 	Unbundled_build                  *bool `json:",omitempty"`
+	Unbundled_build_apps             *bool `json:",omitempty"`
 	Unbundled_build_sdks_from_source *bool `json:",omitempty"`
 	Malloc_not_svelte                *bool `json:",omitempty"`
 	Malloc_zero_contents             *bool `json:",omitempty"`
diff --git a/apex/androidmk.go b/apex/androidmk.go
index 91a405c..4dd14d8 100644
--- a/apex/androidmk.go
+++ b/apex/androidmk.go
@@ -184,7 +184,7 @@
 			// we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
 			// we will have foo.jar.jar
 			fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.Stem(), ".jar"))
-			if javaModule, ok := fi.module.(java.Dependency); ok {
+			if javaModule, ok := fi.module.(java.ApexDependency); ok {
 				fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
 				fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
 			} else {
diff --git a/apex/apex.go b/apex/apex.go
index ce5d663..a4af7aa 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -1652,7 +1652,9 @@
 func apexFileForJavaLibrary(ctx android.BaseModuleContext, lib javaDependency, module android.Module) apexFile {
 	dirInApex := "javalib"
 	fileToCopy := lib.DexJarBuildPath()
-	af := newApexFile(ctx, fileToCopy, module.Name(), dirInApex, javaSharedLib, module)
+	// Remove prebuilt_ if necessary so the source and prebuilt modules have the same name.
+	name := strings.TrimPrefix(module.Name(), "prebuilt_")
+	af := newApexFile(ctx, fileToCopy, name, dirInApex, javaSharedLib, module)
 	af.jacocoReportClassesFile = lib.JacocoReportClassesFile()
 	af.stem = lib.Stem() + ".jar"
 	return af
diff --git a/cc/sdk.go b/cc/sdk.go
index a6f94af..b68baad 100644
--- a/cc/sdk.go
+++ b/cc/sdk.go
@@ -41,7 +41,7 @@
 			modules[0].(*Module).Properties.Sdk_version = nil
 			modules[1].(*Module).Properties.IsSdkVariant = true
 
-			if ctx.Config().UnbundledBuild() {
+			if ctx.Config().UnbundledBuildApps() {
 				modules[0].(*Module).Properties.HideFromMake = true
 				modules[0].(*Module).Properties.PreventInstall = true
 			} else {
diff --git a/java/aar.go b/java/aar.go
index 28e388a..878704d 100644
--- a/java/aar.go
+++ b/java/aar.go
@@ -506,11 +506,8 @@
 func AndroidLibraryFactory() android.Module {
 	module := &AndroidLibrary{}
 
+	module.Module.addHostAndDeviceProperties()
 	module.AddProperties(
-		&module.Module.properties,
-		&module.Module.deviceProperties,
-		&module.Module.dexpreoptProperties,
-		&module.Module.protoProperties,
 		&module.aaptProperties,
 		&module.androidLibraryProperties)
 
diff --git a/java/app.go b/java/app.go
index 74c92a6..86d8c3c 100755
--- a/java/app.go
+++ b/java/app.go
@@ -963,11 +963,8 @@
 	module.Module.properties.Instrument = true
 	module.Module.properties.Installable = proptools.BoolPtr(true)
 
+	module.addHostAndDeviceProperties()
 	module.AddProperties(
-		&module.Module.properties,
-		&module.Module.deviceProperties,
-		&module.Module.dexpreoptProperties,
-		&module.Module.protoProperties,
 		&module.aaptProperties,
 		&module.appProperties,
 		&module.overridableAppProperties,
@@ -1083,11 +1080,8 @@
 	module.appProperties.AlwaysPackageNativeLibs = true
 	module.Module.dexpreopter.isTest = true
 
+	module.addHostAndDeviceProperties()
 	module.AddProperties(
-		&module.Module.properties,
-		&module.Module.deviceProperties,
-		&module.Module.dexpreoptProperties,
-		&module.Module.protoProperties,
 		&module.aaptProperties,
 		&module.appProperties,
 		&module.appTestProperties,
@@ -1135,11 +1129,8 @@
 	module.appProperties.AlwaysPackageNativeLibs = true
 	module.Module.dexpreopter.isTest = true
 
+	module.addHostAndDeviceProperties()
 	module.AddProperties(
-		&module.Module.properties,
-		&module.Module.deviceProperties,
-		&module.Module.dexpreoptProperties,
-		&module.Module.protoProperties,
 		&module.aaptProperties,
 		&module.appProperties,
 		&module.appTestHelperAppProperties,
diff --git a/java/java.go b/java/java.go
index bd5f6b7..e00f681 100644
--- a/java/java.go
+++ b/java/java.go
@@ -481,6 +481,21 @@
 	modulePaths []string
 }
 
+func (j *Module) addHostProperties() {
+	j.AddProperties(
+		&j.properties,
+		&j.protoProperties,
+	)
+}
+
+func (j *Module) addHostAndDeviceProperties() {
+	j.addHostProperties()
+	j.AddProperties(
+		&j.deviceProperties,
+		&j.dexpreoptProperties,
+	)
+}
+
 func (j *Module) OutputFiles(tag string) (android.Paths, error) {
 	switch tag {
 	case "":
@@ -496,11 +511,16 @@
 
 var _ android.OutputFileProducer = (*Module)(nil)
 
-type Dependency interface {
+// Methods that need to be implemented for a module that is added to apex java_libs property.
+type ApexDependency interface {
 	HeaderJars() android.Paths
+	ImplementationAndResourcesJars() android.Paths
+}
+
+type Dependency interface {
+	ApexDependency
 	ImplementationJars() android.Paths
 	ResourceJars() android.Paths
-	ImplementationAndResourcesJars() android.Paths
 	DexJarBuildPath() android.Path
 	DexJarInstallPath() android.Path
 	AidlIncludeDirs() android.Paths
@@ -2046,12 +2066,8 @@
 func LibraryFactory() android.Module {
 	module := &Library{}
 
-	module.AddProperties(
-		&module.Module.properties,
-		&module.Module.deviceProperties,
-		&module.Module.dexpreoptProperties,
-		&module.Module.protoProperties,
-		&module.libraryProperties)
+	module.addHostAndDeviceProperties()
+	module.AddProperties(&module.libraryProperties)
 
 	module.initModuleAndImport(&module.ModuleBase)
 
@@ -2073,9 +2089,7 @@
 func LibraryHostFactory() android.Module {
 	module := &Library{}
 
-	module.AddProperties(
-		&module.Module.properties,
-		&module.Module.protoProperties)
+	module.addHostProperties()
 
 	module.Module.properties.Installable = proptools.BoolPtr(true)
 
@@ -2243,12 +2257,8 @@
 func TestFactory() android.Module {
 	module := &Test{}
 
-	module.AddProperties(
-		&module.Module.properties,
-		&module.Module.deviceProperties,
-		&module.Module.dexpreoptProperties,
-		&module.Module.protoProperties,
-		&module.testProperties)
+	module.addHostAndDeviceProperties()
+	module.AddProperties(&module.testProperties)
 
 	module.Module.properties.Installable = proptools.BoolPtr(true)
 	module.Module.dexpreopter.isTest = true
@@ -2261,12 +2271,8 @@
 func TestHelperLibraryFactory() android.Module {
 	module := &TestHelperLibrary{}
 
-	module.AddProperties(
-		&module.Module.properties,
-		&module.Module.deviceProperties,
-		&module.Module.dexpreoptProperties,
-		&module.Module.protoProperties,
-		&module.testHelperLibraryProperties)
+	module.addHostAndDeviceProperties()
+	module.AddProperties(&module.testHelperLibraryProperties)
 
 	module.Module.properties.Installable = proptools.BoolPtr(true)
 	module.Module.dexpreopter.isTest = true
@@ -2307,10 +2313,8 @@
 func TestHostFactory() android.Module {
 	module := &Test{}
 
-	module.AddProperties(
-		&module.Module.properties,
-		&module.Module.protoProperties,
-		&module.testProperties)
+	module.addHostProperties()
+	module.AddProperties(&module.testProperties)
 
 	module.Module.properties.Installable = proptools.BoolPtr(true)
 
@@ -2394,12 +2398,8 @@
 func BinaryFactory() android.Module {
 	module := &Binary{}
 
-	module.AddProperties(
-		&module.Module.properties,
-		&module.Module.deviceProperties,
-		&module.Module.dexpreoptProperties,
-		&module.Module.protoProperties,
-		&module.binaryProperties)
+	module.addHostAndDeviceProperties()
+	module.AddProperties(&module.binaryProperties)
 
 	module.Module.properties.Installable = proptools.BoolPtr(true)
 
@@ -2415,10 +2415,8 @@
 func BinaryHostFactory() android.Module {
 	module := &Binary{}
 
-	module.AddProperties(
-		&module.Module.properties,
-		&module.Module.protoProperties,
-		&module.binaryProperties)
+	module.addHostProperties()
+	module.AddProperties(&module.binaryProperties)
 
 	module.Module.properties.Installable = proptools.BoolPtr(true)
 
diff --git a/java/plugin.go b/java/plugin.go
index a5e8292..947c286 100644
--- a/java/plugin.go
+++ b/java/plugin.go
@@ -24,10 +24,8 @@
 func PluginFactory() android.Module {
 	module := &Plugin{}
 
-	module.AddProperties(
-		&module.Module.properties,
-		&module.Module.protoProperties,
-		&module.pluginProperties)
+	module.addHostProperties()
+	module.AddProperties(&module.pluginProperties)
 
 	InitJavaModule(module, android.HostSupported)
 	return module
diff --git a/java/robolectric.go b/java/robolectric.go
index 3195615..64bd325 100644
--- a/java/robolectric.go
+++ b/java/robolectric.go
@@ -215,10 +215,9 @@
 func RobolectricTestFactory() android.Module {
 	module := &robolectricTest{}
 
+	module.addHostProperties()
 	module.AddProperties(
-		&module.Module.properties,
 		&module.Module.deviceProperties,
-		&module.Module.protoProperties,
 		&module.robolectricProperties)
 
 	module.Module.dexpreopter.isTest = true
diff --git a/java/sdk_library.go b/java/sdk_library.go
index 503ad59..8401162 100644
--- a/java/sdk_library.go
+++ b/java/sdk_library.go
@@ -1543,13 +1543,8 @@
 }
 
 func (module *SdkLibrary) InitSdkLibraryProperties() {
-	module.AddProperties(
-		&module.sdkLibraryProperties,
-		&module.properties,
-		&module.dexpreoptProperties,
-		&module.deviceProperties,
-		&module.protoProperties,
-	)
+	module.addHostAndDeviceProperties()
+	module.AddProperties(&module.sdkLibraryProperties)
 
 	module.initSdkLibraryComponent(&module.ModuleBase)
 
@@ -1999,6 +1994,26 @@
 	return module.BaseModuleName()
 }
 
+var _ ApexDependency = (*SdkLibraryImport)(nil)
+
+// to satisfy java.ApexDependency interface
+func (module *SdkLibraryImport) HeaderJars() android.Paths {
+	if module.implLibraryModule == nil {
+		return nil
+	} else {
+		return module.implLibraryModule.HeaderJars()
+	}
+}
+
+// to satisfy java.ApexDependency interface
+func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
+	if module.implLibraryModule == nil {
+		return nil
+	} else {
+		return module.implLibraryModule.ImplementationAndResourcesJars()
+	}
+}
+
 //
 // java_sdk_library_xml
 //
diff --git a/rust/builder.go b/rust/builder.go
index 5069b07..7dbb59d 100644
--- a/rust/builder.go
+++ b/rust/builder.go
@@ -163,7 +163,7 @@
 	if flags.Coverage {
 		var gcnoFile android.WritablePath
 		// Provide consistency with cc gcda output, see cc/builder.go init()
-		profileEmitArg := strings.TrimPrefix("PWD=", cc.PwdPrefix()) + "/"
+		profileEmitArg := strings.TrimPrefix(cc.PwdPrefix(), "PWD=") + "/"
 
 		if outputFile.Ext() != "" {
 			gcnoFile = android.PathForModuleOut(ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcno"))