Skip to content

Commit

Permalink
Merge pull request #327 from tracytheron/master
Browse files Browse the repository at this point in the history
Update sugar for multi-dex
  • Loading branch information
sibelius committed Oct 27, 2015
2 parents 963716e + 563738c commit ed73852
Showing 1 changed file with 63 additions and 12 deletions.
75 changes: 63 additions & 12 deletions library/src/main/java/com/orm/util/ReflectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Build;
import android.util.Log;

import com.orm.SugarRecord;
Expand Down Expand Up @@ -286,33 +289,53 @@ private static Class getDomainClass(String className, Context context) {
}
}

private static final String EXTRACTED_NAME_EXT = ".classes";
private static final String EXTRACTED_SUFFIX = ".zip";

private static final String SECONDARY_FOLDER_NAME = "code_cache" + File.separator +
"secondary-dexes";

private static final String PREFS_FILE = "multidex.version";
private static final String KEY_DEX_NUMBER = "dex.number";

private static SharedPreferences getMultiDexPreferences(Context context) {
return context.getSharedPreferences(PREFS_FILE,
Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB
? Context.MODE_PRIVATE
: Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS);
}

private static List<String> getAllClasses(Context context) throws PackageManager.NameNotFoundException, IOException {
String packageName = ManifestHelper.getDomainPackageName(context);
String path = getSourcePath(context);
List<String> classNames = new ArrayList<String>();
List<String> paths = getSourcePaths(context);
List<String> classNames = new ArrayList<>();
DexFile dexfile = null;
try {
dexfile = new DexFile(path);
Enumeration<String> dexEntries = dexfile.entries();
while (dexEntries.hasMoreElements()) {
String className = dexEntries.nextElement();
if (className.startsWith(packageName)) classNames.add(className);
for (int i = 0; i <paths.size(); i++){
String path = paths.get(i);
if (path.endsWith(EXTRACTED_SUFFIX)) {
//NOT use new DexFile(path) here, because it will throw "permission error in /data/dalvik-cache"
dexfile = DexFile.loadDex(path, path + ".tmp", 0);
} else {
dexfile = new DexFile(path);
}

Enumeration<String> dexEntries = dexfile.entries();
while (dexEntries.hasMoreElements()) {
classNames.add(dexEntries.nextElement());
}
}
} catch (NullPointerException e) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Enumeration<URL> urls = classLoader.getResources("");
List<String> fileNames = new ArrayList<String>();
while (urls.hasMoreElements()) {
List<String> fileNames = new ArrayList<String>();
String classDirectoryName = urls.nextElement().getFile();
if (classDirectoryName.contains("bin") || classDirectoryName.contains("classes")) {
File classDirectory = new File(classDirectoryName);
for (File filePath : classDirectory.listFiles()) {
populateFiles(filePath, fileNames, "");
}
for (String fileName : fileNames) {
if (fileName.startsWith(packageName)) classNames.add(fileName);
}
classNames.addAll(fileNames);
}
}
} finally {
Expand All @@ -321,6 +344,34 @@ private static List<String> getAllClasses(Context context) throws PackageManager
return classNames;
}

public static List<String> getSourcePaths(Context context) throws PackageManager.NameNotFoundException, IOException {
ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0);
File sourceApk = new File(applicationInfo.sourceDir);
File dexDir = new File(applicationInfo.dataDir, SECONDARY_FOLDER_NAME);

List<String> sourcePaths = new ArrayList<String>();
//default apk path
sourcePaths.add(applicationInfo.sourceDir);

String extractedFilePrefix = sourceApk.getName() + EXTRACTED_NAME_EXT;

//get the dex number
int totalDexNumber = getMultiDexPreferences(context).getInt(KEY_DEX_NUMBER, 1);

//get other dexes
for (int i = 2; i <= totalDexNumber; i++) {
String fileName = extractedFilePrefix + i + EXTRACTED_SUFFIX;
File extractedFile = new File(dexDir, fileName);
if (extractedFile.isFile()) {
sourcePaths.add(extractedFile.getAbsolutePath());
} else {
throw new IOException("Missing extracted secondary dex file '" +
extractedFile.getPath() + "'");
}
}

return sourcePaths;
}
private static void populateFiles(File path, List<String> fileNames, String parent) {
if (path.isDirectory()) {
for (File newPath : path.listFiles()) {
Expand Down

0 comments on commit ed73852

Please sign in to comment.