Remove lingering system app native libs in /data

If a system app had a lingering native library in /data/data/<app>/lib,
it would prefer that over the one in /system/lib due to recent changed
in the Dalvik JNI class loading code.

To "fix" that we need to check if there are any native libraries in a
/data/data/<app>/lib directory for any non-updated system apps and
delete them during scanning.

Change-Id: If3a22e41a8531e9e5a44ba001dcea46253d47d45
diff --git a/services/java/com/android/server/PackageManagerService.java b/services/java/com/android/server/PackageManagerService.java
index 58ea4db..050e0c8 100644
--- a/services/java/com/android/server/PackageManagerService.java
+++ b/services/java/com/android/server/PackageManagerService.java
@@ -140,7 +140,6 @@
     private static final boolean DEBUG_PREFERRED = false;
     private static final boolean DEBUG_UPGRADE = false;
     private static final boolean DEBUG_INSTALL = false;
-    private static final boolean DEBUG_NATIVE = false;
 
     private static final boolean MULTIPLE_APPLICATION_UIDS = true;
     private static final int RADIO_UID = Process.PHONE_UID;
@@ -3276,10 +3275,23 @@
              *        In other words, we're going to unpack the binaries
              *        only for non-system apps and system app upgrades.
              */
-            if ((!isSystemApp(pkg) || isUpdatedSystemApp(pkg)) && !isExternal(pkg)) {
-                Log.i(TAG, path + " changed; unpacking");
-                File sharedLibraryDir = new File(pkg.applicationInfo.nativeLibraryDir);
-                NativeLibraryHelper.copyNativeBinariesLI(scanFile, sharedLibraryDir);
+            if (pkg.applicationInfo.nativeLibraryDir != null) {
+                final File sharedLibraryDir = new File(pkg.applicationInfo.nativeLibraryDir);
+                if (isSystemApp(pkg) && !isUpdatedSystemApp(pkg)) {
+                    /*
+                     * Upgrading from a previous version of the OS sometimes
+                     * leaves native libraries in the /data/data/<app>/lib
+                     * directory for system apps even when they shouldn't be.
+                     * Recent changes in the JNI library search path
+                     * necessitates we remove those to match previous behavior.
+                     */
+                    if (NativeLibraryHelper.removeNativeBinariesFromDirLI(sharedLibraryDir)) {
+                        Log.i(TAG, "removed obsolete native libraries for system package " + path);
+                    }
+                } else if (!isExternal(pkg)) {
+                    Log.i(TAG, path + " changed; unpacking");
+                    NativeLibraryHelper.copyNativeBinariesLI(scanFile, sharedLibraryDir);
+                }
             }
             pkg.mScanPath = path;