summaryrefslogtreecommitdiff
path: root/library/src
diff options
context:
space:
mode:
authorYohann Roussel <yroussel@google.com>2014-07-08 16:50:10 +0200
committerYohann Roussel <yroussel@google.com>2014-07-09 20:01:23 +0200
commitd79604bd38c101b54e41745f85ddc2e04d978af2 (patch)
tree170e38c06b9c475ab7e227cdfc56a60514750a82 /library/src
parent591acd99f16989233ff3e226546272bb60024559 (diff)
downloadmultidex-d79604bd38c101b54e41745f85ddc2e04d978af2.tar.gz
Clear old secondary dex dir when multidex becomes supported.
There may be a need for clearing those unused extracted files after an OTA bringing Art L on the device. Bug: 10447095 Change-Id: I80b9c0afa2bd8dfa0cf04e96fb04ba2527da0fe5
Diffstat (limited to 'library/src')
-rw-r--r--library/src/android/support/multidex/MultiDex.java94
-rw-r--r--library/src/android/support/multidex/MultiDexExtractor.java4
2 files changed, 75 insertions, 23 deletions
diff --git a/library/src/android/support/multidex/MultiDex.java b/library/src/android/support/multidex/MultiDex.java
index cd38112..68462c2 100644
--- a/library/src/android/support/multidex/MultiDex.java
+++ b/library/src/android/support/multidex/MultiDex.java
@@ -20,6 +20,7 @@ import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
+import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.util.Log;
@@ -88,6 +89,12 @@ public final class MultiDex {
Log.i(TAG, "install");
if (IS_VM_MULTIDEX_CAPABLE) {
Log.i(TAG, "VM has multidex support, MultiDex support library is disabled.");
+ try {
+ clearOldDexDir(context);
+ } catch (Throwable t) {
+ Log.w(TAG, "Something went wrong when trying to clear old MultiDex extraction, "
+ + "continuing without cleaning.", t);
+ }
return;
}
@@ -97,28 +104,9 @@ public final class MultiDex {
}
try {
- PackageManager pm;
- String packageName;
- try {
- pm = context.getPackageManager();
- packageName = context.getPackageName();
- } catch (RuntimeException e) {
- /* Ignore those exceptions so that we don't break tests relying on Context like
- * a android.test.mock.MockContext or a android.content.ContextWrapper with a null
- * base Context.
- */
- Log.w(TAG, "Failure while trying to obtain ApplicationInfo from Context. " +
- "Must be running in test mode. Skip patching.", e);
- return;
- }
- if (pm == null || packageName == null) {
- // This is most likely a mock context, so just return without patching.
- return;
- }
- ApplicationInfo applicationInfo =
- pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
+ ApplicationInfo applicationInfo = getApplicationInfo(context);
if (applicationInfo == null) {
- // This is from a mock context, so just return without patching.
+ // Looks like running on a test Context, so just return without patching.
return;
}
@@ -188,6 +176,31 @@ public final class MultiDex {
Log.i(TAG, "install done");
}
+ private static ApplicationInfo getApplicationInfo(Context context)
+ throws NameNotFoundException {
+ PackageManager pm;
+ String packageName;
+ try {
+ pm = context.getPackageManager();
+ packageName = context.getPackageName();
+ } catch (RuntimeException e) {
+ /* Ignore those exceptions so that we don't break tests relying on Context like
+ * a android.test.mock.MockContext or a android.content.ContextWrapper with a null
+ * base Context.
+ */
+ Log.w(TAG, "Failure while trying to obtain ApplicationInfo from Context. " +
+ "Must be running in test mode. Skip patching.", e);
+ return null;
+ }
+ if (pm == null || packageName == null) {
+ // This is most likely a mock context, so just return without patching.
+ return null;
+ }
+ ApplicationInfo applicationInfo =
+ pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
+ return applicationInfo;
+ }
+
/**
* Identifies if the current VM has a native support for multidex, meaning there is no need for
* additional installation by this library.
@@ -320,6 +333,45 @@ public final class MultiDex {
jlrField.set(instance, combined);
}
+ private static void clearOldDexDir(Context context) throws Exception {
+ ApplicationInfo applicationInfo = getApplicationInfo(context);
+ if (applicationInfo == null) {
+ // Looks like running on a test Context, so just return.
+ return;
+ }
+
+ synchronized (installedApk) {
+ String apkPath = applicationInfo.sourceDir;
+ if (installedApk.contains(apkPath)) {
+ return;
+ }
+ installedApk.add(apkPath);
+ }
+ File dexDir = new File(context.getFilesDir(), SECONDARY_FOLDER_NAME);
+ if (dexDir.isDirectory()) {
+ Log.i(TAG, "Clearing old secondary dex dir (" + dexDir.getPath() + ").");
+ File[] files = dexDir.listFiles();
+ if (files == null) {
+ Log.w(TAG, "Failed to list secondary dex dir content (" + dexDir.getPath() + ").");
+ return;
+ }
+ for (File oldFile : files) {
+ Log.i(TAG, "Trying to delete old file " + oldFile.getPath() + " of size "
+ + oldFile.length());
+ if (!oldFile.delete()) {
+ Log.w(TAG, "Failed to delete old file " + oldFile.getPath());
+ } else {
+ Log.i(TAG, "Deleted old file " + oldFile.getPath());
+ }
+ }
+ if (!dexDir.delete()) {
+ Log.w(TAG, "Failed to delete secondary dex dir " + dexDir.getPath());
+ } else {
+ Log.i(TAG, "Deleted old secondary dex dir " + dexDir.getPath());
+ }
+ }
+ }
+
/**
* Installer for platform versions 19.
*/
diff --git a/library/src/android/support/multidex/MultiDexExtractor.java b/library/src/android/support/multidex/MultiDexExtractor.java
index 99c5269..fd70dee 100644
--- a/library/src/android/support/multidex/MultiDexExtractor.java
+++ b/library/src/android/support/multidex/MultiDexExtractor.java
@@ -273,12 +273,12 @@ final class MultiDexExtractor {
return;
}
for (File oldFile : files) {
- Log.w(TAG, "Trying to delete old file " + oldFile.getPath() + " of size " +
+ Log.i(TAG, "Trying to delete old file " + oldFile.getPath() + " of size " +
oldFile.length());
if (!oldFile.delete()) {
Log.w(TAG, "Failed to delete old file " + oldFile.getPath());
} else {
- Log.w(TAG, "Deleted old file " + oldFile.getPath());
+ Log.i(TAG, "Deleted old file " + oldFile.getPath());
}
}
}