Merge "Rename to LOCAL_PREFER_CODE_INTEGRITY"
diff --git a/core/Makefile b/core/Makefile
index 179cf29..c5dbe24 100644
--- a/core/Makefile
+++ b/core/Makefile
@@ -3282,7 +3282,8 @@
   $(HOST_LIBRARY_PATH)/libpcre2$(HOST_SHLIB_SUFFIX) \
   $(HOST_LIBRARY_PATH)/libbrotli$(HOST_SHLIB_SUFFIX) \
   $(HOST_LIBRARY_PATH)/liblp$(HOST_SHLIB_SUFFIX) \
-  $(HOST_LIBRARY_PATH)/libext4_utils$(HOST_SHLIB_SUFFIX)
+  $(HOST_LIBRARY_PATH)/libext4_utils$(HOST_SHLIB_SUFFIX) \
+  $(HOST_LIBRARY_PATH)/libfec$(HOST_SHLIB_SUFFIX)
 
 
 .PHONY: otatools
diff --git a/core/product_config.mk b/core/product_config.mk
index ac7fe91..f0f7da1 100644
--- a/core/product_config.mk
+++ b/core/product_config.mk
@@ -175,15 +175,9 @@
 include $(BUILD_SYSTEM)/product.mk
 include $(BUILD_SYSTEM)/device.mk
 
-ifneq ($(strip $(TARGET_BUILD_APPS)),)
-# An unbundled app build needs only the core product makefiles.
-all_product_configs := $(call get-product-makefiles,\
-    $(SRC_TARGET_DIR)/product/AndroidProducts.mk)
-else
 # Read in all of the product definitions specified by the AndroidProducts.mk
 # files in the tree.
 all_product_configs := $(get-all-product-makefiles)
-endif
 
 all_named_products :=
 
diff --git a/envsetup.sh b/envsetup.sh
index 44ae659..62a64b6 100644
--- a/envsetup.sh
+++ b/envsetup.sh
@@ -772,6 +772,9 @@
 {
     local TOPFILE=build/make/core/envsetup.mk
     local HERE=$PWD
+    if [ "$1" ]; then
+        \cd $1
+    fi;
     local T=
     while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
         T=`PWD= /bin/pwd`
@@ -852,24 +855,29 @@
             # Remove the leading ./ and trailing / if any exists.
             DIR=${DIR#./}
             DIR=${DIR%/}
-            if [ -f $DIR/Android.mk -o -f $DIR/Android.bp ]; then
-                local TO_CHOP=`(\cd -P -- $T && pwd -P) | wc -c | tr -d ' '`
-                local TO_CHOP=`expr $TO_CHOP + 1`
-                local START=`PWD= /bin/pwd`
-                local MDIR=`echo $START | cut -c${TO_CHOP}-`
-                if [ "$MDIR" = "" ] ; then
-                    MDIR=$DIR
-                else
-                    MDIR=$MDIR/$DIR
+            local M
+            if [ "$DIR_MODULES" = "" ]; then
+                M=$(findmakefile $DIR)
+            else
+                # Only check the target directory if a module is specified.
+                if [ -f $DIR/Android.mk -o -f $DIR/Android.bp ]; then
+                    local HERE=$PWD
+                    cd $DIR
+                    M=`PWD= /bin/pwd`
+                    M=$M/Android.mk
+                    cd $HERE
                 fi
-                MDIR=${MDIR%/.}
+            fi
+            if [ "$M" ]; then
+                # Remove the path to top as the makefilepath needs to be relative
+                local M=`echo $M|sed 's:'$T'/::'`
                 if [ "$DIR_MODULES" = "" ]; then
-                    MODULES_IN_PATHS="$MODULES_IN_PATHS MODULES-IN-$MDIR"
-                    GET_INSTALL_PATHS="$GET_INSTALL_PATHS GET-INSTALL-PATH-IN-$MDIR"
+                    MODULES_IN_PATHS="$MODULES_IN_PATHS MODULES-IN-$(dirname ${M})"
+                    GET_INSTALL_PATHS="$GET_INSTALL_PATHS GET-INSTALL-PATH-IN-$(dirname ${M})"
                 else
                     MODULES="$MODULES $DIR_MODULES"
                 fi
-                MAKEFILE="$MAKEFILE $MDIR/Android.mk"
+                MAKEFILE="$MAKEFILE $M"
             else
                 case $DIR in
                   showcommands | snod | dist | *=*) ARGS="$ARGS $DIR";;
diff --git a/target/product/base_system.mk b/target/product/base_system.mk
index 392a580..6e8e79b 100644
--- a/target/product/base_system.mk
+++ b/target/product/base_system.mk
@@ -206,6 +206,7 @@
     MediaProvider \
     mediaserver \
     mke2fs \
+    ModuleMetadata \
     monkey \
     mtpd \
     ndc \
@@ -315,10 +316,7 @@
 # Packages included only for eng or userdebug builds, previously debug tagged
 PRODUCT_PACKAGES_DEBUG := \
     adb_keys \
-    apex.test.key \
     arping \
-    com.android.apex.test_package.key \
-    com.android.apex.test_package.preinstall.key \
     iotop \
     iw \
     logpersist.start \
diff --git a/target/product/treble_common.mk b/target/product/treble_common.mk
index 0408028..7642876 100644
--- a/target/product/treble_common.mk
+++ b/target/product/treble_common.mk
@@ -38,7 +38,7 @@
 # Telephony:
 #   Provide a default APN configuration
 PRODUCT_COPY_FILES += \
-    device/generic/goldfish/data/etc/apns-conf.xml:system/etc/apns-conf.xml
+    device/sample/etc/apns-full-conf.xml:system/etc/apns-conf.xml
 
 # NFC:
 #   Provide default libnfc-nci.conf file for devices that does not have one in
diff --git a/tools/releasetools/build_super_image.py b/tools/releasetools/build_super_image.py
index e8730ae..bb0e641 100755
--- a/tools/releasetools/build_super_image.py
+++ b/tools/releasetools/build_super_image.py
@@ -67,6 +67,17 @@
     return os.path.getsize(img)
 
 
+def GetArgumentsForImage(partition, group, image=None):
+  image_size = GetPartitionSizeFromImage(image) if image else 0
+
+  cmd = ["--partition",
+         "{}:readonly:{}:{}".format(partition, image_size, group)]
+  if image:
+    cmd += ["--image", "{}={}".format(partition, image)]
+
+  return cmd
+
+
 def BuildSuperImageFromDict(info_dict, output):
 
   cmd = [info_dict["lpmake"],
@@ -105,26 +116,25 @@
 
     for partition in partition_list:
       image = info_dict.get("{}_image".format(partition))
-      image_size = 0
       if image:
-        image_size = GetPartitionSizeFromImage(image)
         has_image = True
-      if append_suffix:
-        cmd += ["--partition",
-                "{}_a:readonly:{}:{}_a".format(partition, image_size, group),
-                "--partition",
-                "{}_b:readonly:0:{}_b".format(partition, group)]
-        if image:
-          # For A/B devices, super partition always contains sub-partitions in
-          # the _a slot, because this image should only be used for
-          # bootstrapping / initializing the device. When flashing the image,
-          # bootloader fastboot should always mark _a slot as bootable.
-          cmd += ["--image", "{}_a={}".format(partition, image)]
-      else:
-        cmd += ["--partition",
-                "{}:readonly:{}:{}".format(partition, image_size, group)]
-        if image:
-          cmd += ["--image", "{}={}".format(partition, image)]
+
+      if not append_suffix:
+        cmd += GetArgumentsForImage(partition, group, image)
+        continue
+
+      # For A/B devices, super partition always contains sub-partitions in
+      # the _a slot, because this image should only be used for
+      # bootstrapping / initializing the device. When flashing the image,
+      # bootloader fastboot should always mark _a slot as bootable.
+      cmd += GetArgumentsForImage(partition + "_a", group + "_a", image)
+
+      other_image = None
+      if partition == "system" and "system_other_image" in info_dict:
+        other_image = info_dict["system_other_image"]
+        has_image = True
+
+      cmd += GetArgumentsForImage(partition + "_b", group + "_b", other_image)
 
   if has_image:
     cmd.append("--sparse")
@@ -145,6 +155,12 @@
   info_dict = common.LoadInfoDict(inp)
   partition_list = shlex.split(
       info_dict.get("dynamic_partition_list", "").strip())
+
+  if "system" in partition_list:
+    image_path = os.path.join(inp, "IMAGES", "system_other.img")
+    if os.path.isfile(image_path):
+      info_dict["system_other_image"] = image_path
+
   missing_images = []
   for partition in partition_list:
     image_path = os.path.join(inp, "IMAGES", "{}.img".format(partition))