0

Can I load an apk that I placed in my res/raw/ folder using a ClassLoader? And to load the apk I use openRawResources().

Hank
  • 3,367
  • 10
  • 48
  • 86

3 Answers3

1

I don't know if you can do this from an asset, but if you extract the asset to the file system, you can use the following sequence (which ignores exceptions that you'll need to handle). From here you can use normal reflection methods on 'loadedClass'.

DexClassLoader classLoader = new DexClassLoader(
    "/path/to/your/apk", getFilesDir().getAbsolutePath(),
    null, getClass().getClassLoader());
Class<?> loadedClass = classLoader.loadClass("full.package.and.class.name");
mah
  • 39,056
  • 9
  • 76
  • 93
0

You sure can load the APK-file, but you can't simply "launch" it (as it needs to install the App first), and your App can't install new Applications (due to security policy's).

If you need some other App for your Program, you should mention this at the first startup or something like that.


You can open an .apk-file using something to unzip (like 7zip). But you can't use the classes inside it as there are no normal .class-files in it.

This is due to the fact that Android doesn't use a normal JVM but a DVM. This is a JVM, based on the Apache Harmony Project and optimized to run on devices with low memory (like Android Phones). This JVM doesn't use normal Java Byte-Code (which is in the .class-files) but it's own Dalvik-Byte-Code which is created by the Android SDK (the tool is dx) when you export your project.

So to make it short: You can't access the classes in an APK-file from your App. Not if you don't have the Source Code to compile the classes yourself.

See the Wikipedia Article.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
  • I don't want to install it, I just want the classes, but how do I load it? – Hank May 25 '11 at 18:50
  • 1
    Then how does DexClassLoader get class files from an apk? – Hank May 25 '11 at 19:22
  • Yes, looks like you can use it to load the `.dex`-files. See [here](http://stackoverflow.com/questions/2903260/android-using-dexclassloader-to-load-apk-file) – Lukas Knuth May 25 '11 at 20:25
0

You can always dump this file to the file system, and load it using reflection and DexFile, which gives you reflection based access to your class.

Kerem Kusmezer
  • 492
  • 7
  • 15