Fix issue #2448075: aapt doesn't fix up activity-alias android:targetActivity links

And related:

- The aapt tool now sets a resource configurations sdk level to match any configs
  that have been set (for example if you specify density your sdk level will be
  at least 4).
- New option to modify the targetPackage attribute of instrumentation.
- Clean up of aapt options help.
- Fix of UI type values to leave 0 for "unspecified".
- Make the UI mode config APIs public.
diff --git a/api/current.xml b/api/current.xml
index 189835e..85367d0 100644
--- a/api/current.xml
+++ b/api/current.xml
@@ -42377,6 +42377,17 @@
  visibility="public"
 >
 </field>
+<field name="CONFIG_UI_MODE"
+ type="int"
+ transient="false"
+ volatile="false"
+ value="512"
+ static="true"
+ final="true"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</field>
 <field name="CREATOR"
  type="android.os.Parcelable.Creator"
  transient="false"
@@ -47830,6 +47841,94 @@
  visibility="public"
 >
 </field>
+<field name="UI_MODE_NIGHT_MASK"
+ type="int"
+ transient="false"
+ volatile="false"
+ value="48"
+ static="true"
+ final="true"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</field>
+<field name="UI_MODE_NIGHT_NO"
+ type="int"
+ transient="false"
+ volatile="false"
+ value="16"
+ static="true"
+ final="true"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</field>
+<field name="UI_MODE_NIGHT_UNDEFINED"
+ type="int"
+ transient="false"
+ volatile="false"
+ value="0"
+ static="true"
+ final="true"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</field>
+<field name="UI_MODE_NIGHT_YES"
+ type="int"
+ transient="false"
+ volatile="false"
+ value="32"
+ static="true"
+ final="true"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</field>
+<field name="UI_MODE_TYPE_CAR"
+ type="int"
+ transient="false"
+ volatile="false"
+ value="2"
+ static="true"
+ final="true"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</field>
+<field name="UI_MODE_TYPE_MASK"
+ type="int"
+ transient="false"
+ volatile="false"
+ value="15"
+ static="true"
+ final="true"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</field>
+<field name="UI_MODE_TYPE_NORMAL"
+ type="int"
+ transient="false"
+ volatile="false"
+ value="1"
+ static="true"
+ final="true"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</field>
+<field name="UI_MODE_TYPE_UNDEFINED"
+ type="int"
+ transient="false"
+ volatile="false"
+ value="0"
+ static="true"
+ final="true"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</field>
 <field name="fontScale"
  type="float"
  transient="false"
@@ -47950,6 +48049,16 @@
  visibility="public"
 >
 </field>
+<field name="uiMode"
+ type="int"
+ transient="false"
+ volatile="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</field>
 </class>
 <class name="Resources"
  extends="java.lang.Object"
diff --git a/core/java/android/content/pm/ActivityInfo.java b/core/java/android/content/pm/ActivityInfo.java
index a13f7f9..91b2be7 100644
--- a/core/java/android/content/pm/ActivityInfo.java
+++ b/core/java/android/content/pm/ActivityInfo.java
@@ -253,7 +253,6 @@
      * Bit in {@link #configChanges} that indicates that the activity
      * can itself handle the ui mode. Set from the
      * {@link android.R.attr#configChanges} attribute.
-     * @hide (UIMODE) Pending API council approval
      */
     public static final int CONFIG_UI_MODE = 0x0200;
     /**
diff --git a/core/java/android/content/res/Configuration.java b/core/java/android/content/res/Configuration.java
index 6490b65..a737283 100644
--- a/core/java/android/content/res/Configuration.java
+++ b/core/java/android/content/res/Configuration.java
@@ -162,20 +162,14 @@
      */
     public int orientation;
 
-    /** @hide (UIMODE) Pending API council approval */
     public static final int UI_MODE_TYPE_MASK = 0x0f;
-    /** @hide (UIMODE) Pending API council approval */
-    public static final int UI_MODE_TYPE_NORMAL = 0x00;
-    /** @hide (UIMODE) Pending API council approval */
-    public static final int UI_MODE_TYPE_CAR = 0x01;
+    public static final int UI_MODE_TYPE_UNDEFINED = 0x00;
+    public static final int UI_MODE_TYPE_NORMAL = 0x01;
+    public static final int UI_MODE_TYPE_CAR = 0x02;
 
-    /** @hide (UIMODE) Pending API council approval */
     public static final int UI_MODE_NIGHT_MASK = 0x30;
-    /** @hide (UIMODE) Pending API council approval */
     public static final int UI_MODE_NIGHT_UNDEFINED = 0x00;
-    /** @hide (UIMODE) Pending API council approval */
     public static final int UI_MODE_NIGHT_NO = 0x10;
-    /** @hide (UIMODE) Pending API council approval */
     public static final int UI_MODE_NIGHT_YES = 0x20;
 
     /**
@@ -187,8 +181,6 @@
      * <p>The {@link #UI_MODE_NIGHT_MASK} defines whether the screen
      * is in a special mode. They may be one of
      * {@link #UI_MODE_NIGHT_NO} or {@link #UI_MODE_NIGHT_YES}.
-     *
-     * @hide (UIMODE) Pending API council approval
      */
     public int uiMode;
 
diff --git a/include/utils/ResourceTypes.h b/include/utils/ResourceTypes.h
index cd657e8..cbcef4e 100644
--- a/include/utils/ResourceTypes.h
+++ b/include/utils/ResourceTypes.h
@@ -944,8 +944,9 @@
     enum {
         // uiMode bits for the mode type.
         MASK_UI_MODE_TYPE = 0x0f,
-        UI_MODE_TYPE_NORMAL = 0x00,
-        UI_MODE_TYPE_CAR = 0x01,
+        UI_MODE_TYPE_ANY = 0x00,
+        UI_MODE_TYPE_NORMAL = 0x01,
+        UI_MODE_TYPE_CAR = 0x02,
 
         // uiMode bits for the night switch.
         MASK_UI_MODE_NIGHT = 0x30,
@@ -1086,7 +1087,7 @@
             }
         }
 
-        if (screenConfig || o.screenConfig) {
+        if (screenLayout || o.screenLayout) {
             if (((screenLayout^o.screenLayout) & MASK_SCREENSIZE) != 0) {
                 if (!(screenLayout & MASK_SCREENSIZE)) return false;
                 if (!(o.screenLayout & MASK_SCREENSIZE)) return true;
@@ -1102,7 +1103,7 @@
             if (!o.orientation) return true;
         }
 
-        if (screenConfig || o.screenConfig) {
+        if (uiMode || o.uiMode) {
             if (((uiMode^o.uiMode) & MASK_UI_MODE_TYPE) != 0) {
                 if (!(uiMode & MASK_UI_MODE_TYPE)) return false;
                 if (!(o.uiMode & MASK_UI_MODE_TYPE)) return true;
@@ -1203,7 +1204,7 @@
                 }
             }
 
-            if (screenConfig || o.screenConfig) {
+            if (screenLayout || o.screenLayout) {
                 if (((screenLayout^o.screenLayout) & MASK_SCREENSIZE) != 0
                         && (requested->screenLayout & MASK_SCREENSIZE)) {
                     return (screenLayout & MASK_SCREENSIZE);
@@ -1218,7 +1219,7 @@
                 return (orientation);
             }
 
-            if (screenConfig || o.screenConfig) {
+            if (uiMode || o.uiMode) {
                 if (((uiMode^o.uiMode) & MASK_UI_MODE_TYPE) != 0
                         && (requested->uiMode & MASK_UI_MODE_TYPE)) {
                     return (uiMode & MASK_UI_MODE_TYPE);
diff --git a/libs/utils/ResourceTypes.cpp b/libs/utils/ResourceTypes.cpp
index 5f89788..6da11b5 100644
--- a/libs/utils/ResourceTypes.cpp
+++ b/libs/utils/ResourceTypes.cpp
@@ -4149,22 +4149,165 @@
                     } else {
                         sprintf(density, "%d", (int)dval);
                     }
-                    printf("      config %d lang=%c%c cnt=%c%c orien=%d touch=%d density=%s key=%d infl=%d nav=%d w=%d h=%d sz=%d lng=%d\n",
-                           (int)configIndex,
-                           type->config.language[0] ? type->config.language[0] : '-',
-                           type->config.language[1] ? type->config.language[1] : '-',
-                           type->config.country[0] ? type->config.country[0] : '-',
-                           type->config.country[1] ? type->config.country[1] : '-',
-                           type->config.orientation,
-                           type->config.touchscreen,
-                           density,
-                           type->config.keyboard,
-                           type->config.inputFlags,
-                           type->config.navigation,
-                           dtohs(type->config.screenWidth),
-                           dtohs(type->config.screenHeight),
-                           type->config.screenLayout&ResTable_config::MASK_SCREENSIZE,
-                           type->config.screenLayout&ResTable_config::MASK_SCREENLONG);
+                    printf("      config %d", (int)configIndex);
+                    if (type->config.mcc != 0) {
+                        printf(" mcc=%d", dtohs(type->config.mcc));
+                    }
+                    if (type->config.mnc != 0) {
+                        printf(" mnc=%d", dtohs(type->config.mnc));
+                    }
+                    if (type->config.locale != 0) {
+                        printf(" lang=%c%c cnt=%c%c",
+                               type->config.language[0] ? type->config.language[0] : '-',
+                               type->config.language[1] ? type->config.language[1] : '-',
+                               type->config.country[0] ? type->config.country[0] : '-',
+                               type->config.country[1] ? type->config.country[1] : '-');
+                    }
+                    if (type->config.screenLayout != 0) {
+                        printf(" sz=%d",
+                                type->config.screenLayout&ResTable_config::MASK_SCREENSIZE);
+                        switch (type->config.screenLayout&ResTable_config::MASK_SCREENSIZE) {
+                            case ResTable_config::SCREENSIZE_SMALL:
+                                printf(" (small)");
+                                break;
+                            case ResTable_config::SCREENSIZE_NORMAL:
+                                printf(" (normal)");
+                                break;
+                            case ResTable_config::SCREENSIZE_LARGE:
+                                printf(" (large)");
+                                break;
+                        }
+                        printf(" lng=%d",
+                                type->config.screenLayout&ResTable_config::MASK_SCREENLONG);
+                        switch (type->config.screenLayout&ResTable_config::MASK_SCREENLONG) {
+                            case ResTable_config::SCREENLONG_NO:
+                                printf(" (notlong)");
+                                break;
+                            case ResTable_config::SCREENLONG_YES:
+                                printf(" (long)");
+                                break;
+                        }
+                    }
+                    if (type->config.orientation != 0) {
+                        printf(" orient=%d", type->config.orientation);
+                        switch (type->config.orientation) {
+                            case ResTable_config::ORIENTATION_PORT:
+                                printf(" (port)");
+                                break;
+                            case ResTable_config::ORIENTATION_LAND:
+                                printf(" (land)");
+                                break;
+                            case ResTable_config::ORIENTATION_SQUARE:
+                                printf(" (square)");
+                                break;
+                        }
+                    }
+                    if (type->config.uiMode != 0) {
+                        printf(" type=%d",
+                                type->config.uiMode&ResTable_config::MASK_UI_MODE_TYPE);
+                        switch (type->config.uiMode&ResTable_config::MASK_UI_MODE_TYPE) {
+                            case ResTable_config::UI_MODE_TYPE_NORMAL:
+                                printf(" (normal)");
+                                break;
+                            case ResTable_config::UI_MODE_TYPE_CAR:
+                                printf(" (car)");
+                                break;
+                        }
+                        printf(" night=%d",
+                                type->config.uiMode&ResTable_config::MASK_UI_MODE_NIGHT);
+                        switch (type->config.uiMode&ResTable_config::MASK_UI_MODE_NIGHT) {
+                            case ResTable_config::UI_MODE_NIGHT_NO:
+                                printf(" (no)");
+                                break;
+                            case ResTable_config::UI_MODE_NIGHT_YES:
+                                printf(" (yes)");
+                                break;
+                        }
+                    }
+                    if (dval != 0) {
+                        printf(" density=%s", density);
+                    }
+                    if (type->config.touchscreen != 0) {
+                        printf(" touch=%d", type->config.touchscreen);
+                        switch (type->config.touchscreen) {
+                            case ResTable_config::TOUCHSCREEN_NOTOUCH:
+                                printf(" (notouch)");
+                                break;
+                            case ResTable_config::TOUCHSCREEN_STYLUS:
+                                printf(" (stylus)");
+                                break;
+                            case ResTable_config::TOUCHSCREEN_FINGER:
+                                printf(" (finger)");
+                                break;
+                        }
+                    }
+                    if (type->config.inputFlags != 0) {
+                        printf(" keyhid=%d", type->config.inputFlags&ResTable_config::MASK_KEYSHIDDEN);
+                        switch (type->config.inputFlags&ResTable_config::MASK_KEYSHIDDEN) {
+                            case ResTable_config::KEYSHIDDEN_NO:
+                                printf(" (no)");
+                                break;
+                            case ResTable_config::KEYSHIDDEN_YES:
+                                printf(" (yes)");
+                                break;
+                            case ResTable_config::KEYSHIDDEN_SOFT:
+                                printf(" (soft)");
+                                break;
+                        }
+                        printf(" navhid=%d", type->config.inputFlags&ResTable_config::MASK_NAVHIDDEN);
+                        switch (type->config.inputFlags&ResTable_config::MASK_NAVHIDDEN) {
+                            case ResTable_config::NAVHIDDEN_NO:
+                                printf(" (no)");
+                                break;
+                            case ResTable_config::NAVHIDDEN_YES:
+                                printf(" (yes)");
+                                break;
+                        }
+                    }
+                    if (type->config.keyboard != 0) {
+                        printf(" kbd=%d", type->config.keyboard);
+                        switch (type->config.keyboard) {
+                            case ResTable_config::KEYBOARD_NOKEYS:
+                                printf(" (nokeys)");
+                                break;
+                            case ResTable_config::KEYBOARD_QWERTY:
+                                printf(" (qwerty)");
+                                break;
+                            case ResTable_config::KEYBOARD_12KEY:
+                                printf(" (12key)");
+                                break;
+                        }
+                    }
+                    if (type->config.navigation != 0) {
+                        printf(" nav=%d", type->config.navigation);
+                        switch (type->config.navigation) {
+                            case ResTable_config::NAVIGATION_NONAV:
+                                printf(" (nonav)");
+                                break;
+                            case ResTable_config::NAVIGATION_DPAD:
+                                printf(" (dpad)");
+                                break;
+                            case ResTable_config::NAVIGATION_TRACKBALL:
+                                printf(" (trackball)");
+                                break;
+                            case ResTable_config::NAVIGATION_WHEEL:
+                                printf(" (wheel)");
+                                break;
+                        }
+                    }
+                    if (type->config.screenWidth != 0) {
+                        printf(" w=%d", dtohs(type->config.screenWidth));
+                    }
+                    if (type->config.screenHeight != 0) {
+                        printf(" h=%d", dtohs(type->config.screenHeight));
+                    }
+                    if (type->config.sdkVersion != 0) {
+                        printf(" sdk=%d", dtohs(type->config.sdkVersion));
+                    }
+                    if (type->config.minorVersion != 0) {
+                        printf(" mver=%d", dtohs(type->config.minorVersion));
+                    }
+                    printf("\n");
                     size_t entryCount = dtohl(type->entryCount);
                     uint32_t entriesStart = dtohl(type->entriesStart);
                     if ((entriesStart&0x3) != 0) {
diff --git a/tools/aapt/AaptAssets.cpp b/tools/aapt/AaptAssets.cpp
index 69b2207..6e7a66d 100644
--- a/tools/aapt/AaptAssets.cpp
+++ b/tools/aapt/AaptAssets.cpp
@@ -1133,6 +1133,26 @@
     getNavigationName(navigation.string(), &params);
     getScreenSizeName(screenSize.string(), &params);
     getVersionName(version.string(), &params);
+    
+    // Fix up version number based on specified parameters.
+    int minSdk = 0;
+    if ((params.uiMode&ResTable_config::MASK_UI_MODE_TYPE)
+                != ResTable_config::UI_MODE_TYPE_ANY
+            ||  (params.uiMode&ResTable_config::MASK_UI_MODE_NIGHT)
+                != ResTable_config::UI_MODE_NIGHT_ANY) {
+        minSdk = SDK_FROYO;
+    } else if ((params.screenLayout&ResTable_config::MASK_SCREENSIZE)
+                != ResTable_config::SCREENSIZE_ANY
+            ||  (params.screenLayout&ResTable_config::MASK_SCREENLONG)
+                != ResTable_config::SCREENLONG_ANY
+            || params.density != ResTable_config::DENSITY_DEFAULT) {
+        minSdk = SDK_DONUT;
+    }
+    
+    if (minSdk > params.sdkVersion) {
+        params.sdkVersion = minSdk;
+    }
+    
     return params;
 }
 
diff --git a/tools/aapt/AaptAssets.h b/tools/aapt/AaptAssets.h
index 9a848e4..eeb00c0 100644
--- a/tools/aapt/AaptAssets.h
+++ b/tools/aapt/AaptAssets.h
@@ -45,6 +45,15 @@
     AXIS_VERSION
 };
 
+enum {
+    SDK_CUPCAKE = 3,
+    SDK_DONUT = 4,
+    SDK_ECLAIR = 5,
+    SDK_ECLAIR_0_1 = 6,
+    SDK_MR1 = 7,
+    SDK_FROYO = 8,
+};
+
 /**
  * This structure contains a specific variation of a single file out
  * of all the variations it can have that we can have.
diff --git a/tools/aapt/Bundle.h b/tools/aapt/Bundle.h
index 558b587..08530a0 100644
--- a/tools/aapt/Bundle.h
+++ b/tools/aapt/Bundle.h
@@ -38,7 +38,8 @@
           mUpdate(false), mExtending(false),
           mRequireLocalization(false), mPseudolocalize(false),
           mUTF8(false), mEncodingSpecified(false), mValues(false),
-          mCompressionMethod(0), mOutputAPKFile(NULL), mManifestPackageNameOverride(NULL),
+          mCompressionMethod(0), mOutputAPKFile(NULL),
+          mManifestPackageNameOverride(NULL), mInstrumentationPackageNameOverride(NULL),
           mAutoAddOverlay(false), mAssetSourceDir(NULL), mProguardFile(NULL),
           mAndroidManifestFile(NULL), mPublicOutputFile(NULL),
           mRClassDir(NULL), mResourceIntermediatesDir(NULL),
@@ -90,6 +91,8 @@
     void setOutputAPKFile(const char* val) { mOutputAPKFile = val; }
     const char* getManifestPackageNameOverride() const { return mManifestPackageNameOverride; }
     void setManifestPackageNameOverride(const char * val) { mManifestPackageNameOverride = val; }
+    const char* getInstrumentationPackageNameOverride() const { return mInstrumentationPackageNameOverride; }
+    void setInstrumentationPackageNameOverride(const char * val) { mInstrumentationPackageNameOverride = val; }
     bool getAutoAddOverlay() { return mAutoAddOverlay; }
     void setAutoAddOverlay(bool val) { mAutoAddOverlay = val; }
 
@@ -183,6 +186,7 @@
     bool        mJunkPath;
     const char* mOutputAPKFile;
     const char* mManifestPackageNameOverride;
+    const char* mInstrumentationPackageNameOverride;
     bool        mAutoAddOverlay;
     const char* mAssetSourceDir;
     const char* mProguardFile;
diff --git a/tools/aapt/Command.cpp b/tools/aapt/Command.cpp
index ee07415..537ae5e 100644
--- a/tools/aapt/Command.cpp
+++ b/tools/aapt/Command.cpp
@@ -525,6 +525,8 @@
             bool actWallpaperService = false;
             bool specCameraFeature = false;
             bool hasCameraPermission = false;
+            bool specGpsFeature = false;
+            bool hasGpsPermission = false;
             int targetSdk = 0;
             int smallScreen = 1;
             int normalScreen = 1;
@@ -719,6 +721,8 @@
                                     REQUIRED_ATTR, NULL, 1);
                             if (name == "android.hardware.camera") {
                                 specCameraFeature = true;
+                            } else if (name == "android.hardware.location.gps") {
+                                specGpsFeature = true;
                             }
                             printf("uses-feature%s:'%s'\n",
                                     req ? "" : "-not-required", name.string());
@@ -734,6 +738,8 @@
                         if (name != "" && error == "") {
                             if (name == "android.permission.CAMERA") {
                                 hasCameraPermission = true;
+                            } else if (name == "android.permission.ACCESS_FINE_LOCATION") {
+                                hasGpsPermission = true;
                             }
                             printf("uses-permission:'%s'\n", name.string());
                         } else {
@@ -859,6 +865,14 @@
                 printf("uses-feature:'android.hardware.camera.autofocus'\n");
             }
 
+            if (!specGpsFeature && hasGpsPermission) {
+                // For applications that have not explicitly stated their
+                // GPS feature requirements, but have requested the "fine" (GPS)
+                // permission, we are going to give them compatibility treatment
+                // of requiring the equivalent to original android devices.
+                printf("uses-feature:'android.hardware.location.gps'\n");
+            }
+
             if (hasMainActivity) {
                 printf("main\n");
             }
diff --git a/tools/aapt/Main.cpp b/tools/aapt/Main.cpp
index 6d0a351..dd98c85 100644
--- a/tools/aapt/Main.cpp
+++ b/tools/aapt/Main.cpp
@@ -58,9 +58,10 @@
         " %s p[ackage] [-d][-f][-m][-u][-v][-x][-z][-M AndroidManifest.xml] \\\n"
         "        [-0 extension [-0 extension ...]] [-g tolerance] [-j jarfile] \\\n"
         "        [--min-sdk-version VAL] [--target-sdk-version VAL] \\\n"
-        "        [--max-sdk-version VAL] [--app-version VAL] \\\n"
-        "        [--app-version-name TEXT] [--custom-package VAL] [--utf16] \\\n"
-        "        [--auto-add-overlay] \\\n"
+        "        [--app-version VAL] [--app-version-name TEXT] [--custom-package VAL] \\\n"
+        "        [--rename-manifest-package PACKAGE] \\\n"
+        "        [--rename-instrumentation-target-package PACKAGE] \\\n"
+        "        [--utf16] [--auto-add-overlay] \\\n"
         "        [-I base-package [-I base-package ...]] \\\n"
         "        [-A asset-source-dir]  [-G class-list-file] [-P public-definitions-file] \\\n"
         "        [-S resource-sources [-S resource-sources ...]] "
@@ -127,8 +128,6 @@
         "       higher, the default encoding for resources will be in UTF-8.\n"
         "   --target-sdk-version\n"
         "       inserts android:targetSdkVersion in to manifest.\n"
-        "   --max-sdk-version\n"
-        "       inserts android:maxSdkVersion in to manifest.\n"
         "   --values\n"
         "       when used with \"dump resources\" also includes resource values.\n"
         "   --version-code\n"
@@ -139,6 +138,16 @@
         "       generates R.java into a different package.\n"
         "   --auto-add-overlay\n"
         "       Automatically add resources that are only in overlays.\n"
+        "   --rename-manifest-package\n"
+        "       Rewrite the manifest so that its package name is the package name\n"
+        "       given here.  Relative class names (for example .Foo) will be\n"
+        "       changed to absolute names with the old package so that the code\n"
+        "       does not need to change.\n"
+        "   --rename-instrumentation-target-package\n"
+        "       Rewrite the manifest so that all of its instrumentation\n"
+        "       components target the given package.  Useful when used in\n"
+        "       conjunction with --rename-manifest-package to fix tests against\n"
+        "       a package that has been renamed.\n"
         "   --utf16\n"
         "       changes default encoding for resources to UTF-16.  Only useful when API\n"
         "       level is set to 7 or higher where the default encoding is UTF-8.\n");
@@ -448,6 +457,15 @@
                         goto bail;
                     }
                     bundle.setManifestPackageNameOverride(argv[0]);
+                } else if (strcmp(cp, "-rename-instrumentation-target-package") == 0) {
+                    argc--;
+                    argv++;
+                    if (!argc) {
+                        fprintf(stderr, "ERROR: No argument supplied for '--rename-instrumentation-target-package' option\n");
+                        wantUsage = true;
+                        goto bail;
+                    }
+                    bundle.setInstrumentationPackageNameOverride(argv[0]);
                 } else if (strcmp(cp, "-auto-add-overlay") == 0) {
                     bundle.setAutoAddOverlay(true);
                 } else {
diff --git a/tools/aapt/Resource.cpp b/tools/aapt/Resource.cpp
index ae4bd14..7142b1c 100644
--- a/tools/aapt/Resource.cpp
+++ b/tools/aapt/Resource.cpp
@@ -562,9 +562,10 @@
     node->addAttribute(ns, attr, String16(value));
 }
 
-static void fullyQualifyClassName(String8& package, sp<XMLNode> node) {
+static void fullyQualifyClassName(const String8& package, sp<XMLNode> node,
+        const String16& attrName) {
     XMLNode::attribute_entry* attr = node->editAttribute(
-            String16("http://schemas.android.com/apk/res/android"), String16("name"));
+            String16("http://schemas.android.com/apk/res/android"), attrName);
     if (attr != NULL) {
         String8 name(attr->string);
 
@@ -635,19 +636,40 @@
         // Make class names fully qualified
         sp<XMLNode> application = root->getChildElement(String16(), String16("application"));
         if (application != NULL) {
-            fullyQualifyClassName(origPackage, application);
+            fullyQualifyClassName(origPackage, application, String16("name"));
 
             Vector<sp<XMLNode> >& children = const_cast<Vector<sp<XMLNode> >&>(application->getChildren());
             for (size_t i = 0; i < children.size(); i++) {
                 sp<XMLNode> child = children.editItemAt(i);
                 String8 tag(child->getElementName());
                 if (tag == "activity" || tag == "service" || tag == "receiver" || tag == "provider") {
-                    fullyQualifyClassName(origPackage, child);
+                    fullyQualifyClassName(origPackage, child, String16("name"));
+                } else if (tag == "activity-alias") {
+                    fullyQualifyClassName(origPackage, child, String16("name"));
+                    fullyQualifyClassName(origPackage, child, String16("targetActivity"));
                 }
             }
         }
     }
 
+    // Deal with manifest package name overrides
+    const char* instrumentationPackageNameOverride = bundle->getInstrumentationPackageNameOverride();
+    if (instrumentationPackageNameOverride != NULL) {
+        // Fix up instrumentation targets.
+        Vector<sp<XMLNode> >& children = const_cast<Vector<sp<XMLNode> >&>(root->getChildren());
+        for (size_t i = 0; i < children.size(); i++) {
+            sp<XMLNode> child = children.editItemAt(i);
+            String8 tag(child->getElementName());
+            if (tag == "instrumentation") {
+                XMLNode::attribute_entry* attr = child->editAttribute(
+                        String16("http://schemas.android.com/apk/res/android"), String16("targetPackage"));
+                if (attr != NULL) {
+                    attr->string.setTo(String16(instrumentationPackageNameOverride));
+                }
+            }
+        }
+    }
+    
     return NO_ERROR;
 }