问题
When an activity is launched, the classes.dex file is loaded by the system and start executing the instructions. I need to get readonly access to the classes.dex of the same application under which the current activity is executing.
After searching for hours on the net, I could only infer that the Android Security system does not allow access to the application sandbox.
However, i need readonly access to the classes.dex file in order to accomplish my task.
Does anyone have a insight on this?
Thanks in advance!
回答1:
Depends on what you are trying to do, but you can access the DexFile :
String sourceDir = context.getApplicationInfo().sourceDir;
DexFile dexFile = new DexFile(sourceDir);
it gives you a http://developer.android.com/reference/dalvik/system/DexFile.html which you can enumerate, and load classes from.
回答2:
You may be able to obtain an InputStream for "classes.dex" in the following way:
- Retrieve the path to the apk container of your application.
- Retrieve the "classes.dex" entry inside your apk container thanks to JarFile class.
- Get an Input Stream for it.
Here is a snippet of code to exemplify:
// Get the path to the apk container.
String apkPath = getApplicationInfo().sourceDir;
JarFile containerJar = null;
try {
// Open the apk container as a jar..
containerJar = new JarFile(apkPath);
// Look for the "classes.dex" entry inside the container.
ZipEntry ze = containerJar.getEntry("classes.dex");
// If this entry is present in the jar container
if (ze != null) {
// Get an Input Stream for the "classes.dex" entry
InputStream in = containerJar.getInputStream(ze);
// Perform read operations on the stream like in.read();
// Notice that you reach this part of the code
// only if the InputStream was properly created;
// otherwise an IOException is raised
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (containerJar != null)
try {
containerJar.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Hope it helps!
来源:https://stackoverflow.com/questions/10122918/how-to-access-classes-dex-of-an-android-application