Merge "Revert "Also package recursive jni_libs deps of android_apps as well as direct deps.""
diff --git a/android/apex.go b/android/apex.go
index 3da4828..77000da 100644
--- a/android/apex.go
+++ b/android/apex.go
@@ -138,7 +138,7 @@
 }
 
 const (
-	availableToPlatform = "//apex_available:platform"
+	AvailableToPlatform = "//apex_available:platform"
 	availableToAnyApex  = "//apex_available:anyapex"
 )
 
@@ -149,7 +149,7 @@
 		return true
 	}
 	return InList(what, apex_available) ||
-		(what != availableToPlatform && InList(availableToAnyApex, apex_available))
+		(what != AvailableToPlatform && InList(availableToAnyApex, apex_available))
 }
 
 func (m *ApexModuleBase) AvailableFor(what string) bool {
@@ -165,7 +165,7 @@
 
 func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
 	for _, n := range m.ApexProperties.Apex_available {
-		if n == availableToPlatform || n == availableToAnyApex {
+		if n == AvailableToPlatform || n == availableToAnyApex {
 			continue
 		}
 		if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
@@ -179,7 +179,7 @@
 		m.checkApexAvailableProperty(mctx)
 		sort.Strings(m.apexVariations)
 		variations := []string{}
-		availableForPlatform := mctx.Module().(ApexModule).AvailableFor(availableToPlatform) || mctx.Host()
+		availableForPlatform := mctx.Module().(ApexModule).AvailableFor(AvailableToPlatform) || mctx.Host()
 		if availableForPlatform {
 			variations = append(variations, "") // Original variation for platform
 		}
diff --git a/apex/apex.go b/apex/apex.go
index 3d7b45d..488d3d3 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -488,9 +488,11 @@
 
 	properties            apexBundleProperties
 	targetProperties      apexTargetBundleProperties
-	vndkProperties        apexVndkProperties
 	overridableProperties overridableProperties
 
+	// specific to apex_vndk modules
+	vndkProperties apexVndkProperties
+
 	bundleModuleFile android.WritablePath
 	outputFile       android.WritablePath
 	installDir       android.InstallPath
@@ -1278,6 +1280,7 @@
 	module.AddProperties(
 		&apexBundleProperties{},
 		&apexTargetBundleProperties{},
+		&overridableProperties{},
 	)
 
 	android.InitDefaultsModule(module)
diff --git a/apex/apex_test.go b/apex/apex_test.go
index 4f0ab3a..0c6572b 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -532,6 +532,62 @@
 	ensureListContains(t, noticeInputs, "custom_notice")
 }
 
+func TestDefaults(t *testing.T) {
+	ctx, _ := testApex(t, `
+		apex_defaults {
+			name: "myapex-defaults",
+			key: "myapex.key",
+			prebuilts: ["myetc"],
+			native_shared_libs: ["mylib"],
+			java_libs: ["myjar"],
+			apps: ["AppFoo"],
+		}
+
+		prebuilt_etc {
+			name: "myetc",
+			src: "myprebuilt",
+		}
+
+		apex {
+			name: "myapex",
+			defaults: ["myapex-defaults"],
+		}
+
+		apex_key {
+			name: "myapex.key",
+			public_key: "testkey.avbpubkey",
+			private_key: "testkey.pem",
+		}
+
+		cc_library {
+			name: "mylib",
+			system_shared_libs: [],
+			stl: "none",
+		}
+
+		java_library {
+			name: "myjar",
+			srcs: ["foo/bar/MyClass.java"],
+			sdk_version: "none",
+			system_modules: "none",
+			compile_dex: true,
+		}
+
+		android_app {
+			name: "AppFoo",
+			srcs: ["foo/bar/MyClass.java"],
+			sdk_version: "none",
+			system_modules: "none",
+		}
+	`)
+	ensureExactContents(t, ctx, "myapex", []string{
+		"etc/myetc",
+		"javalib/myjar.jar",
+		"lib64/mylib.so",
+		"app/AppFoo/AppFoo.apk",
+	})
+}
+
 func TestApexManifest(t *testing.T) {
 	ctx, _ := testApex(t, `
 		apex {
diff --git a/java/androidmk.go b/java/androidmk.go
index f4e3c34..11fea82 100644
--- a/java/androidmk.go
+++ b/java/androidmk.go
@@ -24,7 +24,7 @@
 func (library *Library) AndroidMkEntriesHostDex() android.AndroidMkEntries {
 	hostDexNeeded := Bool(library.deviceProperties.Hostdex) && !library.Host()
 	if !library.IsForPlatform() {
-		// If the platform variant is available, don't emit hostdex modules from the APEX variants
+		// Don't emit hostdex modules from the APEX variants
 		hostDexNeeded = false
 	}
 
@@ -62,8 +62,14 @@
 	var entriesList []android.AndroidMkEntries
 
 	mainEntries := android.AndroidMkEntries{Disabled: true}
+
 	// For a java library built for an APEX, we don't need Make module
-	if library.IsForPlatform() {
+	hideFromMake := !library.IsForPlatform()
+	// If not available for platform, don't emit to make.
+	if !library.ApexModuleBase.AvailableFor(android.AvailableToPlatform) {
+		hideFromMake = true
+	}
+	if !hideFromMake {
 		mainEntries = android.AndroidMkEntries{
 			Class:      "JAVA_LIBRARIES",
 			OutputFile: android.OptionalPathForPath(library.outputFile),
diff --git a/java/app.go b/java/app.go
index 7595e36..9fa7179 100755
--- a/java/app.go
+++ b/java/app.go
@@ -636,7 +636,7 @@
 		fixedConfig := android.PathForModuleOut(ctx, "test_config_fixer", "AndroidTest.xml")
 		rule := android.NewRuleBuilder()
 		rule.Command().BuiltTool(ctx, "test_config_fixer").
-			FlagWithArg("--manifest ", a.manifestPath.String()).
+			FlagWithInput("--manifest ", a.manifestPath).
 			FlagWithArg("--package-name ", *a.overridableAppProperties.Package_name).
 			Input(a.testConfig).
 			Output(fixedConfig)
diff --git a/java/hiddenapi_singleton.go b/java/hiddenapi_singleton.go
index ad84cde..e9e4a45 100644
--- a/java/hiddenapi_singleton.go
+++ b/java/hiddenapi_singleton.go
@@ -16,6 +16,7 @@
 
 import (
 	"fmt"
+	"strings"
 
 	"android/soong/android"
 )
@@ -152,11 +153,23 @@
 		// Collect dex jar paths for modules that had hiddenapi encode called on them.
 		if h, ok := module.(hiddenAPIIntf); ok {
 			if jar := h.bootDexJar(); jar != nil {
-				// For a java lib included in an APEX, only take the one built for
-				// the platform variant, and skip the variants for APEXes.
-				// Otherwise, the hiddenapi tool will complain about duplicated classes
-				if a, ok := module.(android.ApexModule); ok {
-					if android.InAnyApex(module.Name()) && !a.IsForPlatform() {
+				// Don't add multiple variants of the same library to bootDexJars, otherwise
+				// hiddenapi tool will complain about duplicated classes. Such multiple variants
+				// of the same library can happen when the library is included in one or more APEXes.
+				// TODO(b/146308764): remove this heuristic
+				if a, ok := module.(android.ApexModule); ok && android.InAnyApex(module.Name()) {
+					if a.AvailableFor("//apex_available:platform") && !a.IsForPlatform() {
+						// skip the apex variants if the jar is available for the platform
+						return
+					}
+					apexName := a.ApexName()
+					if strings.Contains(apexName, "test") {
+						// skip the if the jar is in test APEX
+						return
+					}
+
+					if strings.Contains(apexName, "com.android.art") && apexName != "com.android.art.release" {
+						// skip the ART APEX variants other than com.android.art.release
 						return
 					}
 				}
diff --git a/java/java.go b/java/java.go
index d8db5f8..59bfaf7 100644
--- a/java/java.go
+++ b/java/java.go
@@ -559,6 +559,16 @@
 	return j.sdkVersion()
 }
 
+func (j *Module) AvailableFor(what string) bool {
+	if what == android.AvailableToPlatform && Bool(j.deviceProperties.Hostdex) {
+		// Exception: for hostdex: true libraries, the platform variant is created
+		// even if it's not marked as available to platform. In that case, the platform
+		// variant is used only for the hostdex and not installed to the device.
+		return true
+	}
+	return j.ApexModuleBase.AvailableFor(what)
+}
+
 func (j *Module) deps(ctx android.BottomUpMutatorContext) {
 	if ctx.Device() {
 		sdkDep := decodeSdkDep(ctx, sdkContext(j))