Android: How to dynamically load classes from a JAR file?
Asked Answered
C

1

3

I'm trying to dynamically load a class at runtime on the Android platform. The class is contained within a separate library JAR file, but packaged with the APK (as per the new library mechanism in the SDK). When using Class.forname method, I received a class not found exception. I've seen some discussion around the DexClassLoader method, but I can't find a good example of how it's used (and whether it's the best way to go about it - it seems a lot more complicated than the forname approach!).

I would be very grateful if someone could provide an example snippet of code of how to go about this.

Many thanks for your help.

Cammiecammy answered 30/10, 2011 at 19:57 Comment(0)
U
4

Here's an example to dynamically load the DevicePolicyManager class.

Class myClass =                                                                        
  ClassLoader.getSystemClassLoader().loadClass("android.app.admin.DevicePolicyManager")

Object DPMInstance = myClass.newInstance();                                            

Method myMethod = myClass.getMethod("setPasswordQuality",                              
                                    new Class[] { ComponentName.class,                 
                                                  int.class });                        
myMethod.invoke(DPMInstance,                                                           
                new Object[] { myComponentName,                                        
                               PASSWORD_QUALITY_NUMERIC }); 

This is useful so you can choose to only load a class when the device is running a high enough SDK version.

Ufa answered 31/10, 2011 at 0:39 Comment(5)
Thanks Dan. However, I still get a ClassNotFoundException using this method. The class will load ok if it's within the main application, rather than the JAR file, so perhaps the JAR needs to be expanded first to obtain the file if it's not directly reference in code (and therefore not loaded by the ClassLoader)?Cammiecammy
If the JAR has already been included in your APK then I would expect the class to be available. However, does this question give you any extra hints? #195198Ufa
Assuming you are using Eclipse, I've tended to use Properties -> Java Build Path -> Projects -> Add to a required project. I don't know how that relates to the Properties -> Android -> Library feature (which is what I am guessing you are using)...Ufa
This is what iam searching for last 2 weeks.. thanks alot buddy.. thank you very much for answer..!! @DanJPresidency
I have the same issue here. a jar with a group of dynaminc clases that are not referenced in any other way except for dynamic loading. II would think google in trying not to use all the memory on a mobile device would try to cull out "dead code" from large jars people commonly include in applications. I wonder if this is what is happening if the classes are not "imported" or otherwise explicitly loaded via reference that the dex parser manager etc can discover. I wonder if this is the case?Anent

© 2022 - 2024 — McMap. All rights reserved.