Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion dexmaker/src/main/java/com/android/dx/AppDataDirGuesser.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public File guess() {
try {
ClassLoader classLoader = guessSuitableClassLoader();
// Check that we have an instance of the PathClassLoader.
Class<?> clazz = Class.forName("dalvik.system.PathClassLoader");
Class<?> clazz = Class.forName("dalvik.system.BaseDexClassLoader");
clazz.cast(classLoader);
// Use the toString() method to calculate the data directory.
String pathFromThisClassLoader = getPathFromThisClassLoader(classLoader, clazz);
Expand All @@ -44,7 +44,16 @@ public File guess() {
}
} catch (ClassCastException ignored) {
} catch (ClassNotFoundException ignored) {
} catch (Throwable ignored) {
}

// Fallback
File fallback = guessContextCacheDir();
if (fallback != null) {
System.out.println("DexMaker: Guessed dexcache directory via ActivityThread: " + fallback.getAbsolutePath());
return fallback;
}

return null;
}

Expand Down Expand Up @@ -217,6 +226,25 @@ Integer getProcessUid() {
}
}

private File guessContextCacheDir() {
try {
Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
Method currentApplicationMethod = activityThreadClass.getMethod("currentApplication");
Object app = currentApplicationMethod.invoke(null);
if (app != null) {
Method getCacheDirMethod = app.getClass().getMethod("getCacheDir");
File cacheDir = (File) getCacheDirMethod.invoke(app);
if (cacheDir != null) {
if (isWriteableDirectory(cacheDir)) {
return cacheDir;
}
}
}
} catch (Throwable ignored) {
}
return null;
}

File guessUserDataDirectory(String packageName) {
Integer uid = getProcessUid();
if (uid == null) {
Expand Down