Merge master@5406228 into git_qt-dev-plus-aosp.

Change-Id: I28b591a5816adbefb89d2617e74f0aac5156325b
BUG: 129345239
diff --git a/Android.bp b/Android.bp
index 9ad1247..50f0603 100644
--- a/Android.bp
+++ b/Android.bp
@@ -55,6 +55,7 @@
         "android/namespace.go",
         "android/neverallow.go",
         "android/onceper.go",
+        "android/override_module.go",
         "android/package_ctx.go",
         "android/path_properties.go",
         "android/paths.go",
@@ -103,6 +104,7 @@
         "cc/config/global.go",
         "cc/config/tidy.go",
         "cc/config/toolchain.go",
+        "cc/config/vndk.go",
 
         "cc/config/arm_device.go",
         "cc/config/arm64_device.go",
diff --git a/android/androidmk.go b/android/androidmk.go
index fc34471..bd49e4c 100644
--- a/android/androidmk.go
+++ b/android/androidmk.go
@@ -319,7 +319,7 @@
 		}
 	}
 
-	if amod.noticeFile != nil {
+	if amod.noticeFile.Valid() {
 		fmt.Fprintln(&data.preamble, "LOCAL_NOTICE_FILE :=", amod.noticeFile.String())
 	}
 
diff --git a/android/config.go b/android/config.go
index 1520739..fff77ca 100644
--- a/android/config.go
+++ b/android/config.go
@@ -475,8 +475,12 @@
 	return *c.productVariables.DeviceName
 }
 
-func (c *config) ResourceOverlays() []string {
-	return c.productVariables.ResourceOverlays
+func (c *config) DeviceResourceOverlays() []string {
+	return c.productVariables.DeviceResourceOverlays
+}
+
+func (c *config) ProductResourceOverlays() []string {
+	return c.productVariables.ProductResourceOverlays
 }
 
 func (c *config) PlatformVersionName() string {
@@ -818,6 +822,10 @@
 	return c.config.productVariables.ExtraVndkVersions
 }
 
+func (c *deviceConfig) VndkUseCoreVariant() bool {
+	return Bool(c.config.productVariables.VndkUseCoreVariant)
+}
+
 func (c *deviceConfig) SystemSdkVersions() []string {
 	return c.config.productVariables.DeviceSystemSdkVersions
 }
diff --git a/android/module.go b/android/module.go
index afd2b71..abf2cae 100644
--- a/android/module.go
+++ b/android/module.go
@@ -189,6 +189,7 @@
 	InstallInRecovery() bool
 	SkipInstall()
 	ExportedToMake() bool
+	NoticeFile() OptionalPath
 
 	AddProperties(props ...interface{})
 	GetProperties() []interface{}
@@ -466,7 +467,7 @@
 	noAddressSanitizer bool
 	installFiles       Paths
 	checkbuildFiles    Paths
-	noticeFile         Path
+	noticeFile         OptionalPath
 
 	// Used by buildTargetSingleton to create checkbuild and per-directory build targets
 	// Only set on the final variant of each module
@@ -667,6 +668,10 @@
 	return String(a.commonProperties.Owner)
 }
 
+func (a *ModuleBase) NoticeFile() OptionalPath {
+	return a.noticeFile
+}
+
 func (a *ModuleBase) generateModuleTarget(ctx ModuleContext) {
 	allInstalledFiles := Paths{}
 	allCheckbuildFiles := Paths{}
@@ -852,9 +857,12 @@
 		a.installFiles = append(a.installFiles, ctx.installFiles...)
 		a.checkbuildFiles = append(a.checkbuildFiles, ctx.checkbuildFiles...)
 
-		if a.commonProperties.Notice != nil {
-			// For filegroup-based notice file references.
-			a.noticeFile = PathForModuleSrc(ctx, *a.commonProperties.Notice)
+		notice := proptools.StringDefault(a.commonProperties.Notice, "NOTICE")
+		if m := SrcIsModule(notice); m != "" {
+			a.noticeFile = ctx.ExpandOptionalSource(&notice, "notice")
+		} else {
+			noticePath := filepath.Join(ctx.ModuleDir(), notice)
+			a.noticeFile = ExistentPathForSource(ctx, noticePath)
 		}
 	}
 
diff --git a/android/mutator.go b/android/mutator.go
index 6c9f17a..71237a1 100644
--- a/android/mutator.go
+++ b/android/mutator.go
@@ -79,6 +79,7 @@
 	RegisterNamespaceMutator,
 	RegisterPrebuiltsPreArchMutators,
 	RegisterDefaultsPreArchMutators,
+	RegisterOverridePreArchMutators,
 }
 
 func registerArchMutator(ctx RegisterMutatorsContext) {
diff --git a/android/override_module.go b/android/override_module.go
new file mode 100644
index 0000000..02db359
--- /dev/null
+++ b/android/override_module.go
@@ -0,0 +1,213 @@
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package android
+
+// This file contains all the foundation components for override modules and their base module
+// types. Override modules are a kind of opposite of default modules in that they override certain
+// properties of an existing base module whereas default modules provide base module data to be
+// overridden. However, unlike default and defaultable module pairs, both override and overridable
+// modules generate and output build actions, and it is up to product make vars to decide which one
+// to actually build and install in the end. In other words, default modules and defaultable modules
+// can be compared to abstract classes and concrete classes in C++ and Java. By the same analogy,
+// both override and overridable modules act like concrete classes.
+//
+// There is one more crucial difference from the logic perspective. Unlike default pairs, most Soong
+// actions happen in the base (overridable) module by creating a local variant for each override
+// module based on it.
+
+import (
+	"sync"
+
+	"github.com/google/blueprint"
+	"github.com/google/blueprint/proptools"
+)
+
+// Interface for override module types, e.g. override_android_app, override_apex
+type OverrideModule interface {
+	Module
+
+	getOverridingProperties() []interface{}
+	setOverridingProperties(properties []interface{})
+
+	getOverrideModuleProperties() *OverrideModuleProperties
+}
+
+// Base module struct for override module types
+type OverrideModuleBase struct {
+	moduleProperties OverrideModuleProperties
+
+	overridingProperties []interface{}
+}
+
+type OverrideModuleProperties struct {
+	// Name of the base module to be overridden
+	Base *string
+
+	// TODO(jungjw): Add an optional override_name bool flag.
+}
+
+func (o *OverrideModuleBase) getOverridingProperties() []interface{} {
+	return o.overridingProperties
+}
+
+func (o *OverrideModuleBase) setOverridingProperties(properties []interface{}) {
+	o.overridingProperties = properties
+}
+
+func (o *OverrideModuleBase) getOverrideModuleProperties() *OverrideModuleProperties {
+	return &o.moduleProperties
+}
+
+func InitOverrideModule(m OverrideModule) {
+	m.setOverridingProperties(m.GetProperties())
+
+	m.AddProperties(m.getOverrideModuleProperties())
+}
+
+// Interface for overridable module types, e.g. android_app, apex
+type OverridableModule interface {
+	setOverridableProperties(prop []interface{})
+
+	addOverride(o OverrideModule)
+	getOverrides() []OverrideModule
+
+	override(ctx BaseModuleContext, o OverrideModule)
+
+	setOverridesProperty(overridesProperties *[]string)
+}
+
+// Base module struct for overridable module types
+type OverridableModuleBase struct {
+	ModuleBase
+
+	// List of OverrideModules that override this base module
+	overrides []OverrideModule
+	// Used to parallelize registerOverrideMutator executions. Note that only addOverride locks this
+	// mutex. It is because addOverride and getOverride are used in different mutators, and so are
+	// guaranteed to be not mixed. (And, getOverride only reads from overrides, and so don't require
+	// mutex locking.)
+	overridesLock sync.Mutex
+
+	overridableProperties []interface{}
+
+	// If an overridable module has a property to list other modules that itself overrides, it should
+	// set this to a pointer to the property through the InitOverridableModule function, so that
+	// override information is propagated and aggregated correctly.
+	overridesProperty *[]string
+}
+
+func InitOverridableModule(m OverridableModule, overridesProperty *[]string) {
+	m.setOverridableProperties(m.(Module).GetProperties())
+	m.setOverridesProperty(overridesProperty)
+}
+
+func (b *OverridableModuleBase) setOverridableProperties(prop []interface{}) {
+	b.overridableProperties = prop
+}
+
+func (b *OverridableModuleBase) addOverride(o OverrideModule) {
+	b.overridesLock.Lock()
+	b.overrides = append(b.overrides, o)
+	b.overridesLock.Unlock()
+}
+
+// Should NOT be used in the same mutator as addOverride.
+func (b *OverridableModuleBase) getOverrides() []OverrideModule {
+	return b.overrides
+}
+
+func (b *OverridableModuleBase) setOverridesProperty(overridesProperty *[]string) {
+	b.overridesProperty = overridesProperty
+}
+
+// Overrides a base module with the given OverrideModule.
+func (b *OverridableModuleBase) override(ctx BaseModuleContext, o OverrideModule) {
+	for _, p := range b.overridableProperties {
+		for _, op := range o.getOverridingProperties() {
+			if proptools.TypeEqual(p, op) {
+				err := proptools.PrependProperties(p, op, nil)
+				if err != nil {
+					if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
+						ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
+					} else {
+						panic(err)
+					}
+				}
+			}
+		}
+	}
+	// Adds the base module to the overrides property, if exists, of the overriding module. See the
+	// comment on OverridableModuleBase.overridesProperty for details.
+	if b.overridesProperty != nil {
+		*b.overridesProperty = append(*b.overridesProperty, b.Name())
+	}
+	// The base module name property has to be updated separately for Name() to work as intended.
+	b.module.base().nameProperties.Name = proptools.StringPtr(o.Name())
+}
+
+// Mutators for override/overridable modules. All the fun happens in these functions. It is critical
+// to keep them in this order and not put any order mutators between them.
+func RegisterOverridePreArchMutators(ctx RegisterMutatorsContext) {
+	ctx.BottomUp("override_deps", overrideModuleDepsMutator).Parallel()
+	ctx.TopDown("register_override", registerOverrideMutator).Parallel()
+	ctx.BottomUp("perform_override", performOverrideMutator).Parallel()
+}
+
+type overrideBaseDependencyTag struct {
+	blueprint.BaseDependencyTag
+}
+
+var overrideBaseDepTag overrideBaseDependencyTag
+
+// Adds dependency on the base module to the overriding module so that they can be visited in the
+// next phase.
+func overrideModuleDepsMutator(ctx BottomUpMutatorContext) {
+	if module, ok := ctx.Module().(OverrideModule); ok {
+		ctx.AddDependency(ctx.Module(), overrideBaseDepTag, *module.getOverrideModuleProperties().Base)
+	}
+}
+
+// Visits the base module added as a dependency above, checks the module type, and registers the
+// overriding module.
+func registerOverrideMutator(ctx TopDownMutatorContext) {
+	ctx.VisitDirectDepsWithTag(overrideBaseDepTag, func(base Module) {
+		if o, ok := base.(OverridableModule); ok {
+			o.addOverride(ctx.Module().(OverrideModule))
+		} else {
+			ctx.PropertyErrorf("base", "unsupported base module type")
+		}
+	})
+}
+
+// Now, goes through all overridable modules, finds all modules overriding them, creates a local
+// variant for each of them, and performs the actual overriding operation by calling override().
+func performOverrideMutator(ctx BottomUpMutatorContext) {
+	if b, ok := ctx.Module().(OverridableModule); ok {
+		overrides := b.getOverrides()
+		if len(overrides) == 0 {
+			return
+		}
+		variants := make([]string, len(overrides)+1)
+		// The first variant is for the original, non-overridden, base module.
+		variants[0] = ""
+		for i, o := range overrides {
+			variants[i+1] = o.(Module).Name()
+		}
+		mods := ctx.CreateLocalVariations(variants...)
+		for i, o := range overrides {
+			mods[i+1].(OverridableModule).override(ctx, o)
+		}
+	}
+}
diff --git a/android/sh_binary.go b/android/sh_binary.go
index bee61ef..cf415c5 100644
--- a/android/sh_binary.go
+++ b/android/sh_binary.go
@@ -139,6 +139,7 @@
 
 func (s *ShTest) AndroidMk() AndroidMkData {
 	data := s.ShBinary.AndroidMk()
+	data.Class = "NATIVE_TESTS"
 	data.Extra = append(data.Extra, func(w io.Writer, outputFile Path) {
 		fmt.Fprintln(w, "LOCAL_COMPATIBILITY_SUITE :=",
 			strings.Join(s.testProperties.Test_suites, " "))
diff --git a/android/variable.go b/android/variable.go
index 1c17e5a..aa8c804 100644
--- a/android/variable.go
+++ b/android/variable.go
@@ -165,7 +165,8 @@
 	CrossHostArch          *string `json:",omitempty"`
 	CrossHostSecondaryArch *string `json:",omitempty"`
 
-	ResourceOverlays           []string `json:",omitempty"`
+	DeviceResourceOverlays     []string `json:",omitempty"`
+	ProductResourceOverlays    []string `json:",omitempty"`
 	EnforceRROTargets          []string `json:",omitempty"`
 	EnforceRROExcludedOverlays []string `json:",omitempty"`
 
@@ -253,6 +254,8 @@
 
 	PgoAdditionalProfileDirs []string `json:",omitempty"`
 
+	VndkUseCoreVariant *bool `json:",omitempty"`
+
 	BoardVendorSepolicyDirs      []string `json:",omitempty"`
 	BoardOdmSepolicyDirs         []string `json:",omitempty"`
 	BoardPlatPublicSepolicyDirs  []string `json:",omitempty"`
diff --git a/androidmk/cmd/androidmk/android.go b/androidmk/cmd/androidmk/android.go
index 9276eb5..52bcf9c 100644
--- a/androidmk/cmd/androidmk/android.go
+++ b/androidmk/cmd/androidmk/android.go
@@ -201,6 +201,7 @@
 			"LOCAL_DEX_PREOPT_GENERATE_PROFILE": "dex_preopt.profile_guided",
 
 			"LOCAL_PRIVATE_PLATFORM_APIS": "platform_apis",
+			"LOCAL_JETIFIER_ENABLED":      "jetifier",
 		})
 }
 
diff --git a/androidmk/cmd/androidmk/androidmk_test.go b/androidmk/cmd/androidmk/androidmk_test.go
index b2c3fab..f2dc6ff 100644
--- a/androidmk/cmd/androidmk/androidmk_test.go
+++ b/androidmk/cmd/androidmk/androidmk_test.go
@@ -631,12 +631,14 @@
 			LOCAL_SRC_FILES := test.jar
 			LOCAL_MODULE_CLASS := JAVA_LIBRARIES
 			LOCAL_STATIC_ANDROID_LIBRARIES :=
+			LOCAL_JETIFIER_ENABLED := true
 			include $(BUILD_PREBUILT)
 		`,
 		expected: `
 			java_import {
 				jars: ["test.jar"],
 
+				jetifier: true,
 			}
 		`,
 	},
diff --git a/apex/apex.go b/apex/apex.go
index ad1ef74..c1f52a6 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -90,6 +90,12 @@
 		CommandDeps: []string{"${zip2zip}"},
 		Description: "app bundle",
 	}, "abi")
+
+	apexMergeNoticeRule = pctx.StaticRule("apexMergeNoticeRule", blueprint.RuleParams{
+		Command:     `${mergenotice} --output $out $inputs`,
+		CommandDeps: []string{"${mergenotice}"},
+		Description: "merge notice files into $out",
+	}, "inputs")
 )
 
 var imageApexSuffix = ".apex"
@@ -138,6 +144,8 @@
 	pctx.HostBinToolVariable("zip2zip", "zip2zip")
 	pctx.HostBinToolVariable("zipalign", "zipalign")
 
+	pctx.SourcePathVariable("mergenotice", "build/soong/scripts/mergenotice.py")
+
 	android.RegisterModuleType("apex", apexBundleFactory)
 	android.RegisterModuleType("apex_test", testApexBundleFactory)
 	android.RegisterModuleType("apex_defaults", defaultsFactory)
@@ -394,6 +402,8 @@
 	container_certificate_file android.Path
 	container_private_key_file android.Path
 
+	mergedNoticeFile android.WritablePath
+
 	// list of files to be included in this apex
 	filesInfo []apexFile
 
@@ -814,6 +824,8 @@
 	a.installDir = android.PathForModuleInstall(ctx, "apex")
 	a.filesInfo = filesInfo
 
+	a.buildNoticeFile(ctx)
+
 	if a.apexTypes.zip() {
 		a.buildUnflattenedApex(ctx, zipApex)
 	}
@@ -827,6 +839,37 @@
 	}
 }
 
+func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext) {
+	noticeFiles := []android.Path{}
+	noticeFilesString := []string{}
+	for _, f := range a.filesInfo {
+		if f.module != nil {
+			notice := f.module.NoticeFile()
+			if notice.Valid() {
+				noticeFiles = append(noticeFiles, notice.Path())
+				noticeFilesString = append(noticeFilesString, notice.Path().String())
+			}
+		}
+	}
+	// append the notice file specified in the apex module itself
+	if a.NoticeFile().Valid() {
+		noticeFiles = append(noticeFiles, a.NoticeFile().Path())
+		noticeFilesString = append(noticeFilesString, a.NoticeFile().Path().String())
+	}
+
+	if len(noticeFiles) > 0 {
+		a.mergedNoticeFile = android.PathForModuleOut(ctx, "NOTICE")
+		ctx.Build(pctx, android.BuildParams{
+			Rule:   apexMergeNoticeRule,
+			Inputs: noticeFiles,
+			Output: a.mergedNoticeFile,
+			Args: map[string]string{
+				"inputs": strings.Join(noticeFilesString, " "),
+			},
+		})
+	}
+}
+
 func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
 	cert := String(a.properties.Certificate)
 	if cert != "" && android.SrcIsModule(cert) == "" {
@@ -1078,6 +1121,10 @@
 			if len(fi.symlinks) > 0 {
 				fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
 			}
+
+			if fi.module != nil && fi.module.NoticeFile().Valid() {
+				fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
+			}
 		} else {
 			fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
 		}
@@ -1168,6 +1215,9 @@
 				fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
 				fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
 				fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", String(a.properties.Key))
+				if a.installable() && a.mergedNoticeFile != nil {
+					fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", a.mergedNoticeFile.String())
+				}
 				if len(moduleNames) > 0 {
 					fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
 				}
diff --git a/apex/apex_test.go b/apex/apex_test.go
index f221cf2..8a2e55a 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -159,6 +159,10 @@
 		"testkey.override.pk8":                 nil,
 		"vendor/foo/devkeys/testkey.avbpubkey": nil,
 		"vendor/foo/devkeys/testkey.pem":       nil,
+		"NOTICE":                               nil,
+		"custom_notice":                        nil,
+		"testkey2.avbpubkey":                   nil,
+		"testkey2.pem":                         nil,
 	})
 	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
 	android.FailIfErrored(t, errs)
@@ -280,6 +284,7 @@
 			srcs: ["mylib.cpp"],
 			system_shared_libs: [],
 			stl: "none",
+			notice: "custom_notice",
 		}
 	`)
 
@@ -319,6 +324,14 @@
 	if !good {
 		t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
 	}
+
+	apexMergeNoticeRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexMergeNoticeRule")
+	noticeInputs := strings.Split(apexMergeNoticeRule.Args["inputs"], " ")
+	if len(noticeInputs) != 3 {
+		t.Errorf("number of input notice files: expected = 3, actual = %d", len(noticeInputs))
+	}
+	ensureListContains(t, noticeInputs, "NOTICE")
+	ensureListContains(t, noticeInputs, "custom_notice")
 }
 
 func TestBasicZipApex(t *testing.T) {
@@ -1183,3 +1196,36 @@
 	}
 
 }
+
+func TestApexKeyFromOtherModule(t *testing.T) {
+	ctx := testApex(t, `
+		apex_key {
+			name: "myapex.key",
+			public_key: ":my.avbpubkey",
+			private_key: ":my.pem",
+			product_specific: true,
+		}
+
+		filegroup {
+			name: "my.avbpubkey",
+			srcs: ["testkey2.avbpubkey"],
+		}
+
+		filegroup {
+			name: "my.pem",
+			srcs: ["testkey2.pem"],
+		}
+	`)
+
+	apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
+	expected_pubkey := "testkey2.avbpubkey"
+	actual_pubkey := apex_key.public_key_file.String()
+	if actual_pubkey != expected_pubkey {
+		t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
+	}
+	expected_privkey := "testkey2.pem"
+	actual_privkey := apex_key.private_key_file.String()
+	if actual_privkey != expected_privkey {
+		t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
+	}
+}
diff --git a/apex/key.go b/apex/key.go
index 07c3105..848e8ce 100644
--- a/apex/key.go
+++ b/apex/key.go
@@ -45,11 +45,11 @@
 }
 
 type apexKeyProperties struct {
-	// Path to the public key file in avbpubkey format. Installed to the device.
+	// Path or module to the public key file in avbpubkey format. Installed to the device.
 	// Base name of the file is used as the ID for the key.
-	Public_key *string
-	// Path to the private key file in pem format. Used to sign APEXs.
-	Private_key *string
+	Public_key *string `android:"path"`
+	// Path or module to the private key file in pem format. Used to sign APEXs.
+	Private_key *string `android:"path"`
 
 	// Whether this key is installable to one of the partitions. Defualt: true.
 	Installable *bool
@@ -68,15 +68,26 @@
 }
 
 func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) {
-	m.public_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key))
-	m.private_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key))
-
-	// If not found, fall back to the local key pairs
-	if !android.ExistentPathForSource(ctx, m.public_key_file.String()).Valid() {
+	// If the keys are from other modules (i.e. :module syntax) respect it.
+	// Otherwise, try to locate the key files in the default cert dir or
+	// in the local module dir
+	if android.SrcIsModule(String(m.properties.Public_key)) != "" {
 		m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
+	} else {
+		m.public_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key))
+		// If not found, fall back to the local key pairs
+		if !android.ExistentPathForSource(ctx, m.public_key_file.String()).Valid() {
+			m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
+		}
 	}
-	if !android.ExistentPathForSource(ctx, m.private_key_file.String()).Valid() {
+
+	if android.SrcIsModule(String(m.properties.Private_key)) != "" {
 		m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
+	} else {
+		m.private_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key))
+		if !android.ExistentPathForSource(ctx, m.private_key_file.String()).Valid() {
+			m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
+		}
 	}
 
 	pubKeyName := m.public_key_file.Base()[0 : len(m.public_key_file.Base())-len(m.public_key_file.Ext())]
diff --git a/cc/androidmk.go b/cc/androidmk.go
index d229d0c..02806f9 100644
--- a/cc/androidmk.go
+++ b/cc/androidmk.go
@@ -185,6 +185,12 @@
 		if library.coverageOutputFile.Valid() {
 			fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", library.coverageOutputFile.String())
 		}
+
+		if library.useCoreVariant {
+			fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
+			fmt.Fprintln(w, "LOCAL_NO_NOTICE_FILE := true")
+			fmt.Fprintln(w, "LOCAL_VNDK_DEPEND_ON_CORE_VARIANT := true")
+		}
 	})
 
 	if library.shared() && !library.buildStubs() {
diff --git a/cc/cc.go b/cc/cc.go
index c036b84..c80d00c 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -266,6 +266,7 @@
 	hasStubsVariants() bool
 	isStubs() bool
 	bootstrap() bool
+	mustUseVendorVariant() bool
 }
 
 type ModuleContext interface {
@@ -558,6 +559,10 @@
 	return false
 }
 
+func (c *Module) mustUseVendorVariant() bool {
+	return c.isVndkSp() || inList(c.Name(), config.VndkMustUseVendorVariantList)
+}
+
 func (c *Module) getVndkExtendsModuleName() string {
 	if vndkdep := c.vndkdep; vndkdep != nil {
 		return vndkdep.getVndkExtendsModuleName()
@@ -709,6 +714,10 @@
 	return ctx.mod.isVndkExt()
 }
 
+func (ctx *moduleContextImpl) mustUseVendorVariant() bool {
+	return ctx.mod.mustUseVendorVariant()
+}
+
 func (ctx *moduleContextImpl) inRecovery() bool {
 	return ctx.mod.inRecovery()
 }
@@ -1769,7 +1778,12 @@
 			isLLndk := inList(libName, llndkLibraries)
 			isVendorPublicLib := inList(libName, vendorPublicLibraries)
 			bothVendorAndCoreVariantsExist := ccDep.hasVendorVariant() || isLLndk
-			if c.useVndk() && bothVendorAndCoreVariantsExist {
+
+			if ctx.DeviceConfig().VndkUseCoreVariant() && ccDep.isVndk() && !ccDep.mustUseVendorVariant() {
+				// The vendor module is a no-vendor-variant VNDK library.  Depend on the
+				// core module instead.
+				return libName
+			} else if c.useVndk() && bothVendorAndCoreVariantsExist {
 				// The vendor module in Make will have been renamed to not conflict with the core
 				// module, so update the dependency name here accordingly.
 				return libName + vendorSuffix
@@ -1908,6 +1922,8 @@
 		// TODO(b/114741097): use the correct ndk stl once build errors have been fixed
 		//family, link := getNdkStlFamilyAndLinkType(c)
 		//return fmt.Sprintf("native:ndk:%s:%s", family, link)
+	} else if inList(c.Name(), vndkUsingCoreVariantLibraries) {
+		return "native:platform_vndk"
 	} else {
 		return "native:platform"
 	}
diff --git a/cc/config/vndk.go b/cc/config/vndk.go
new file mode 100644
index 0000000..542f737
--- /dev/null
+++ b/cc/config/vndk.go
@@ -0,0 +1,157 @@
+// Copyright 2019 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package config
+
+// List of VNDK libraries that have different core variant and vendor variant.
+// For these libraries, the vendor variants must be installed even if the device
+// has VndkUseCoreVariant set.
+var VndkMustUseVendorVariantList = []string{
+	"android.frameworks.sensorservice@1.0",
+	"android.hardware.atrace@1.0",
+	"android.hardware.audio.common@5.0",
+	"android.hardware.audio.effect@2.0",
+	"android.hardware.audio.effect@4.0",
+	"android.hardware.audio.effect@5.0",
+	"android.hardware.audio@2.0",
+	"android.hardware.audio@4.0",
+	"android.hardware.audio@5.0",
+	"android.hardware.automotive.evs@1.0",
+	"android.hardware.automotive.vehicle@2.0",
+	"android.hardware.bluetooth.audio@2.0",
+	"android.hardware.boot@1.0",
+	"android.hardware.broadcastradio@1.0",
+	"android.hardware.broadcastradio@1.1",
+	"android.hardware.broadcastradio@2.0",
+	"android.hardware.camera.device@1.0",
+	"android.hardware.camera.device@3.2",
+	"android.hardware.camera.device@3.3",
+	"android.hardware.camera.device@3.4",
+	"android.hardware.camera.provider@2.4",
+	"android.hardware.cas.native@1.0",
+	"android.hardware.cas@1.0",
+	"android.hardware.configstore@1.0",
+	"android.hardware.configstore@1.1",
+	"android.hardware.contexthub@1.0",
+	"android.hardware.drm@1.0",
+	"android.hardware.drm@1.1",
+	"android.hardware.fastboot@1.0",
+	"android.hardware.gatekeeper@1.0",
+	"android.hardware.gnss@1.0",
+	"android.hardware.graphics.allocator@2.0",
+	"android.hardware.graphics.bufferqueue@1.0",
+	"android.hardware.graphics.composer@2.1",
+	"android.hardware.graphics.composer@2.2",
+	"android.hardware.health@1.0",
+	"android.hardware.health@2.0",
+	"android.hardware.ir@1.0",
+	"android.hardware.keymaster@3.0",
+	"android.hardware.keymaster@4.0",
+	"android.hardware.light@2.0",
+	"android.hardware.media.bufferpool@1.0",
+	"android.hardware.media.omx@1.0",
+	"android.hardware.memtrack@1.0",
+	"android.hardware.neuralnetworks@1.0",
+	"android.hardware.neuralnetworks@1.1",
+	"android.hardware.neuralnetworks@1.2",
+	"android.hardware.nfc@1.1",
+	"android.hardware.nfc@1.2",
+	"android.hardware.oemlock@1.0",
+	"android.hardware.power.stats@1.0",
+	"android.hardware.power@1.0",
+	"android.hardware.power@1.1",
+	"android.hardware.radio@1.4",
+	"android.hardware.secure_element@1.0",
+	"android.hardware.sensors@1.0",
+	"android.hardware.soundtrigger@2.0",
+	"android.hardware.soundtrigger@2.0-core",
+	"android.hardware.soundtrigger@2.1",
+	"android.hardware.tetheroffload.config@1.0",
+	"android.hardware.tetheroffload.control@1.0",
+	"android.hardware.thermal@1.0",
+	"android.hardware.tv.cec@1.0",
+	"android.hardware.tv.input@1.0",
+	"android.hardware.vibrator@1.0",
+	"android.hardware.vibrator@1.1",
+	"android.hardware.vibrator@1.2",
+	"android.hardware.weaver@1.0",
+	"android.hardware.wifi.hostapd@1.0",
+	"android.hardware.wifi.offload@1.0",
+	"android.hardware.wifi.supplicant@1.0",
+	"android.hardware.wifi.supplicant@1.1",
+	"android.hardware.wifi@1.0",
+	"android.hardware.wifi@1.1",
+	"android.hardware.wifi@1.2",
+	"android.hardwareundtrigger@2.0",
+	"android.hardwareundtrigger@2.0-core",
+	"android.hardwareundtrigger@2.1",
+	"android.hidl.allocator@1.0",
+	"android.hidl.token@1.0",
+	"android.hidl.token@1.0-utils",
+	"android.system.net.netd@1.0",
+	"android.system.wifi.keystore@1.0",
+	"libaudioroute",
+	"libaudioutils",
+	"libbinder",
+	"libcamera_metadata",
+	"libcrypto",
+	"libdiskconfig",
+	"libdumpstateutil",
+	"libexpat",
+	"libfmq",
+	"libgui",
+	"libhidlcache",
+	"libmedia_helper",
+	"libmedia_omx",
+	"libmemtrack",
+	"libnetutils",
+	"libpuresoftkeymasterdevice",
+	"libradio_metadata",
+	"libselinux",
+	"libsoftkeymasterdevice",
+	"libsqlite",
+	"libssl",
+	"libstagefright_bufferqueue_helper",
+	"libstagefright_flacdec",
+	"libstagefright_foundation",
+	"libstagefright_omx",
+	"libstagefright_omx_utils",
+	"libstagefright_soft_aacdec",
+	"libstagefright_soft_aacenc",
+	"libstagefright_soft_amrdec",
+	"libstagefright_soft_amrnbenc",
+	"libstagefright_soft_amrwbenc",
+	"libstagefright_soft_avcdec",
+	"libstagefright_soft_avcenc",
+	"libstagefright_soft_flacdec",
+	"libstagefright_soft_flacenc",
+	"libstagefright_soft_g711dec",
+	"libstagefright_soft_gsmdec",
+	"libstagefright_soft_hevcdec",
+	"libstagefright_soft_mp3dec",
+	"libstagefright_soft_mpeg2dec",
+	"libstagefright_soft_mpeg4dec",
+	"libstagefright_soft_mpeg4enc",
+	"libstagefright_soft_opusdec",
+	"libstagefright_soft_rawdec",
+	"libstagefright_soft_vorbisdec",
+	"libstagefright_soft_vpxdec",
+	"libstagefright_soft_vpxenc",
+	"libstagefright_xmlparser",
+	"libsysutils",
+	"libui",
+	"libvorbisidec",
+	"libxml2",
+	"libziparchive",
+}
diff --git a/cc/library.go b/cc/library.go
index 7216832..e5bb347 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -293,6 +293,10 @@
 
 	post_install_cmds []string
 
+	// If useCoreVariant is true, the vendor variant of a VNDK library is
+	// not installed.
+	useCoreVariant bool
+
 	// Decorated interafaces
 	*baseCompiler
 	*baseLinker
@@ -914,6 +918,9 @@
 			if ctx.isVndkSp() {
 				library.baseInstaller.subDir = "vndk-sp"
 			} else if ctx.isVndk() {
+				if ctx.DeviceConfig().VndkUseCoreVariant() && !ctx.mustUseVendorVariant() {
+					library.useCoreVariant = true
+				}
 				library.baseInstaller.subDir = "vndk"
 			}
 
diff --git a/cc/lto.go b/cc/lto.go
index 0d7a246..1084869 100644
--- a/cc/lto.go
+++ b/cc/lto.go
@@ -91,22 +91,22 @@
 		flags.CFlags = append(flags.CFlags, ltoFlag)
 		flags.LdFlags = append(flags.LdFlags, ltoFlag)
 
-		if ctx.Config().IsEnvTrue("USE_THINLTO_CACHE") && Bool(lto.Properties.Lto.Thin) && !lto.useClangLld(ctx) {
+		if ctx.Config().IsEnvTrue("USE_THINLTO_CACHE") && Bool(lto.Properties.Lto.Thin) && lto.useClangLld(ctx) {
 			// Set appropriate ThinLTO cache policy
-			cacheDirFormat := "-Wl,-plugin-opt,cache-dir="
+			cacheDirFormat := "-Wl,--thinlto-cache-dir="
 			cacheDir := android.PathForOutput(ctx, "thinlto-cache").String()
 			flags.LdFlags = append(flags.LdFlags, cacheDirFormat+cacheDir)
 
 			// Limit the size of the ThinLTO cache to the lesser of 10% of available
 			// disk space and 10GB.
-			cachePolicyFormat := "-Wl,-plugin-opt,cache-policy="
+			cachePolicyFormat := "-Wl,--thinlto-cache-policy="
 			policy := "cache_size=10%:cache_size_bytes=10g"
 			flags.LdFlags = append(flags.LdFlags, cachePolicyFormat+policy)
 		}
 
 		// If the module does not have a profile, be conservative and do not inline
 		// or unroll loops during LTO, in order to prevent significant size bloat.
-		if !ctx.isPgoCompile() && !lto.useClangLld(ctx) {
+		if !ctx.isPgoCompile() {
 			flags.LdFlags = append(flags.LdFlags, "-Wl,-plugin-opt,-inline-threshold=0")
 			flags.LdFlags = append(flags.LdFlags, "-Wl,-plugin-opt,-unroll-threshold=0")
 		}
diff --git a/cc/makevars.go b/cc/makevars.go
index 4a9ade2..aa6fdea 100644
--- a/cc/makevars.go
+++ b/cc/makevars.go
@@ -97,6 +97,7 @@
 	ctx.Strict("VNDK_SAMEPROCESS_LIBRARIES", strings.Join(vndkSpLibraries, " "))
 	ctx.Strict("LLNDK_LIBRARIES", strings.Join(llndkLibraries, " "))
 	ctx.Strict("VNDK_PRIVATE_LIBRARIES", strings.Join(vndkPrivateLibraries, " "))
+	ctx.Strict("VNDK_USING_CORE_VARIANT_LIBRARIES", strings.Join(vndkUsingCoreVariantLibraries, " "))
 
 	// Filter vendor_public_library that are exported to make
 	exportedVendorPublicLibraries := []string{}
diff --git a/cc/sanitize.go b/cc/sanitize.go
index 26418ad..2d80c22 100644
--- a/cc/sanitize.go
+++ b/cc/sanitize.go
@@ -39,7 +39,8 @@
 	// the default.
 	hwasanCflags = []string{"-fno-omit-frame-pointer", "-Wno-frame-larger-than=",
 		"-mllvm", "-hwasan-create-frame-descriptions=0",
-		"-mllvm", "-hwasan-allow-ifunc"}
+		"-mllvm", "-hwasan-allow-ifunc",
+		"-fsanitize-hwaddress-abi=platform"}
 
 	cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
 		"-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
diff --git a/cc/stl.go b/cc/stl.go
index 5e61e1e..e59f677 100644
--- a/cc/stl.go
+++ b/cc/stl.go
@@ -66,7 +66,7 @@
 		}
 		if ctx.useSdk() && ctx.Device() {
 			switch s {
-			case "":
+			case "", "system":
 				return "ndk_system"
 			case "c++_shared", "c++_static":
 				return "ndk_lib" + s
diff --git a/cc/test.go b/cc/test.go
index c3fadbd..045cc4f 100644
--- a/cc/test.go
+++ b/cc/test.go
@@ -30,6 +30,12 @@
 	Isolated *bool
 }
 
+// Test option struct.
+type TestOptions struct {
+	// The UID that you want to run the test as on a device.
+	Run_test_as *string
+}
+
 type TestBinaryProperties struct {
 	// Create a separate binary for each source file.  Useful when there is
 	// global state that can not be torn down and reset between each test suite.
@@ -55,6 +61,9 @@
 	// the name of the test configuration template (for example "AndroidTestTemplate.xml") that
 	// should be installed with the module.
 	Test_config_template *string `android:"path,arch_variant"`
+
+	// Test options.
+	Test_options TestOptions
 }
 
 func init() {
@@ -258,6 +267,11 @@
 	if Bool(test.testDecorator.Properties.Isolated) {
 		optionsMap["not-shardable"] = "true"
 	}
+
+	if test.Properties.Test_options.Run_test_as != nil {
+		optionsMap["run-test-as"] = String(test.Properties.Test_options.Run_test_as)
+	}
+
 	test.testConfig = tradefed.AutoGenNativeTestConfig(ctx, test.Properties.Test_config,
 		test.Properties.Test_config_template,
 		test.Properties.Test_suites, optionsMap)
diff --git a/cc/vndk.go b/cc/vndk.go
index 623097d..44a83e7 100644
--- a/cc/vndk.go
+++ b/cc/vndk.go
@@ -21,6 +21,7 @@
 	"sync"
 
 	"android/soong/android"
+	"android/soong/cc/config"
 )
 
 type VndkProperties struct {
@@ -191,11 +192,12 @@
 }
 
 var (
-	vndkCoreLibraries    []string
-	vndkSpLibraries      []string
-	llndkLibraries       []string
-	vndkPrivateLibraries []string
-	vndkLibrariesLock    sync.Mutex
+	vndkCoreLibraries             []string
+	vndkSpLibraries               []string
+	llndkLibraries                []string
+	vndkPrivateLibraries          []string
+	vndkUsingCoreVariantLibraries []string
+	vndkLibrariesLock             sync.Mutex
 )
 
 // gather list of vndk-core, vndk-sp, and ll-ndk libs
@@ -223,6 +225,12 @@
 				if m.vndkdep.isVndk() && !m.vndkdep.isVndkExt() {
 					vndkLibrariesLock.Lock()
 					defer vndkLibrariesLock.Unlock()
+					if mctx.DeviceConfig().VndkUseCoreVariant() && !inList(name, config.VndkMustUseVendorVariantList) {
+						if !inList(name, vndkUsingCoreVariantLibraries) {
+							vndkUsingCoreVariantLibraries = append(vndkUsingCoreVariantLibraries, name)
+							sort.Strings(vndkUsingCoreVariantLibraries)
+						}
+					}
 					if m.vndkdep.isVndkSp() {
 						if !inList(name, vndkSpLibraries) {
 							vndkSpLibraries = append(vndkSpLibraries, name)
diff --git a/java/aar.go b/java/aar.go
index 1a5ea4d..a993bf6 100644
--- a/java/aar.go
+++ b/java/aar.go
@@ -27,7 +27,7 @@
 	Dependency
 	ExportPackage() android.Path
 	ExportedProguardFlagFiles() android.Paths
-	ExportedRRODirs() android.Paths
+	ExportedRRODirs() []rroDir
 	ExportedStaticPackages() android.Paths
 	ExportedManifest() android.Path
 }
@@ -75,7 +75,7 @@
 	exportPackage         android.Path
 	manifestPath          android.Path
 	proguardOptionsFile   android.Path
-	rroDirs               android.Paths
+	rroDirs               []rroDir
 	rTxt                  android.Path
 	extraAaptPackagesFile android.Path
 	isLibrary             bool
@@ -99,7 +99,7 @@
 	return a.exportPackage
 }
 
-func (a *aapt) ExportedRRODirs() android.Paths {
+func (a *aapt) ExportedRRODirs() []rroDir {
 	return a.rroDirs
 }
 
@@ -108,7 +108,7 @@
 }
 
 func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext sdkContext, manifestPath android.Path) (flags []string,
-	deps android.Paths, resDirs, overlayDirs []globbedResourceDir, rroDirs, resZips android.Paths) {
+	deps android.Paths, resDirs, overlayDirs []globbedResourceDir, rroDirs []rroDir, resZips android.Paths) {
 
 	hasVersionCode := false
 	hasVersionName := false
@@ -286,8 +286,8 @@
 }
 
 // aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
-func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext) (transitiveStaticLibs, staticLibManifests,
-	staticRRODirs, deps android.Paths, flags []string) {
+func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext) (transitiveStaticLibs, staticLibManifests android.Paths,
+	staticRRODirs []rroDir, deps android.Paths, flags []string) {
 
 	var sharedLibs android.Paths
 
@@ -315,7 +315,16 @@
 				transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...)
 				transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
 				staticLibManifests = append(staticLibManifests, aarDep.ExportedManifest())
-				staticRRODirs = append(staticRRODirs, aarDep.ExportedRRODirs()...)
+
+			outer:
+				for _, d := range aarDep.ExportedRRODirs() {
+					for _, e := range staticRRODirs {
+						if d.path == e.path {
+							continue outer
+						}
+					}
+					staticRRODirs = append(staticRRODirs, d)
+				}
 			}
 		}
 	})
@@ -332,7 +341,6 @@
 	}
 
 	transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs)
-	staticRRODirs = android.FirstUniquePaths(staticRRODirs)
 
 	return transitiveStaticLibs, staticLibManifests, staticRRODirs, deps, flags
 }
@@ -438,7 +446,7 @@
 	Libs        []string
 
 	// if set to true, run Jetifier against .aar file. Defaults to false.
-	Jetifier_enabled *bool
+	Jetifier *bool
 }
 
 type AARImport struct {
@@ -482,7 +490,7 @@
 	return android.Paths{a.proguardFlags}
 }
 
-func (a *AARImport) ExportedRRODirs() android.Paths {
+func (a *AARImport) ExportedRRODirs() []rroDir {
 	return nil
 }
 
@@ -532,7 +540,7 @@
 	aarName := ctx.ModuleName() + ".aar"
 	var aar android.Path
 	aar = android.PathForModuleSrc(ctx, a.properties.Aars[0])
-	if Bool(a.properties.Jetifier_enabled) {
+	if Bool(a.properties.Jetifier) {
 		inputFile := aar
 		aar = android.PathForModuleOut(ctx, "jetifier", aarName)
 		TransformJetifier(ctx, aar.(android.WritablePath), inputFile)
diff --git a/java/android_resources.go b/java/android_resources.go
index 44cb709..c2bc746 100644
--- a/java/android_resources.go
+++ b/java/android_resources.go
@@ -41,9 +41,22 @@
 	return ctx.GlobFiles(filepath.Join(dir.String(), "**/*"), androidResourceIgnoreFilenames)
 }
 
+type overlayType int
+
+const (
+	device overlayType = iota + 1
+	product
+)
+
+type rroDir struct {
+	path        android.Path
+	overlayType overlayType
+}
+
 type overlayGlobResult struct {
-	dir   string
-	paths android.DirectorySortedPaths
+	dir         string
+	paths       android.DirectorySortedPaths
+	overlayType overlayType
 }
 
 var overlayDataKey = android.NewOnceKey("overlayDataKey")
@@ -54,7 +67,7 @@
 }
 
 func overlayResourceGlob(ctx android.ModuleContext, dir android.Path) (res []globbedResourceDir,
-	rroDirs android.Paths) {
+	rroDirs []rroDir) {
 
 	overlayData := ctx.Config().Get(overlayDataKey).([]overlayGlobResult)
 
@@ -70,7 +83,7 @@
 			// exclusion list, ignore the overlay.  The list of ignored overlays will be
 			// passed to Make to be turned into an RRO package.
 			if rroEnabled && !ctx.Config().EnforceRROExcludedOverlay(overlayModuleDir.String()) {
-				rroDirs = append(rroDirs, overlayModuleDir)
+				rroDirs = append(rroDirs, rroDir{overlayModuleDir, data.overlayType})
 			} else {
 				res = append(res, globbedResourceDir{
 					dir:   overlayModuleDir,
@@ -91,29 +104,34 @@
 
 func (overlaySingleton) GenerateBuildActions(ctx android.SingletonContext) {
 	var overlayData []overlayGlobResult
-	overlayDirs := ctx.Config().ResourceOverlays()
-	for i := range overlayDirs {
-		// Iterate backwards through the list of overlay directories so that the later, lower-priority
-		// directories in the list show up earlier in the command line to aapt2.
-		overlay := overlayDirs[len(overlayDirs)-1-i]
-		var result overlayGlobResult
-		result.dir = overlay
 
-		files, err := ctx.GlobWithDeps(filepath.Join(overlay, "**/*"), androidResourceIgnoreFilenames)
-		if err != nil {
-			ctx.Errorf("failed to glob resource dir %q: %s", overlay, err.Error())
-			continue
-		}
-		var paths android.Paths
-		for _, f := range files {
-			if !strings.HasSuffix(f, "/") {
-				paths = append(paths, android.PathForSource(ctx, f))
+	appendOverlayData := func(overlayDirs []string, t overlayType) {
+		for i := range overlayDirs {
+			// Iterate backwards through the list of overlay directories so that the later, lower-priority
+			// directories in the list show up earlier in the command line to aapt2.
+			overlay := overlayDirs[len(overlayDirs)-1-i]
+			var result overlayGlobResult
+			result.dir = overlay
+			result.overlayType = t
+
+			files, err := ctx.GlobWithDeps(filepath.Join(overlay, "**/*"), androidResourceIgnoreFilenames)
+			if err != nil {
+				ctx.Errorf("failed to glob resource dir %q: %s", overlay, err.Error())
+				continue
 			}
+			var paths android.Paths
+			for _, f := range files {
+				if !strings.HasSuffix(f, "/") {
+					paths = append(paths, android.PathForSource(ctx, f))
+				}
+			}
+			result.paths = android.PathsToDirectorySortedPaths(paths)
+			overlayData = append(overlayData, result)
 		}
-		result.paths = android.PathsToDirectorySortedPaths(paths)
-		overlayData = append(overlayData, result)
 	}
 
+	appendOverlayData(ctx.Config().DeviceResourceOverlays(), device)
+	appendOverlayData(ctx.Config().ProductResourceOverlays(), product)
 	ctx.Config().Once(overlayDataKey, func() interface{} {
 		return overlayData
 	})
diff --git a/java/androidmk.go b/java/androidmk.go
index 0c3b1c7..5b4f738 100644
--- a/java/androidmk.go
+++ b/java/androidmk.go
@@ -257,10 +257,24 @@
 					fmt.Fprintln(w, "LOCAL_NO_STANDARD_LIBRARIES := true")
 				}
 
-				if len(app.rroDirs) > 0 {
+				filterRRO := func(filter overlayType) android.Paths {
+					var paths android.Paths
+					for _, d := range app.rroDirs {
+						if d.overlayType == filter {
+							paths = append(paths, d.path)
+						}
+					}
 					// Reverse the order, Soong stores rroDirs in aapt2 order (low to high priority), but Make
 					// expects it in LOCAL_RESOURCE_DIRS order (high to low priority).
-					fmt.Fprintln(w, "LOCAL_SOONG_RRO_DIRS :=", strings.Join(android.ReversePaths(app.rroDirs).Strings(), " "))
+					return android.ReversePaths(paths)
+				}
+				deviceRRODirs := filterRRO(device)
+				if len(deviceRRODirs) > 0 {
+					fmt.Fprintln(w, "LOCAL_SOONG_DEVICE_RRO_DIRS :=", strings.Join(deviceRRODirs.Strings(), " "))
+				}
+				productRRODirs := filterRRO(product)
+				if len(productRRODirs) > 0 {
+					fmt.Fprintln(w, "LOCAL_SOONG_PRODUCT_RRO_DIRS :=", strings.Join(productRRODirs.Strings(), " "))
 				}
 
 				if Bool(app.appProperties.Export_package_resources) {
diff --git a/java/app.go b/java/app.go
index 8f5d010..b31f232 100644
--- a/java/app.go
+++ b/java/app.go
@@ -33,16 +33,13 @@
 	android.RegisterModuleType("android_test", AndroidTestFactory)
 	android.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory)
 	android.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory)
+	android.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory)
 }
 
 // AndroidManifest.xml merging
 // package splits
 
 type appProperties struct {
-	// The name of a certificate in the default certificate directory, blank to use the default product certificate,
-	// or an android_app_certificate module name in the form ":module".
-	Certificate *string
-
 	// Names of extra android_app_certificate modules to sign the apk with in the form ":module".
 	Additional_certificates []string
 
@@ -79,14 +76,24 @@
 	Use_embedded_dex *bool
 }
 
+// android_app properties that can be overridden by override_android_app
+type overridableAppProperties struct {
+	// The name of a certificate in the default certificate directory, blank to use the default product certificate,
+	// or an android_app_certificate module name in the form ":module".
+	Certificate *string
+}
+
 type AndroidApp struct {
 	Library
 	aapt
+	android.OverridableModuleBase
 
 	certificate Certificate
 
 	appProperties appProperties
 
+	overridableAppProperties overridableAppProperties
+
 	installJniLibs []jniLib
 
 	bundleFile android.Path
@@ -320,7 +327,7 @@
 
 func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
 	// Check if the install APK name needs to be overridden.
-	a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(ctx.ModuleName())
+	a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name())
 
 	// Process all building blocks, from AAPT to certificates.
 	a.aaptBuildActions(ctx)
@@ -339,6 +346,7 @@
 	certificates := a.certificateBuildActions(certificateDeps, ctx)
 
 	// Build a final signed app package.
+	// TODO(jungjw): Consider changing this to installApkName.
 	packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".apk")
 	CreateAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates)
 	a.outputFile = packageFile
@@ -413,7 +421,7 @@
 	if overridden {
 		return ":" + certificate
 	}
-	return String(a.appProperties.Certificate)
+	return String(a.overridableAppProperties.Certificate)
 }
 
 // android_app compiles sources and Android resources into an Android application package `.apk` file.
@@ -432,7 +440,8 @@
 		&module.Module.dexpreoptProperties,
 		&module.Module.protoProperties,
 		&module.aaptProperties,
-		&module.appProperties)
+		&module.appProperties,
+		&module.overridableAppProperties)
 
 	module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
 		return class == android.Device && ctx.Config().DevicePrefer32BitApps()
@@ -440,6 +449,7 @@
 
 	android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
 	android.InitDefaultableModule(module)
+	android.InitOverridableModule(module, &module.appProperties.Overrides)
 
 	return module
 }
@@ -503,6 +513,7 @@
 		&module.aaptProperties,
 		&module.appProperties,
 		&module.appTestProperties,
+		&module.overridableAppProperties,
 		&module.testProperties)
 
 	android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
@@ -541,7 +552,8 @@
 		&module.Module.protoProperties,
 		&module.aaptProperties,
 		&module.appProperties,
-		&module.appTestHelperAppProperties)
+		&module.appTestHelperAppProperties,
+		&module.overridableAppProperties)
 
 	android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
 	android.InitDefaultableModule(module)
@@ -575,3 +587,24 @@
 		android.PathForModuleSrc(ctx, cert+".pk8"),
 	}
 }
+
+type OverrideAndroidApp struct {
+	android.ModuleBase
+	android.OverrideModuleBase
+}
+
+func (i *OverrideAndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+	// All the overrides happen in the base module.
+	// TODO(jungjw): Check the base module type.
+}
+
+// override_android_app is used to create an android_app module based on another android_app by overriding
+// some of its properties.
+func OverrideAndroidAppModuleFactory() android.Module {
+	m := &OverrideAndroidApp{}
+	m.AddProperties(&overridableAppProperties{})
+
+	android.InitAndroidModule(m)
+	android.InitOverrideModule(m)
+	return m
+}
diff --git a/java/app_test.go b/java/app_test.go
index 3942ecd..cf57c80 100644
--- a/java/app_test.go
+++ b/java/app_test.go
@@ -211,9 +211,11 @@
 				"foo": {
 					buildDir + "/.intermediates/lib2/android_common/package-res.apk",
 					buildDir + "/.intermediates/lib/android_common/package-res.apk",
+					buildDir + "/.intermediates/lib3/android_common/package-res.apk",
 					"foo/res/res/values/strings.xml",
 					"device/vendor/blah/static_overlay/foo/res/values/strings.xml",
 					"device/vendor/blah/overlay/foo/res/values/strings.xml",
+					"product/vendor/blah/overlay/foo/res/values/strings.xml",
 				},
 				"bar": {
 					"device/vendor/blah/static_overlay/bar/res/values/strings.xml",
@@ -244,6 +246,7 @@
 				"foo": {
 					buildDir + "/.intermediates/lib2/android_common/package-res.apk",
 					buildDir + "/.intermediates/lib/android_common/package-res.apk",
+					buildDir + "/.intermediates/lib3/android_common/package-res.apk",
 					"foo/res/res/values/strings.xml",
 					"device/vendor/blah/static_overlay/foo/res/values/strings.xml",
 				},
@@ -260,9 +263,10 @@
 
 			rroDirs: map[string][]string{
 				"foo": {
-					"device/vendor/blah/overlay/foo/res",
+					"device:device/vendor/blah/overlay/foo/res",
 					// Enforce RRO on "foo" could imply RRO on static dependencies, but for now it doesn't.
 					// "device/vendor/blah/overlay/lib/res",
+					"product:product/vendor/blah/overlay/foo/res",
 				},
 				"bar": nil,
 				"lib": nil,
@@ -286,6 +290,7 @@
 				"foo": {
 					buildDir + "/.intermediates/lib2/android_common/package-res.apk",
 					buildDir + "/.intermediates/lib/android_common/package-res.apk",
+					buildDir + "/.intermediates/lib3/android_common/package-res.apk",
 					"foo/res/res/values/strings.xml",
 					"device/vendor/blah/static_overlay/foo/res/values/strings.xml",
 				},
@@ -297,21 +302,27 @@
 			},
 			rroDirs: map[string][]string{
 				"foo": {
-					"device/vendor/blah/overlay/foo/res",
-					"device/vendor/blah/overlay/lib/res",
+					"device:device/vendor/blah/overlay/foo/res",
+					"product:product/vendor/blah/overlay/foo/res",
+					// Lib dep comes after the direct deps
+					"device:device/vendor/blah/overlay/lib/res",
 				},
-				"bar": {"device/vendor/blah/overlay/bar/res"},
-				"lib": {"device/vendor/blah/overlay/lib/res"},
+				"bar": {"device:device/vendor/blah/overlay/bar/res"},
+				"lib": {"device:device/vendor/blah/overlay/lib/res"},
 			},
 		},
 	}
 
-	resourceOverlays := []string{
+	deviceResourceOverlays := []string{
 		"device/vendor/blah/overlay",
 		"device/vendor/blah/overlay2",
 		"device/vendor/blah/static_overlay",
 	}
 
+	productResourceOverlays := []string{
+		"product/vendor/blah/overlay",
+	}
+
 	fs := map[string][]byte{
 		"foo/res/res/values/strings.xml":                               nil,
 		"bar/res/res/values/strings.xml":                               nil,
@@ -323,13 +334,14 @@
 		"device/vendor/blah/static_overlay/foo/res/values/strings.xml": nil,
 		"device/vendor/blah/static_overlay/bar/res/values/strings.xml": nil,
 		"device/vendor/blah/overlay2/res/values/strings.xml":           nil,
+		"product/vendor/blah/overlay/foo/res/values/strings.xml":       nil,
 	}
 
 	bp := `
 			android_app {
 				name: "foo",
 				resource_dirs: ["foo/res"],
-				static_libs: ["lib"],
+				static_libs: ["lib", "lib3"],
 			}
 
 			android_app {
@@ -347,12 +359,19 @@
 				name: "lib2",
 				resource_dirs: ["lib2/res"],
 			}
+
+			// This library has the same resources as lib (should not lead to dupe RROs)
+			android_library {
+				name: "lib3",
+				resource_dirs: ["lib/res"]
+			}
 		`
 
 	for _, testCase := range testCases {
 		t.Run(testCase.name, func(t *testing.T) {
 			config := testConfig(nil)
-			config.TestProductVariables.ResourceOverlays = resourceOverlays
+			config.TestProductVariables.DeviceResourceOverlays = deviceResourceOverlays
+			config.TestProductVariables.ProductResourceOverlays = productResourceOverlays
 			if testCase.enforceRROTargets != nil {
 				config.TestProductVariables.EnforceRROTargets = testCase.enforceRROTargets
 			}
@@ -389,7 +408,17 @@
 					overlayFiles = resourceListToFiles(module, overlayList.Inputs.Strings())
 				}
 
-				rroDirs = module.Module().(AndroidLibraryDependency).ExportedRRODirs().Strings()
+				for _, d := range module.Module().(AndroidLibraryDependency).ExportedRRODirs() {
+					var prefix string
+					if d.overlayType == device {
+						prefix = "device:"
+					} else if d.overlayType == product {
+						prefix = "product:"
+					} else {
+						t.Fatalf("Unexpected overlayType %d", d.overlayType)
+					}
+					rroDirs = append(rroDirs, prefix+d.path.String())
+				}
 
 				return resourceFiles, overlayFiles, rroDirs
 			}
@@ -798,3 +827,75 @@
 		t.Errorf("target package renaming flag, %q is missing in aapt2 link flags, %q", e, aapt2Flags)
 	}
 }
+
+func TestOverrideAndroidApp(t *testing.T) {
+	ctx := testJava(t, `
+		android_app {
+			name: "foo",
+			srcs: ["a.java"],
+			overrides: ["baz"],
+		}
+
+		override_android_app {
+			name: "bar",
+			base: "foo",
+			certificate: ":new_certificate",
+		}
+
+		android_app_certificate {
+			name: "new_certificate",
+			certificate: "cert/new_cert",
+		}
+		`)
+
+	expectedVariants := []struct {
+		variantName string
+		apkName     string
+		apkPath     string
+		signFlag    string
+		overrides   []string
+	}{
+		{
+			variantName: "android_common",
+			apkPath:     "/target/product/test_device/system/app/foo/foo.apk",
+			signFlag:    "build/target/product/security/testkey.x509.pem build/target/product/security/testkey.pk8",
+			overrides:   []string{"baz"},
+		},
+		{
+			variantName: "bar_android_common",
+			apkPath:     "/target/product/test_device/system/app/bar/bar.apk",
+			signFlag:    "cert/new_cert.x509.pem cert/new_cert.pk8",
+			overrides:   []string{"baz", "foo"},
+		},
+	}
+	for _, expected := range expectedVariants {
+		variant := ctx.ModuleForTests("foo", expected.variantName)
+
+		// Check the final apk name
+		outputs := variant.AllOutputs()
+		expectedApkPath := buildDir + expected.apkPath
+		found := false
+		for _, o := range outputs {
+			if o == expectedApkPath {
+				found = true
+				break
+			}
+		}
+		if !found {
+			t.Errorf("Can't find %q in output files.\nAll outputs:%v", expectedApkPath, outputs)
+		}
+
+		// Check the certificate paths
+		signapk := variant.Output("foo.apk")
+		signFlag := signapk.Args["certificates"]
+		if expected.signFlag != signFlag {
+			t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected.signFlag, signFlag)
+		}
+
+		mod := variant.Module().(*AndroidApp)
+		if !reflect.DeepEqual(expected.overrides, mod.appProperties.Overrides) {
+			t.Errorf("Incorrect overrides property value, expected: %q, got: %q",
+				expected.overrides, mod.appProperties.Overrides)
+		}
+	}
+}
diff --git a/java/java.go b/java/java.go
index e0da006..1fd0a9e 100644
--- a/java/java.go
+++ b/java/java.go
@@ -1726,7 +1726,7 @@
 	Exclude_dirs []string
 
 	// if set to true, run Jetifier against .jar file. Defaults to false.
-	Jetifier_enabled *bool
+	Jetifier *bool
 }
 
 type Import struct {
@@ -1771,7 +1771,7 @@
 	outputFile := android.PathForModuleOut(ctx, "combined", jarName)
 	TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
 		false, j.properties.Exclude_files, j.properties.Exclude_dirs)
-	if Bool(j.properties.Jetifier_enabled) {
+	if Bool(j.properties.Jetifier) {
 		inputFile := outputFile
 		outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
 		TransformJetifier(ctx, outputFile, inputFile)
@@ -2068,6 +2068,7 @@
 		&androidLibraryProperties{},
 		&appProperties{},
 		&appTestProperties{},
+		&overridableAppProperties{},
 		&ImportProperties{},
 		&AARImportProperties{},
 		&sdkLibraryProperties{},
diff --git a/java/java_test.go b/java/java_test.go
index a43f1f7..f466499 100644
--- a/java/java_test.go
+++ b/java/java_test.go
@@ -86,10 +86,12 @@
 	ctx.RegisterModuleType("droiddoc_host", android.ModuleFactoryAdaptor(DroiddocHostFactory))
 	ctx.RegisterModuleType("droiddoc_template", android.ModuleFactoryAdaptor(ExportedDroiddocDirFactory))
 	ctx.RegisterModuleType("java_sdk_library", android.ModuleFactoryAdaptor(SdkLibraryFactory))
+	ctx.RegisterModuleType("override_android_app", android.ModuleFactoryAdaptor(OverrideAndroidAppModuleFactory))
 	ctx.RegisterModuleType("prebuilt_apis", android.ModuleFactoryAdaptor(PrebuiltApisFactory))
 	ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
 	ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
 	ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
+	ctx.PreArchMutators(android.RegisterOverridePreArchMutators)
 	ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
 		ctx.TopDown("prebuilt_apis", PrebuiltApisMutator).Parallel()
 		ctx.TopDown("java_sdk_library", SdkLibraryMutator).Parallel()
diff --git a/java/jdeps.go b/java/jdeps.go
index 2eaeab8..18498be 100644
--- a/java/jdeps.go
+++ b/java/jdeps.go
@@ -51,6 +51,10 @@
 	moduleInfos := make(map[string]android.IdeInfo)
 
 	ctx.VisitAllModules(func(module android.Module) {
+		if !module.Enabled() {
+			return
+		}
+
 		ideInfoProvider, ok := module.(android.IDEInfo)
 		if !ok {
 			return
diff --git a/java/prebuilt_apis.go b/java/prebuilt_apis.go
index 02b9b45..c370811 100644
--- a/java/prebuilt_apis.go
+++ b/java/prebuilt_apis.go
@@ -22,13 +22,6 @@
 	"github.com/google/blueprint/proptools"
 )
 
-// prebuilt_apis is a meta-module that generates filegroup modules for all
-// API txt files found under the directory where the Android.bp is located.
-// Specificaly, an API file located at ./<ver>/<scope>/api/<module>.txt
-// generates a filegroup module named <module>-api.<scope>.<ver>.
-//
-// It also creates <module>-api.<scope>.latest for the lastest <ver>.
-//
 func init() {
 	android.RegisterModuleType("prebuilt_apis", PrebuiltApisFactory)
 
@@ -188,6 +181,12 @@
 	}
 }
 
+// prebuilt_apis is a meta-module that generates filegroup modules for all
+// API txt files found under the directory where the Android.bp is located.
+// Specifically, an API file located at ./<ver>/<scope>/api/<module>.txt
+// generates a filegroup module named <module>-api.<scope>.<ver>.
+//
+// It also creates <module>-api.<scope>.latest for the latest <ver>.
 func PrebuiltApisFactory() android.Module {
 	module := &prebuiltApis{}
 	module.AddProperties(&module.properties)
diff --git a/java/sdk_library.go b/java/sdk_library.go
index 18866d5..72cce57 100644
--- a/java/sdk_library.go
+++ b/java/sdk_library.go
@@ -668,11 +668,11 @@
 }
 
 func (module *SdkLibrary) createInternalModules(mctx android.TopDownMutatorContext) {
-	if module.Library.Module.properties.Srcs == nil {
+	if len(module.Library.Module.properties.Srcs) == 0 {
 		mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
 	}
 
-	if module.sdkLibraryProperties.Api_packages == nil {
+	if len(module.sdkLibraryProperties.Api_packages) == 0 {
 		mctx.PropertyErrorf("api_packages", "java_sdk_library must specify api_packages")
 	}
 
diff --git a/scripts/mergenotice.py b/scripts/mergenotice.py
new file mode 100755
index 0000000..407ae8c
--- /dev/null
+++ b/scripts/mergenotice.py
@@ -0,0 +1,49 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2019 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+"""
+Merges input notice files to the output file while ignoring duplicated files
+This script shouldn't be confused with build/make/tools/generate-notice-files.py
+which is responsible for creating the final notice file for all artifacts
+installed. This script has rather limited scope; it is meant to create a merged
+notice file for a set of modules that are packaged together, e.g. in an APEX.
+The merged notice file does not reveal the individual files in the package.
+"""
+
+import sys
+import argparse
+
+def get_args():
+  parser = argparse.ArgumentParser(description='Merge notice files.')
+  parser.add_argument('--output', help='output file path.')
+  parser.add_argument('inputs', metavar='INPUT', nargs='+',
+                      help='input notice file')
+  return parser.parse_args()
+
+def main(argv):
+  args = get_args()
+
+  processed = set()
+  with open(args.output, 'w+') as output:
+    for input in args.inputs:
+      with open(input, 'r') as f:
+        data = f.read().strip()
+        if data not in processed:
+          processed.add(data)
+          output.write('%s\n\n' % data)
+
+if __name__ == '__main__':
+  main(sys.argv)
diff --git a/scripts/strip.sh b/scripts/strip.sh
index bedc527..d536907 100755
--- a/scripts/strip.sh
+++ b/scripts/strip.sh
@@ -28,6 +28,7 @@
 #   --keep-mini-debug-info
 #   --keep-symbols
 #   --use-gnu-strip
+#   --remove-build-id
 
 set -o pipefail
 
@@ -41,6 +42,7 @@
         --keep-mini-debug-info  Keep compressed debug info in out-file
         --keep-symbols          Keep symbols in out-file
         --use-gnu-strip         Use strip/objcopy instead of llvm-{strip,objcopy}
+        --remove-build-id       Remove the gnu build-id section in out-file
 EOF
     exit 1
 }
@@ -110,6 +112,16 @@
     fi
 }
 
+do_remove_build_id() {
+    if [ -z "${use_gnu_strip}" ]; then
+        "${CLANG_BIN}/llvm-strip" -remove-section=.note.gnu.build-id "${outfile}.tmp" -o "${outfile}.tmp.no-build-id"
+    else
+        "${CROSS_COMPILE}strip" --remove-section=.note.gnu.build-id "${outfile}.tmp" -o "${outfile}.tmp.no-build-id"
+    fi
+    rm -f "${outfile}.tmp"
+    mv "${outfile}.tmp.no-build-id" "${outfile}.tmp"
+}
+
 while getopts $OPTSTRING opt; do
     case "$opt" in
 	d) depsfile="${OPTARG}" ;;
@@ -120,7 +132,7 @@
 		add-gnu-debuglink) add_gnu_debuglink=true ;;
 		keep-mini-debug-info) keep_mini_debug_info=true ;;
 		keep-symbols) keep_symbols=true ;;
-		use-gnu-strip) use_gnu_strip=true ;;
+		remove-build-id) remove_build_id=true ;;
 		*) echo "Unknown option --${OPTARG}"; usage ;;
 	    esac;;
 	?) usage ;;
@@ -167,6 +179,10 @@
     do_add_gnu_debuglink
 fi
 
+if [ ! -z "${remove_build_id}" ]; then
+    do_remove_build_id
+fi
+
 rm -f "${outfile}"
 mv "${outfile}.tmp" "${outfile}"
 
diff --git a/sysprop/sysprop_library.go b/sysprop/sysprop_library.go
index 6e8e306..48078d8 100644
--- a/sysprop/sysprop_library.go
+++ b/sysprop/sysprop_library.go
@@ -82,7 +82,11 @@
 }
 
 func syspropLibraryHook(ctx android.LoadHookContext, m *syspropLibrary) {
-	if m.syspropLibraryProperties.Api_packages == nil {
+	if len(m.commonProperties.Srcs) == 0 {
+		ctx.PropertyErrorf("srcs", "sysprop_library must specify srcs")
+	}
+
+	if len(m.syspropLibraryProperties.Api_packages) == 0 {
 		ctx.PropertyErrorf("api_packages", "sysprop_library must specify api_packages")
 	}
 
diff --git a/ui/build/ninja.go b/ui/build/ninja.go
index cb41579..7994f3a 100644
--- a/ui/build/ninja.go
+++ b/ui/build/ninja.go
@@ -31,7 +31,8 @@
 	defer ctx.EndTrace()
 
 	fifo := filepath.Join(config.OutDir(), ".ninja_fifo")
-	status.NinjaReader(ctx, ctx.Status.StartTool(), fifo)
+	nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo)
+	defer nr.Close()
 
 	executable := config.PrebuiltBuildTool("ninja")
 	args := []string{
diff --git a/ui/build/soong.go b/ui/build/soong.go
index c89f0d5..2ce1ac9 100644
--- a/ui/build/soong.go
+++ b/ui/build/soong.go
@@ -109,7 +109,8 @@
 		defer ctx.EndTrace()
 
 		fifo := filepath.Join(config.OutDir(), ".ninja_fifo")
-		status.NinjaReader(ctx, ctx.Status.StartTool(), fifo)
+		nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo)
+		defer nr.Close()
 
 		cmd := Command(ctx, config, "soong "+name,
 			config.PrebuiltBuildTool("ninja"),
diff --git a/ui/logger/logger.go b/ui/logger/logger.go
index 58890e9..9b26ae8 100644
--- a/ui/logger/logger.go
+++ b/ui/logger/logger.go
@@ -180,12 +180,16 @@
 	return s
 }
 
+type panicWriter struct{}
+
+func (panicWriter) Write([]byte) (int, error) { panic("write to panicWriter") }
+
 // Close disables logging to the file and closes the file handle.
 func (s *stdLogger) Close() {
 	s.mutex.Lock()
 	defer s.mutex.Unlock()
 	if s.file != nil {
-		s.fileLogger.SetOutput(ioutil.Discard)
+		s.fileLogger.SetOutput(panicWriter{})
 		s.file.Close()
 		s.file = nil
 	}
diff --git a/ui/status/Android.bp b/ui/status/Android.bp
index 76caaef..901a713 100644
--- a/ui/status/Android.bp
+++ b/ui/status/Android.bp
@@ -28,6 +28,7 @@
     ],
     testSrcs: [
         "kati_test.go",
+        "ninja_test.go",
         "status_test.go",
     ],
 }
diff --git a/ui/status/ninja.go b/ui/status/ninja.go
index 4ceb5ef..ee2a2da 100644
--- a/ui/status/ninja.go
+++ b/ui/status/ninja.go
@@ -20,6 +20,7 @@
 	"io"
 	"os"
 	"syscall"
+	"time"
 
 	"github.com/golang/protobuf/proto"
 
@@ -27,9 +28,9 @@
 	"android/soong/ui/status/ninja_frontend"
 )
 
-// NinjaReader reads the protobuf frontend format from ninja and translates it
+// NewNinjaReader reads the protobuf frontend format from ninja and translates it
 // into calls on the ToolStatus API.
-func NinjaReader(ctx logger.Logger, status ToolStatus, fifo string) {
+func NewNinjaReader(ctx logger.Logger, status ToolStatus, fifo string) *NinjaReader {
 	os.Remove(fifo)
 
 	err := syscall.Mkfifo(fifo, 0666)
@@ -37,14 +38,69 @@
 		ctx.Fatalf("Failed to mkfifo(%q): %v", fifo, err)
 	}
 
-	go ninjaReader(status, fifo)
+	n := &NinjaReader{
+		status: status,
+		fifo:   fifo,
+		done:   make(chan bool),
+		cancel: make(chan bool),
+	}
+
+	go n.run()
+
+	return n
 }
 
-func ninjaReader(status ToolStatus, fifo string) {
-	f, err := os.Open(fifo)
-	if err != nil {
-		status.Error(fmt.Sprintf("Failed to open fifo: %v", err))
+type NinjaReader struct {
+	status ToolStatus
+	fifo   string
+	done   chan bool
+	cancel chan bool
+}
+
+const NINJA_READER_CLOSE_TIMEOUT = 5 * time.Second
+
+// Close waits for NinjaReader to finish reading from the fifo, or 5 seconds.
+func (n *NinjaReader) Close() {
+	// Signal the goroutine to stop if it is blocking opening the fifo.
+	close(n.cancel)
+
+	timeoutCh := time.After(NINJA_READER_CLOSE_TIMEOUT)
+
+	select {
+	case <-n.done:
+		// Nothing
+	case <-timeoutCh:
+		n.status.Error(fmt.Sprintf("ninja fifo didn't finish after %s", NINJA_READER_CLOSE_TIMEOUT.String()))
 	}
+
+	return
+}
+
+func (n *NinjaReader) run() {
+	defer close(n.done)
+
+	// Opening the fifo can block forever if ninja never opens the write end, do it in a goroutine so this
+	// method can exit on cancel.
+	fileCh := make(chan *os.File)
+	go func() {
+		f, err := os.Open(n.fifo)
+		if err != nil {
+			n.status.Error(fmt.Sprintf("Failed to open fifo: %v", err))
+			close(fileCh)
+			return
+		}
+		fileCh <- f
+	}()
+
+	var f *os.File
+
+	select {
+	case f = <-fileCh:
+		// Nothing
+	case <-n.cancel:
+		return
+	}
+
 	defer f.Close()
 
 	r := bufio.NewReader(f)
@@ -55,7 +111,7 @@
 		size, err := readVarInt(r)
 		if err != nil {
 			if err != io.EOF {
-				status.Error(fmt.Sprintf("Got error reading from ninja: %s", err))
+				n.status.Error(fmt.Sprintf("Got error reading from ninja: %s", err))
 			}
 			return
 		}
@@ -64,9 +120,9 @@
 		_, err = io.ReadFull(r, buf)
 		if err != nil {
 			if err == io.EOF {
-				status.Print(fmt.Sprintf("Missing message of size %d from ninja\n", size))
+				n.status.Print(fmt.Sprintf("Missing message of size %d from ninja\n", size))
 			} else {
-				status.Error(fmt.Sprintf("Got error reading from ninja: %s", err))
+				n.status.Error(fmt.Sprintf("Got error reading from ninja: %s", err))
 			}
 			return
 		}
@@ -74,13 +130,13 @@
 		msg := &ninja_frontend.Status{}
 		err = proto.Unmarshal(buf, msg)
 		if err != nil {
-			status.Print(fmt.Sprintf("Error reading message from ninja: %v", err))
+			n.status.Print(fmt.Sprintf("Error reading message from ninja: %v", err))
 			continue
 		}
 
 		// Ignore msg.BuildStarted
 		if msg.TotalEdges != nil {
-			status.SetTotalActions(int(msg.TotalEdges.GetTotalEdges()))
+			n.status.SetTotalActions(int(msg.TotalEdges.GetTotalEdges()))
 		}
 		if msg.EdgeStarted != nil {
 			action := &Action{
@@ -88,7 +144,7 @@
 				Outputs:     msg.EdgeStarted.Outputs,
 				Command:     msg.EdgeStarted.GetCommand(),
 			}
-			status.StartAction(action)
+			n.status.StartAction(action)
 			running[msg.EdgeStarted.GetId()] = action
 		}
 		if msg.EdgeFinished != nil {
@@ -101,7 +157,7 @@
 					err = fmt.Errorf("exited with code: %d", exitCode)
 				}
 
-				status.FinishAction(ActionResult{
+				n.status.FinishAction(ActionResult{
 					Action: started,
 					Output: msg.EdgeFinished.GetOutput(),
 					Error:  err,
@@ -112,17 +168,17 @@
 			message := "ninja: " + msg.Message.GetMessage()
 			switch msg.Message.GetLevel() {
 			case ninja_frontend.Status_Message_INFO:
-				status.Status(message)
+				n.status.Status(message)
 			case ninja_frontend.Status_Message_WARNING:
-				status.Print("warning: " + message)
+				n.status.Print("warning: " + message)
 			case ninja_frontend.Status_Message_ERROR:
-				status.Error(message)
+				n.status.Error(message)
 			default:
-				status.Print(message)
+				n.status.Print(message)
 			}
 		}
 		if msg.BuildFinished != nil {
-			status.Finish()
+			n.status.Finish()
 		}
 	}
 }
diff --git a/ui/status/ninja_test.go b/ui/status/ninja_test.go
new file mode 100644
index 0000000..c400c97
--- /dev/null
+++ b/ui/status/ninja_test.go
@@ -0,0 +1,45 @@
+// Copyright 2019 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package status
+
+import (
+	"io/ioutil"
+	"os"
+	"path/filepath"
+	"testing"
+	"time"
+
+	"android/soong/ui/logger"
+)
+
+// Tests that closing the ninja reader when nothing has opened the other end of the fifo is fast.
+func TestNinjaReader_Close(t *testing.T) {
+	tempDir, err := ioutil.TempDir("", "ninja_test")
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer os.RemoveAll(tempDir)
+
+	stat := &Status{}
+	nr := NewNinjaReader(logger.New(ioutil.Discard), stat.StartTool(), filepath.Join(tempDir, "fifo"))
+
+	start := time.Now()
+
+	nr.Close()
+
+	if g, w := time.Since(start), NINJA_READER_CLOSE_TIMEOUT; g >= w {
+		t.Errorf("nr.Close timed out, %s > %s", g, w)
+	}
+}