Skip to content

Commit

Permalink
Added custom patch for multiple dex files
Browse files Browse the repository at this point in the history
  • Loading branch information
magneticflux- committed Nov 3, 2017
1 parent 081d27b commit dcd7f06
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 4 deletions.
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
defaultConfig {
minSdkVersion 19
targetSdkVersion 19
versionCode 5
versionName "3.5.2"
versionCode 6
versionName "3.5.3"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

ndk {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.List;

import dalvik.system.DexFile;

@SuppressWarnings("WeakerAccess")
public class InstantRunDexHelper {

Expand All @@ -56,8 +57,7 @@ public class InstantRunDexHelper {
* @param context the application context
* @return A list of class names
*/
public static List<String> getAllClassNames(Context context)
{
public static List<String> getAllClassNames(Context context) {
ApplicationInfo applicationInfo = null;
List<String> classNames = new ArrayList<String>();

Expand Down Expand Up @@ -90,6 +90,11 @@ public static List<String> getAllClassNames(Context context)
}
}
}
try {
classNames.addAll(MultiDexHelper.getAllClasses(context));
} catch (PackageManager.NameNotFoundException | IOException e) {
RobotLog.ee(TAG, e, "Error loading extra dex files.");
}
return classNames;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.firstinspires.ftc.robotcore.internal.opmode;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import dalvik.system.DexFile;

/**
* Created by [email protected] on 14/11/13.
*/
public class MultiDexHelper {
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, Context.MODE_MULTI_PROCESS);
}

/**
* get all the dex path
*
* @param context the application context
* @return all the dex path
* @throws PackageManager.NameNotFoundException
* @throws IOException
*/
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<>();
sourcePaths.add(applicationInfo.sourceDir); //add the default apk path

//the prefix of extracted file, ie: test.classes
String extractedFilePrefix = sourceApk.getName() + EXTRACTED_NAME_EXT;
//the total dex numbers
int totalDexNumber = getMultiDexPreferences(context).getInt(KEY_DEX_NUMBER, 1);

for (int secondaryNumber = 2; secondaryNumber <= totalDexNumber; secondaryNumber++) {
//for each dex file, ie: test.classes2.zip, test.classes3.zip...
String fileName = extractedFilePrefix + secondaryNumber + EXTRACTED_SUFFIX;
File extractedFile = new File(dexDir, fileName);
if (extractedFile.isFile()) {
sourcePaths.add(extractedFile.getAbsolutePath());
//we ignore the verify zip part
} else {
throw new IOException("Missing extracted secondary dex file '" +
extractedFile.getPath() + "'");
}
}

return sourcePaths;
}

/**
* get all the classes name in "classes.dex", "classes2.dex", ....
*
* @param context the application context
* @return all the classes name
* @throws PackageManager.NameNotFoundException
* @throws IOException
*/
public static List<String> getAllClasses(Context context) throws PackageManager.NameNotFoundException, IOException {
List<String> classNames = new ArrayList<>();
for (String path : getSourcePaths(context)) {
try {
DexFile dexfile;
if (path.endsWith(EXTRACTED_SUFFIX)) {
//NOT use new DexFile(path), 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 (IOException e) {
throw new IOException("Error at loading dex file '" +
path + "'");
}
}
return classNames;
}
}

0 comments on commit dcd7f06

Please sign in to comment.